Search in sources :

Example 1 with DBDDataReceiverInteractive

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

the class SQLQueryJob method executeSingleQuery.

private boolean executeSingleQuery(@NotNull DBCSession session, @NotNull SQLScriptElement element, final boolean fireEvents) {
    if (element instanceof SQLControlCommand) {
        try {
            return scriptContext.executeControlCommand((SQLControlCommand) element);
        } catch (Throwable e) {
            if (!(e instanceof DBException)) {
                log.error("Unexpected error while processing SQL command", e);
            }
            lastError = e;
            return false;
        }
    }
    SQLQuery sqlQuery = (SQLQuery) element;
    lastError = null;
    if (!skipConfirmation && getDataSourceContainer().getConnectionConfiguration().getConnectionType().isConfirmExecute()) {
        // Validate all transactional queries
        if (!SQLSemanticProcessor.isSelectQuery(session.getDataSource().getSQLDialect(), element.getText())) {
            int confirmResult = confirmQueryExecution((SQLQuery) element, queries.size() > 1);
            switch(confirmResult) {
                case IDialogConstants.NO_ID:
                    return true;
                case IDialogConstants.YES_ID:
                    break;
                case IDialogConstants.YES_TO_ALL_ID:
                    skipConfirmation = true;
                    break;
                default:
                    return false;
            }
        }
    }
    final DBCExecutionContext executionContext = getExecutionContext();
    final DBPDataSource dataSource = executionContext.getDataSource();
    final SQLQuery originalQuery = sqlQuery;
    DBRProgressMonitor monitor = session.getProgressMonitor();
    monitor.beginTask("Get data receiver", 1);
    monitor.subTask("Create results view");
    DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(sqlQuery, resultSetNumber);
    try {
        if (dataReceiver instanceof DBDDataReceiverInteractive) {
            ((DBDDataReceiverInteractive) dataReceiver).setDataReceivePaused(true);
        }
        if (!scriptContext.fillQueryParameters((SQLQuery) element, CommonUtils.isBitSet(fetchFlags, DBSDataContainer.FLAG_REFRESH))) {
            // User canceled
            return false;
        }
    } finally {
        if (dataReceiver instanceof DBDDataReceiverInteractive) {
            ((DBDDataReceiverInteractive) dataReceiver).setDataReceivePaused(false);
        }
    }
    monitor.done();
    long startTime = System.currentTimeMillis();
    boolean startQueryAlerted = false;
    // Modify query (filters + parameters)
    // .trim();
    String queryText = originalQuery.getText();
    if (dataFilter != null && dataFilter.hasFilters()) {
        String filteredQueryText = dataSource.getSQLDialect().addFiltersToQuery(session.getProgressMonitor(), dataSource, queryText, dataFilter);
        sqlQuery = new SQLQuery(executionContext.getDataSource(), filteredQueryText, sqlQuery);
    } else {
        sqlQuery = new SQLQuery(executionContext.getDataSource(), queryText, sqlQuery);
    }
    curResult = new SQLQueryResult(sqlQuery);
    if (rsOffset > 0) {
        curResult.setRowOffset(rsOffset);
    }
    monitor.beginTask("Process query", 1);
    monitor.subTask("Initialize context");
    try {
        // Prepare statement
        closeStatement();
        // Check and invalidate connection
        if (!connectionInvalidated && dataSource.getContainer().getPreferenceStore().getBoolean(SQLPreferenceConstants.STATEMENT_INVALIDATE_BEFORE_EXECUTE)) {
            executionContext.invalidateContext(monitor, true);
            connectionInvalidated = true;
        }
        statistics.setQueryText(sqlQuery.getText());
        // Notify query start
        if (fireEvents && listener != null) {
            // Notify query start
            try {
                listener.onStartQuery(session, sqlQuery);
            } catch (Exception e) {
                log.error(e);
            }
            startQueryAlerted = true;
        }
        monitor.subTask("Execute query");
        startTime = System.currentTimeMillis();
        SQLQuery execStatement = sqlQuery;
        DBExecUtils.tryExecuteRecover(session, session.getDataSource(), param -> {
            try {
                // Toggle smart commit mode
                if (resultsConsumer instanceof ISmartTransactionManager && ((ISmartTransactionManager) resultsConsumer).isSmartAutoCommit()) {
                    DBExecUtils.checkSmartAutoCommit(session, execStatement.getText());
                }
                long execStartTime = System.currentTimeMillis();
                executeStatement(session, execStatement, execStartTime, curResult);
            } catch (Throwable e) {
                throw new InvocationTargetException(e);
            }
        });
    } 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) {
            notifyQueryExecutionEnd(curResult);
        }
        scriptContext.clearStatementContext();
        monitor.done();
    }
    if (curResult.getError() != null && errorHandling != SQLScriptErrorHandling.IGNORE) {
        return false;
    }
    // Success
    lastGoodQuery = originalQuery;
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataReceiver(org.jkiss.dbeaver.model.data.DBDDataReceiver) DBDDataReceiverInteractive(org.jkiss.dbeaver.model.data.DBDDataReceiverInteractive) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 2 with DBDDataReceiverInteractive

use of org.jkiss.dbeaver.model.data.DBDDataReceiverInteractive in project dbeaver by dbeaver.

