@Override
 public void lateBinding(@NotNull DBCSession session, List<Object[]> rows) throws DBException {
   DBSEntityAttribute entityAttribute = getEntityAttribute();
   if (entityAttribute != null) {
     referrers = DBUtils.getAttributeReferrers(session.getProgressMonitor(), entityAttribute);
   }
   super.lateBinding(session, rows);
 }
Exemplo n.º 2
0
 public void extractData(DBCSession session) throws DBCException {
   statistics = new DBCStatistics();
   if (queries.size() != 1) {
     throw new DBCException("Invalid state of SQL Query job");
   }
   resultSetNumber = 0;
   SQLQuery query = queries.get(0);
   session.getProgressMonitor().beginTask(query.getQuery(), 1);
   try {
     boolean result = executeSingleQuery(session, query, true);
     if (!result && lastError != null) {
       if (lastError instanceof DBCException) {
         throw (DBCException) lastError;
       } else {
         throw new DBCException(lastError, getExecutionContext().getDataSource());
       }
     } else if (result) {
       showExecutionResult(session);
     }
   } finally {
     session.getProgressMonitor().done();
   }
 }
 public DBDAttributeBindingMeta(
     @NotNull DBCSession session, @NotNull DBCAttributeMetaData metaAttribute) {
   super(DBUtils.findValueHandler(session, metaAttribute));
   this.dataSource = session.getDataSource();
   this.metaAttribute = metaAttribute;
 }
Exemplo n.º 4
0
  private boolean fetchQueryData(
      DBCSession session,
      DBCResultSet resultSet,
      SQLQueryResult result,
      DBDDataReceiver dataReceiver,
      boolean updateStatistics)
      throws DBCException {
    if (dataReceiver == null) {
      // No data pump - skip fetching stage
      return false;
    }
    if (resultSet == null) {
      return false;
    }
    boolean keepCursor = keepStatementOpen();

    if (keepCursor) {
      curResultSets.add(resultSet);
    }
    DBRProgressMonitor monitor = session.getProgressMonitor();
    monitor.subTask("Fetch result set");
    long rowCount = 0;

    dataReceiver.fetchStart(session, resultSet, rsOffset, rsMaxRows);

    try {
      // Retrieve source entity
      if (result != null) {
        DBCResultSetMetaData rsMeta = resultSet.getMeta();
        String sourceName = null; // resultSet.getResultSetName();
        for (DBCAttributeMetaData attr : rsMeta.getAttributes()) {
          String entityName = attr.getEntityName();
          if (!CommonUtils.isEmpty(entityName)) {
            if (sourceName == null) {
              sourceName = entityName;
            } else if (!sourceName.equals(entityName)) {
              // Multiple source entities
              sourceName += "(+)";
              break;
            }
          }
        }
        /*
                        if (CommonUtils.isEmpty(sourceName)) {
                            try {
                                sourceName = resultSet.getResultSetName();
                            } catch (DBCException e) {
                                log.debug(e);
                            }
                        }
        */
        if (CommonUtils.isEmpty(sourceName)) {
          sourceName = "Result";
        }
        result.setResultSetName(sourceName);
      }
      long fetchStartTime = System.currentTimeMillis();

      // Fetch all rows
      while ((!hasLimits() || rowCount < rsMaxRows) && resultSet.nextRow()) {
        if (monitor.isCanceled()) {
          break;
        }
        rowCount++;

        if (rowCount > 0 && rowCount % 100 == 0) {
          monitor.subTask(rowCount + " rows fetched");
          monitor.worked(100);
        }

        dataReceiver.fetchRow(session, resultSet);
      }
      if (updateStatistics) {
        statistics.addFetchTime(System.currentTimeMillis() - fetchStartTime);
      }
    } finally {
      if (!keepCursor) {
        try {
          resultSet.close();
        } catch (Throwable e) {
          log.error("Error while closing resultset", e);
        }
      }
      try {
        dataReceiver.fetchEnd(session, resultSet);
      } catch (Throwable e) {
        log.error("Error while handling end of result set fetch", e);
      }
      dataReceiver.close();
    }

    if (result != null) {
      result.setRowCount(rowCount);
    }
    if (updateStatistics) {
      statistics.setRowsFetched(rowCount);
    }
    monitor.subTask(rowCount + " rows fetched");

    return true;
  }
