Search in sources :

Example 1 with SQLQueryResult

use of org.jkiss.dbeaver.model.sql.SQLQueryResult 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 SQLQueryResult

use of org.jkiss.dbeaver.model.sql.SQLQueryResult in project dbeaver by serge-rider.

the class SQLQueryJob method fetchExecutionResult.

private void fetchExecutionResult(@NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @NotNull SQLQuery query) throws DBCException {
    // Fetch fake result set
    StatResultSet fakeResultSet = new StatResultSet(session, curStatement);
    SQLQueryResult resultInfo = new SQLQueryResult(query);
    if (statistics.getStatementsCount() > 1) {
        // Multiple statements - show script statistics
        fakeResultSet.addColumn("Queries", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Updated Rows", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Execute time", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Fetch time", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Total time", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Finish time", DBPDataKind.DATETIME);
        fakeResultSet.addRow(statistics.getStatementsCount(), statistics.getRowsUpdated(), statistics.getExecuteTime(), statistics.getFetchTime(), statistics.getTotalTime(), new Date());
        resultInfo.setResultSetName("Statistics");
    } else {
        // Single statement
        long updateCount = statistics.getRowsUpdated();
        if (updateCount >= 0) {
            fakeResultSet.addColumn("Query", DBPDataKind.STRING);
            fakeResultSet.addColumn("Updated Rows", DBPDataKind.NUMERIC);
            fakeResultSet.addColumn("Finish time", DBPDataKind.DATETIME);
            fakeResultSet.addRow(query.getQuery(), updateCount, new Date());
        } else {
            fakeResultSet.addColumn("Result", DBPDataKind.NUMERIC);
        }
        resultInfo.setResultSetName("Result");
    }
    fetchQueryData(session, fakeResultSet, resultInfo, dataReceiver, false);
}
Also used : SQLQueryResult(org.jkiss.dbeaver.model.sql.SQLQueryResult) StatResultSet(org.jkiss.dbeaver.model.impl.local.StatResultSet) Date(java.util.Date)

Aggregations

SQLQueryResult (org.jkiss.dbeaver.model.sql.SQLQueryResult)2 Date (java.util.Date)1 DBException (org.jkiss.dbeaver.DBException)1 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)1 DBDDataReceiver (org.jkiss.dbeaver.model.data.DBDDataReceiver)1 AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)1 StatResultSet (org.jkiss.dbeaver.model.impl.local.StatResultSet)1 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)1 SQLQuery (org.jkiss.dbeaver.model.sql.SQLQuery)1