Search in sources :

Example 1 with ExecutionQueueErrorResponse

use of org.jkiss.dbeaver.ui.dialogs.exec.ExecutionQueueErrorResponse in project dbeaver by serge-rider.

the class SQLQueryJob method run.

@Override
protected IStatus run(DBRProgressMonitor monitor) {
    RuntimeUtils.setThreadName("SQL script execution");
    statistics = new DBCStatistics();
    try {
        DBCExecutionContext context = getExecutionContext();
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
        DBCExecutionPurpose purpose = queries.size() > 1 ? DBCExecutionPurpose.USER_SCRIPT : DBCExecutionPurpose.USER;
        try (DBCSession session = context.openSession(monitor, purpose, "SQL Query")) {
            // Set transaction settings (only if autocommit is off)
            QMUtils.getDefaultHandler().handleScriptBegin(session);
            boolean oldAutoCommit = txnManager == null || txnManager.isAutoCommit();
            boolean newAutoCommit = (commitType == SQLScriptCommitType.AUTOCOMMIT);
            if (txnManager != null && !oldAutoCommit && newAutoCommit) {
                txnManager.setAutoCommit(monitor, true);
            }
            monitor.beginTask(this.getName(), queries.size());
            // Notify job start
            if (listener != null) {
                try {
                    listener.onStartScript();
                } catch (Exception e) {
                    log.error(e);
                }
            }
            resultSetNumber = 0;
            for (int queryNum = 0; queryNum < queries.size(); ) {
                // Execute query
                SQLQuery query = queries.get(queryNum);
                fetchResultSetNumber = resultSetNumber;
                boolean runNext = executeSingleQuery(session, query, true);
                if (!runNext) {
                    // Ask to continue
                    if (lastError != null) {
                        log.error(lastError);
                    }
                    boolean isQueue = queryNum < queries.size() - 1;
                    ExecutionQueueErrorResponse response = ExecutionQueueErrorJob.showError(isQueue ? "SQL script execution" : "SQL query execution", lastError, isQueue);
                    boolean stopScript = false;
                    switch(response) {
                        case STOP:
                            // just stop execution
                            stopScript = true;
                            break;
                        case RETRY:
                            // just make it again
                            continue;
                        case IGNORE:
                            // Just do nothing
                            break;
                        case IGNORE_ALL:
                            errorHandling = SQLScriptErrorHandling.IGNORE;
                            break;
                    }
                    if (stopScript) {
                        break;
                    }
                }
                // Check monitor
                if (monitor.isCanceled()) {
                    break;
                }
                monitor.worked(1);
                queryNum++;
            }
            showExecutionResult(session);
            monitor.done();
            // Commit data
            if (txnManager != null && !oldAutoCommit && commitType != SQLScriptCommitType.AUTOCOMMIT) {
                if (lastError == null || errorHandling == SQLScriptErrorHandling.STOP_COMMIT) {
                    if (commitType != SQLScriptCommitType.NO_COMMIT) {
                        monitor.beginTask("Commit data", 1);
                        txnManager.commit(session);
                        monitor.done();
                    }
                } else {
                    monitor.beginTask("Rollback data", 1);
                    txnManager.rollback(session, null);
                    monitor.done();
                }
            }
            // Restore transactions settings
            if (txnManager != null && !oldAutoCommit && newAutoCommit) {
                txnManager.setAutoCommit(monitor, false);
            }
            QMUtils.getDefaultHandler().handleScriptEnd(session);
            // Return success
            return new Status(Status.OK, DBeaverCore.getCorePluginID(), "SQL job completed");
        }
    } catch (Throwable ex) {
        return new Status(Status.ERROR, DBeaverCore.getCorePluginID(), "Error during SQL job execution: " + ex.getMessage());
    } finally {
        // Notify job end
        if (listener != null) {
            try {
                listener.onEndScript(statistics, lastError != null);
            } catch (Exception e) {
                log.error(e);
            }
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ExecutionQueueErrorResponse(org.jkiss.dbeaver.ui.dialogs.exec.ExecutionQueueErrorResponse) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) DBException(org.jkiss.dbeaver.DBException)

Example 2 with ExecutionQueueErrorResponse

use of org.jkiss.dbeaver.ui.dialogs.exec.ExecutionQueueErrorResponse in project dbeaver by dbeaver.

the class SQLQueryJob method run.

@Override
protected IStatus run(DBRProgressMonitor monitor) {
    RuntimeUtils.setThreadName("SQL script execution");
    statistics = new DBCStatistics();
    skipConfirmation = false;
    try {
        DBCExecutionContext context = getExecutionContext();
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
        DBCExecutionPurpose purpose = queries.size() > 1 ? DBCExecutionPurpose.USER_SCRIPT : DBCExecutionPurpose.USER;
        try (DBCSession session = context.openSession(monitor, purpose, "SQL Query")) {
            // Set transaction settings (only if autocommit is off)
            QMUtils.getDefaultHandler().handleScriptBegin(session);
            boolean oldAutoCommit = txnManager == null || txnManager.isAutoCommit();
            boolean newAutoCommit = (commitType == SQLScriptCommitType.AUTOCOMMIT);
            if (txnManager != null && !oldAutoCommit && newAutoCommit) {
                txnManager.setAutoCommit(monitor, true);
            }
            monitor.beginTask(this.getName(), queries.size());
            // Notify job start
            if (listener != null) {
                try {
                    listener.onStartScript();
                } catch (Exception e) {
                    log.error(e);
                }
            }
            resultSetNumber = 0;
            for (int queryNum = 0; queryNum < queries.size(); ) {
                // Execute query
                SQLScriptElement query = queries.get(queryNum);
                fetchResultSetNumber = resultSetNumber;
                boolean runNext = executeSingleQuery(session, query, true);
                if (!runNext) {
                    if (lastError == null) {
                        // Execution cancel
                        break;
                    }
                    // Ask to continue
                    log.error(lastError);
                    boolean isQueue = queryNum < queries.size() - 1;
                    ExecutionQueueErrorResponse response = ExecutionQueueErrorJob.showError(isQueue ? "SQL script execution" : "SQL query execution", lastError, isQueue);
                    boolean stopScript = false;
                    switch(response) {
                        case STOP:
                            // just stop execution
                            stopScript = true;
                            break;
                        case RETRY:
                            // just make it again
                            continue;
                        case IGNORE:
                            // Just do nothing
                            break;
                        case IGNORE_ALL:
                            errorHandling = SQLScriptErrorHandling.IGNORE;
                            break;
                    }
                    if (stopScript) {
                        break;
                    }
                }
                // Check monitor
                if (monitor.isCanceled()) {
                    break;
                }
                monitor.worked(1);
                queryNum++;
            }
            if (statistics.getStatementsCount() > 0) {
                showExecutionResult(session);
            }
            monitor.done();
            // Commit data
            if (txnManager != null && !oldAutoCommit && commitType != SQLScriptCommitType.AUTOCOMMIT) {
                if (lastError == null || errorHandling == SQLScriptErrorHandling.STOP_COMMIT) {
                    if (commitType != SQLScriptCommitType.NO_COMMIT) {
                        monitor.beginTask("Commit data", 1);
                        txnManager.commit(session);
                        monitor.done();
                    }
                } else if (errorHandling == SQLScriptErrorHandling.STOP_ROLLBACK) {
                    monitor.beginTask("Rollback data", 1);
                    txnManager.rollback(session, null);
                    monitor.done();
                } else {
                    // Just ignore error
                    log.info("Script executed with errors. Changes were not commmitted.");
                }
            }
            // Restore transactions settings
            if (txnManager != null && !oldAutoCommit && newAutoCommit) {
                txnManager.setAutoCommit(monitor, false);
            }
            QMUtils.getDefaultHandler().handleScriptEnd(session);
            // Return success
            return new Status(Status.OK, DBeaverCore.getCorePluginID(), "SQL job completed");
        }
    } catch (Throwable ex) {
        return new Status(Status.ERROR, DBeaverCore.getCorePluginID(), "Error during SQL job execution: " + ex.getMessage());
    } finally {
        // Notify job end
        if (listener != null) {
            try {
                listener.onEndScript(statistics, lastError != null);
            } catch (Exception e) {
                log.error(e);
            }
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ExecutionQueueErrorResponse(org.jkiss.dbeaver.ui.dialogs.exec.ExecutionQueueErrorResponse) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException)

Aggregations

IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 DBException (org.jkiss.dbeaver.DBException)2 ExecutionQueueErrorResponse (org.jkiss.dbeaver.ui.dialogs.exec.ExecutionQueueErrorResponse)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLQuery (org.jkiss.dbeaver.model.sql.SQLQuery)1