Search in sources :

Example 1 with DBDDataReceiver

use of org.jkiss.dbeaver.model.data.DBDDataReceiver in project dbeaver by serge-rider.

the class SQLQueryJob method executeSingleQuery.

private boolean executeSingleQuery(@NotNull DBCSession session, @NotNull SQLQuery sqlQuery, final boolean fireEvents) {
    lastError = null;
    final DBCExecutionContext executionContext = getExecutionContext();
    final DBPDataSource dataSource = executionContext.getDataSource();
    final SQLQuery originalQuery = sqlQuery;
    long startTime = System.currentTimeMillis();
    boolean startQueryAlerted = false;
    if (!prepareStatementParameters(sqlQuery)) {
        return false;
    }
    // Modify query (filters + parameters)
    if (dataFilter != null && dataFilter.hasFilters() && dataSource instanceof SQLDataSource) {
        String filteredQueryText = ((SQLDataSource) dataSource).getSQLDialect().addFiltersToQuery(dataSource, originalQuery.getQuery(), dataFilter);
        sqlQuery = new SQLQuery(executionContext.getDataSource(), filteredQueryText, sqlQuery);
    }
    final SQLQueryResult curResult = new SQLQueryResult(sqlQuery);
    if (rsOffset > 0) {
        curResult.setRowOffset(rsOffset);
    }
    try {
        // Prepare statement
        closeStatement();
        // Check and invalidate connection
        if (!connectionInvalidated && dataSource.getContainer().getPreferenceStore().getBoolean(DBeaverPreferences.STATEMENT_INVALIDATE_BEFORE_EXECUTE)) {
            executionContext.invalidateContext(session.getProgressMonitor());
            connectionInvalidated = true;
        }
        statistics.setQueryText(originalQuery.getQuery());
        // Notify query start
        if (fireEvents && listener != null) {
            // Notify query start
            try {
                listener.onStartQuery(session, sqlQuery);
            } catch (Exception e) {
                log.error(e);
            }
            startQueryAlerted = true;
        }
        startTime = System.currentTimeMillis();
        DBCExecutionSource source = new AbstractExecutionSource(dataContainer, executionContext, partSite.getPart(), sqlQuery);
        final DBCStatement dbcStatement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, rsOffset, rsMaxRows);
        curStatement = dbcStatement;
        int statementTimeout = getDataSourceContainer().getPreferenceStore().getInt(DBeaverPreferences.STATEMENT_TIMEOUT);
        if (statementTimeout > 0) {
            try {
                dbcStatement.setStatementTimeout(statementTimeout);
            } catch (Throwable e) {
                log.debug("Can't set statement timeout:" + e.getMessage());
            }
        }
        // Execute statement
        try {
            boolean hasResultSet = dbcStatement.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, dbcStatement.openResultSet(), curResult, dataReceiver, true);
                        }
                    }
                }
                if (!hasResultSet) {
                    try {
                        updateCount = dbcStatement.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 = dbcStatement.nextResults();
                    updateCount = hasResultSet ? -1 : 0;
                } else {
                    break;
                }
            }
            try {
                curResult.setWarnings(dbcStatement.getStatementWarnings());
            } catch (Throwable e) {
                log.warn("Can't read execution warnings", e);
            }
        } finally {
            //monitor.subTask("Close query");
            if (!keepStatementOpen()) {
                closeStatement();
            }
        }
    } 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 && startQueryAlerted) {
            // Notify query end
            try {
                listener.onEndQuery(session, curResult);
            } catch (Exception e) {
                log.error(e);
            }
        }
    }
    if (curResult.getError() != null && errorHandling != SQLScriptErrorHandling.IGNORE) {
        return false;
    }
    // Success
    lastGoodQuery = originalQuery;
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLQueryResult(org.jkiss.dbeaver.model.sql.SQLQueryResult) DBDDataReceiver(org.jkiss.dbeaver.model.data.DBDDataReceiver) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) DBException(org.jkiss.dbeaver.DBException) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource)

Example 2 with DBDDataReceiver

use of org.jkiss.dbeaver.model.data.DBDDataReceiver in project dbeaver by serge-rider.

the class SQLQueryJob method showExecutionResult.

private void showExecutionResult(DBCSession session) {
    if (statistics.getStatementsCount() > 1 || resultSetNumber == 0) {
        SQLQuery query = new SQLQuery(session.getDataSource(), "", -1, -1);
        if (queries.size() == 1) {
            query.setQuery(queries.get(0).getQuery());
        }
        // It will set tab name to "Stats"
        query.setData(STATS_RESULTS);
        DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(query, resultSetNumber);
        if (dataReceiver != null) {
            try {
                fetchExecutionResult(session, dataReceiver, query);
            } catch (DBCException e) {
                log.error("Error generating execution result stats", e);
            }
        }
    }
}
Also used : DBDDataReceiver(org.jkiss.dbeaver.model.data.DBDDataReceiver) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery)

Aggregations

DBDDataReceiver (org.jkiss.dbeaver.model.data.DBDDataReceiver)2 SQLQuery (org.jkiss.dbeaver.model.sql.SQLQuery)2 DBException (org.jkiss.dbeaver.DBException)1 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)1 AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)1 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)1 SQLQueryResult (org.jkiss.dbeaver.model.sql.SQLQueryResult)1