Search in sources :

Example 1 with DataSourceJob

use of org.jkiss.dbeaver.runtime.jobs.DataSourceJob in project dbeaver by serge-rider.

the class ExasolBaseTableToolDialog method executeSQL.

@Override
protected void executeSQL() {
    final String jobName = getShell().getText();
    final SQLScriptProgressListener<ExasolTableBase> scriptListener = getScriptListener();
    final List<ExasolTableBase> objects = getCheckedObjects();
    final Map<ExasolTableBase, List<String>> objectsSQL = new LinkedHashMap<>();
    for (ExasolTableBase object : objects) {
        final List<String> lines = new ArrayList<>();
        generateObjectCommand(lines, object);
        objectsSQL.put(object, lines);
    }
    final DataSourceJob job = new DataSourceJob(jobName, null, getExecutionContext()) {

        public Exception objectProcessingError;

        @SuppressWarnings("rawtypes")
        @Override
        protected IStatus run(final DBRProgressMonitor monitor) {
            final DataSourceJob curJob = this;
            DBeaverUI.asyncExec(new Runnable() {

                @Override
                public void run() {
                    scriptListener.beginScriptProcessing(curJob, objects);
                }
            });
            monitor.beginTask(jobName, objects.size());
            try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
                for (int i = 0; i < objects.size(); i++) {
                    if (monitor.isCanceled()) {
                        break;
                    }
                    final int objectNumber = i;
                    final ExasolTableBase object = objects.get(i);
                    monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
                    objectProcessingError = null;
                    DBeaverUI.asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            scriptListener.beginObjectProcessing(object, objectNumber);
                        }
                    });
                    try {
                        final List<String> lines = objectsSQL.get(object);
                        for (String line : lines) {
                            try (final Statement statement = ((JDBCSession) session).getOriginal().createStatement()) {
                                int affectedRows = statement.executeUpdate(line);
                                Integer[] resultSetData = new Integer[] { affectedRows };
                                final LocalResultSet resultSet = new LocalResultSet<>(session, new JDBCStatementImpl<>((JDBCSession) session, statement, true));
                                resultSet.addColumn("ROWS_AFFECTED", DBPDataKind.NUMERIC);
                                resultSet.addRow((Object[]) resultSetData);
                                // Run in sync because we need result set
                                DBeaverUI.syncExec(new Runnable() {

                                    @Override
                                    public void run() {
                                        try {
                                            scriptListener.processObjectResults(object, null, resultSet);
                                        } catch (DBCException e) {
                                            objectProcessingError = e;
                                        }
                                    }
                                });
                                if (objectProcessingError != null) {
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        objectProcessingError = e;
                    } finally {
                        DBeaverUI.asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                scriptListener.endObjectProcessing(object, objectProcessingError);
                            }
                        });
                    }
                    monitor.worked(1);
                }
            } finally {
                monitor.done();
                DBeaverUI.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        scriptListener.endScriptProcessing();
                    }
                });
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(false);
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
        }
    });
    job.schedule();
}
Also used : LocalResultSet(org.jkiss.dbeaver.model.impl.local.LocalResultSet) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) Statement(java.sql.Statement) ExasolTableBase(org.jkiss.dbeaver.ext.exasol.model.ExasolTableBase) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 2 with DataSourceJob

use of org.jkiss.dbeaver.runtime.jobs.DataSourceJob in project dbeaver by serge-rider.

the class GenerateMultiSQLDialog method executeSQL.

@Override
protected void executeSQL() {
    final String jobName = getShell().getText();
    final SQLScriptProgressListener<T> scriptListener = getScriptListener();
    final List<T> objects = getCheckedObjects();
    final Map<T, List<String>> objectsSQL = new LinkedHashMap<>();
    for (T object : objects) {
        final List<String> lines = new ArrayList<>();
        generateObjectCommand(lines, object);
        objectsSQL.put(object, lines);
    }
    final DataSourceJob job = new DataSourceJob(jobName, null, getExecutionContext()) {

        public Exception objectProcessingError;

        @Override
        protected IStatus run(final DBRProgressMonitor monitor) {
            final DataSourceJob curJob = this;
            DBeaverUI.asyncExec(new Runnable() {

                @Override
                public void run() {
                    scriptListener.beginScriptProcessing(curJob, objects);
                }
            });
            monitor.beginTask(jobName, objects.size());
            try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
                for (int i = 0; i < objects.size(); i++) {
                    if (monitor.isCanceled()) {
                        break;
                    }
                    final int objectNumber = i;
                    final T object = objects.get(i);
                    monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
                    objectProcessingError = null;
                    DBeaverUI.asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            scriptListener.beginObjectProcessing(object, objectNumber);
                        }
                    });
                    try {
                        final List<String> lines = objectsSQL.get(object);
                        for (String line : lines) {
                            try (final DBCStatement statement = DBUtils.makeStatement(session, line, false)) {
                                if (statement.executeStatement()) {
                                    try (DBCResultSet resultSet = statement.openResultSet()) {
                                        // Run in sync because we need result set
                                        DBeaverUI.syncExec(new Runnable() {

                                            @Override
                                            public void run() {
                                                try {
                                                    scriptListener.processObjectResults(object, statement, resultSet);
                                                } catch (DBCException e) {
                                                    objectProcessingError = e;
                                                }
                                            }
                                        });
                                    }
                                    if (objectProcessingError != null) {
                                        break;
                                    }
                                } else {
                                    DBeaverUI.syncExec(new Runnable() {

                                        @Override
                                        public void run() {
                                            try {
                                                scriptListener.processObjectResults(object, statement, null);
                                            } catch (DBCException e) {
                                                objectProcessingError = e;
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    } catch (Exception e) {
                        objectProcessingError = e;
                    } finally {
                        DBeaverUI.asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                scriptListener.endObjectProcessing(object, objectProcessingError);
                            }
                        });
                    }
                    monitor.worked(1);
                }
            } finally {
                monitor.done();
                DBeaverUI.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        scriptListener.endScriptProcessing();
                    }
                });
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(false);
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
        }
    });
    job.schedule();
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) SWT(org.eclipse.swt.SWT) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 3 with DataSourceJob

use of org.jkiss.dbeaver.runtime.jobs.DataSourceJob in project dbeaver by serge-rider.

the class GenerateSQLDialog method executeSQL.

protected void executeSQL() {
    final String jobName = getShell().getText();
    final String[] scriptLines = generateSQLScript();
    DataSourceJob job = new DataSourceJob(jobName, null, executionContext) {

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
                for (String line : scriptLines) {
                    DBCStatement statement = DBUtils.makeStatement(session, line, false);
                    try {
                        statement.executeStatement();
                    } finally {
                        statement.close();
                    }
                }
            } catch (DBCException e) {
                return GeneralUtils.makeExceptionStatus(e);
            }
            return Status.OK_STATUS;
        }
    };
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            if (event.getResult().isOK()) {
                if (onSuccess != null) {
                    onSuccess.run();
                }
            }
        }
    });
    job.schedule();
}
Also used : DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Aggregations

IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)3 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3 DataSourceJob (org.jkiss.dbeaver.runtime.jobs.DataSourceJob)3 Statement (java.sql.Statement)1 SWT (org.eclipse.swt.SWT)1 ExasolTableBase (org.jkiss.dbeaver.ext.exasol.model.ExasolTableBase)1 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)1 LocalResultSet (org.jkiss.dbeaver.model.impl.local.LocalResultSet)1