the class SQLQueryJob method executeSingleQuery.

private boolean executeSingleQuery(@NotNull DBCSession session, @NotNull SQLScriptElement element, final boolean fireEvents) {
    if (element instanceof SQLControlCommand) {
        try {
            return scriptContext.executeControlCommand((SQLControlCommand) element);
        } catch (Throwable e) {
            if (!(e instanceof DBException)) {
                log.error("Unexpected error while processing SQL command", e);
            }
            lastError = e;
            return false;
        }
    }
    SQLQuery sqlQuery = (SQLQuery) element;
    lastError = null;
    if (!skipConfirmation && getDataSourceContainer().getConnectionConfiguration().getConnectionType().isConfirmExecute()) {
        // Validate all transactional queries
        if (!SQLSemanticProcessor.isSelectQuery(session.getDataSource().getSQLDialect(), element.getText())) {
            int confirmResult = confirmQueryExecution((SQLQuery) element, queries.size() > 1);
            switch(confirmResult) {
                case IDialogConstants.NO_ID:
                    return true;
                case IDialogConstants.YES_ID:
                    break;
                case IDialogConstants.YES_TO_ALL_ID:
                    skipConfirmation = true;
                    break;
                default:
                    return false;
            }
        }
    }
    final DBCExecutionContext executionContext = getExecutionContext();
    final DBPDataSource dataSource = executionContext.getDataSource();
    final SQLQuery originalQuery = sqlQuery;
    DBRProgressMonitor monitor = session.getProgressMonitor();
    monitor.beginTask("Get data receiver", 1);
    monitor.subTask("Create results view");
    DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(sqlQuery, resultSetNumber);
    try {
        if (dataReceiver instanceof DBDDataReceiverInteractive) {
            ((DBDDataReceiverInteractive) dataReceiver).setDataReceivePaused(true);
        }
        if (!scriptContext.fillQueryParameters((SQLQuery) element, CommonUtils.isBitSet(fetchFlags, DBSDataContainer.FLAG_REFRESH))) {
            // User canceled
            return false;
        }
    } finally {
        if (dataReceiver instanceof DBDDataReceiverInteractive) {
            ((DBDDataReceiverInteractive) dataReceiver).setDataReceivePaused(false);
        }
    }
    monitor.done();
    long startTime = System.currentTimeMillis();
    boolean startQueryAlerted = false;
    // Modify query (filters + parameters)
    // .trim();
    String queryText = originalQuery.getText();
    if (dataFilter != null && dataFilter.hasFilters()) {
        String filteredQueryText = dataSource.getSQLDialect().addFiltersToQuery(session.getProgressMonitor(), dataSource, queryText, dataFilter);
        sqlQuery = new SQLQuery(executionContext.getDataSource(), filteredQueryText, sqlQuery);
    } else {
        sqlQuery = new SQLQuery(executionContext.getDataSource(), queryText, sqlQuery);
    }
    curResult = new SQLQueryResult(sqlQuery);
    if (rsOffset > 0) {
        curResult.setRowOffset(rsOffset);
    }
    monitor.beginTask("Process query", 1);
    monitor.subTask("Initialize context");
    try {
        // Prepare statement
        closeStatement();
        // Check and invalidate connection
        if (!connectionInvalidated && dataSource.getContainer().getPreferenceStore().getBoolean(SQLPreferenceConstants.STATEMENT_INVALIDATE_BEFORE_EXECUTE)) {
            executionContext.invalidateContext(monitor, true);
            connectionInvalidated = true;
        }
        statistics.setQueryText(sqlQuery.getText());
        // Notify query start
        if (fireEvents && listener != null) {
            // Notify query start
            try {
                listener.onStartQuery(session, sqlQuery);
            } catch (Exception e) {
                log.error(e);
            }
            startQueryAlerted = true;
        }
        monitor.subTask("Execute query");
        startTime = System.currentTimeMillis();
        SQLQuery execStatement = sqlQuery;
        DBExecUtils.tryExecuteRecover(session, session.getDataSource(), param -> {
            try {
                // Toggle smart commit mode
                if (resultsConsumer instanceof ISmartTransactionManager && ((ISmartTransactionManager) resultsConsumer).isSmartAutoCommit()) {
                    DBExecUtils.checkSmartAutoCommit(session, execStatement.getText());
                }
                long execStartTime = System.currentTimeMillis();
                executeStatement(session, execStatement, execStartTime, curResult);
            } catch (Throwable e) {
                throw new InvocationTargetException(e);
            }
        });
    } 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) {
            notifyQueryExecutionEnd(curResult);
        }
        scriptContext.clearStatementContext();
        monitor.done();
    }
    if (curResult.getError() != null && errorHandling != SQLScriptErrorHandling.IGNORE) {
        return false;
    }
    // Success
    lastGoodQuery = originalQuery;
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataReceiver(org.jkiss.dbeaver.model.data.DBDDataReceiver) DBDDataReceiverInteractive(org.jkiss.dbeaver.model.data.DBDDataReceiverInteractive) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 DBException (org.jkiss.dbeaver.DBException)2 DBDDataReceiver (org.jkiss.dbeaver.model.data.DBDDataReceiver)2 DBDDataReceiverInteractive (org.jkiss.dbeaver.model.data.DBDDataReceiverInteractive)2 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)2