Exemplo n.º 5
0
  private boolean executeSingleQuery(
      @NotNull DBCSession session, @NotNull SQLQuery sqlQuery, boolean fireEvents) {
    lastError = null;

    final String originalQueryText = sqlQuery.getQuery();
    DBCExecutionContext executionContext = getExecutionContext();
    SQLQueryResult curResult = new SQLQueryResult(sqlQuery);
    if (rsOffset > 0) {
      curResult.setRowOffset(rsOffset);
    }
    SQLQuery originalQuery = sqlQuery;
    long startTime = System.currentTimeMillis();

    if (fireEvents && listener != null) {
      // Notify query start
      listener.onStartQuery(sqlQuery);
    }

    try {
      // Prepare statement
      closeStatement();

      // Check and invalidate connection
      DBPDataSource dataSource = executionContext.getDataSource();
      if (!connectionInvalidated
          && dataSource
              .getContainer()
              .getPreferenceStore()
              .getBoolean(DBeaverPreferences.STATEMENT_INVALIDATE_BEFORE_EXECUTE)) {
        executionContext.invalidateContext(session.getProgressMonitor());
        connectionInvalidated = true;
      }

      try {
        // Modify query (filters + parameters)
        if (dataFilter != null && dataFilter.hasFilters() && dataSource instanceof SQLDataSource) {
          String filteredQueryText =
              ((SQLDataSource) dataSource)
                  .getSQLDialect()
                  .addFiltersToQuery(dataSource, originalQueryText, dataFilter);
          sqlQuery = new SQLQuery(filteredQueryText, sqlQuery.getOffset(), sqlQuery.getLength());
        }
      } catch (DBException e) {
        throw new DBCException("Can't apply query filter", e);
      }

      Boolean hasParameters = prepareStatementParameters(sqlQuery);
      if (hasParameters == null) {
        return false;
      }

      statistics.setQueryText(originalQueryText);

      startTime = System.currentTimeMillis();
      DBCExecutionSource source =
          new AbstractExecutionSource(
              dataContainer, executionContext, partSite.getPart(), sqlQuery);
      curStatement =
          DBUtils.prepareStatement(
              source,
              session,
              hasParameters ? DBCStatementType.QUERY : DBCStatementType.SCRIPT,
              sqlQuery,
              rsOffset,
              rsMaxRows);

      if (hasParameters) {
        bindStatementParameters(session, sqlQuery);
      }

      // Execute statement
      try {
        boolean hasResultSet = curStatement.executeStatement();
        curResult.setHasResultSet(hasResultSet);
        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
        statistics.addStatementsCount();

        long updateCount = -1;
        while (hasResultSet || resultSetNumber == 0 || updateCount >= 0) {
          // Fetch data only if we have to fetch all results or if it is rs requested
          if (fetchResultSetNumber < 0 || fetchResultSetNumber == resultSetNumber) {
            if (hasResultSet && fetchResultSets) {
              DBDDataReceiver dataReceiver =
                  resultsConsumer.getDataReceiver(sqlQuery, resultSetNumber);
              if (dataReceiver != null) {
                hasResultSet =
                    fetchQueryData(
                        session, curStatement.openResultSet(), curResult, dataReceiver, true);
              }
            }
          }
          if (!hasResultSet) {
            try {
              updateCount = curStatement.getUpdateRowCount();
              if (updateCount >= 0) {
                curResult.setUpdateCount(updateCount);
                statistics.addRowsUpdated(updateCount);
              }
            } catch (DBCException e) {
              // In some cases we can't read update count
              // This is bad but we can live with it
              // Just print a warning
              log.warn("Can't obtain update count", e);
            }
          }
          if (hasResultSet && fetchResultSets) {
            resultSetNumber++;
            fetchResultSetNumber = resultSetNumber;
          }
          if (!hasResultSet && updateCount < 0) {
            // Nothing else to fetch
            break;
          }

          if (dataSource.getInfo().supportsMultipleResults()) {
            hasResultSet = curStatement.nextResults();
            updateCount = hasResultSet ? -1 : 0;
          } else {
            break;
          }
        }

        try {
          curResult.setWarnings(curStatement.getStatementWarnings());
        } catch (Throwable e) {
          log.warn("Can't read execution warnings", e);
        }
      } finally {
        // monitor.subTask("Close query");
        if (!keepStatementOpen()) {
          closeStatement();
        }

        // Release parameters
        releaseStatementParameters(sqlQuery);
      }
    } catch (Throwable ex) {
      if (!(ex instanceof DBException)) {
        log.error("Unexpected error while processing SQL", ex);
      }
      curResult.setError(ex);
      lastError = ex;
    } finally {
      curResult.setQueryTime(System.currentTimeMillis() - startTime);

      if (fireEvents && listener != null) {
        // Notify query end
        listener.onEndQuery(curResult);
      }
    }

    if (curResult.getError() != null && errorHandling != SQLScriptErrorHandling.IGNORE) {
      return false;
    }
    // Success
    lastGoodQuery = originalQuery;
    return true;
  }