Search in sources :

Example 76 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class DBGJDBCSession method runAsync.

/**
 * Start thread for SQL command
 */
protected void runAsync(String commandSQL, String name, DBGEvent begin, DBGEvent end) throws DBGException {
    workerJob = new DBGJDBCWorker(this, name, commandSQL, begin, end);
    workerJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            workerJob = null;
        }
    });
    workerJob.schedule();
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 77 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class ClientHomesSelector method populateHomes.

public void populateHomes(DBPDriver driver, String currentHome, boolean selectDefault) {
    this.driver = driver;
    this.currentHomeId = currentHome;
    this.homesCombo.removeAll();
    this.homeIds.clear();
    Map<String, DBPNativeClientLocation> homes = new LinkedHashMap<>();
    AbstractJob hlJob = new AbstractJob("Find native client homes") {

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            for (DBPNativeClientLocation ncl : driver.getNativeClientLocations()) {
                homes.put(ncl.getName(), ncl);
            }
            DBPNativeClientLocationManager clientManager = driver.getNativeClientManager();
            if (clientManager != null) {
                for (DBPNativeClientLocation location : clientManager.findLocalClientLocations()) {
                    homes.putIfAbsent(location.getName(), location);
                }
            }
            return Status.OK_STATUS;
        }
    };
    hlJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            UIUtils.syncExec(() -> {
                for (DBPNativeClientLocation location : homes.values()) {
                    homesCombo.add(location.getDisplayName());
                    homeIds.add(location.getName());
                    if (currentHomeId != null && location.getName().equals(currentHomeId)) {
                        homesCombo.select(homesCombo.getItemCount() - 1);
                    }
                }
                if (homesCombo.getItemCount() == 0) {
                    homesCombo.add(UIConnectionMessages.controls_client_home_selector_missing);
                    homeIds.add(null);
                }
                if (selectDefault && homesCombo.getSelectionIndex() == -1) {
                    homesCombo.select(0);
                    currentHomeId = homeIds.get(0);
                }
                homesCombo.add(UIConnectionMessages.controls_client_home_selector_browse);
                displayClientVersion();
                homesCombo.setEnabled(true);
            });
            super.done(event);
        }
    });
    hlJob.schedule();
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBPNativeClientLocation(org.jkiss.dbeaver.model.connection.DBPNativeClientLocation) DBPNativeClientLocationManager(org.jkiss.dbeaver.model.connection.DBPNativeClientLocationManager) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 78 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

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, getExecutionContext()) {

        Exception objectProcessingError;

        @Override
        protected IStatus run(final DBRProgressMonitor monitor) {
            final DataSourceJob curJob = this;
            UIUtils.asyncExec(() -> scriptListener.beginScriptProcessing(curJob, objects));
            monitor.beginTask(jobName, objects.size());
            try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
                if (isRunInSeparateTransaction()) {
                    commitChanges(session);
                }
                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;
                    UIUtils.asyncExec(() -> 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
                                        UIUtils.syncExec(() -> {
                                            try {
                                                scriptListener.processObjectResults(object, statement, resultSet);
                                            } catch (DBCException e) {
                                                objectProcessingError = e;
                                            }
                                        });
                                    }
                                    if (objectProcessingError != null) {
                                        break;
                                    }
                                } else {
                                    UIUtils.syncExec(() -> {
                                        try {
                                            scriptListener.processObjectResults(object, statement, null);
                                        } catch (DBCException e) {
                                            objectProcessingError = e;
                                        }
                                    });
                                }
                            }
                        }
                    } catch (Exception e) {
                        objectProcessingError = e;
                    } finally {
                        UIUtils.asyncExec(() -> scriptListener.endObjectProcessing(object, objectProcessingError));
                    }
                    monitor.worked(1);
                }
                if (isRunInSeparateTransaction()) {
                    commitChanges(session);
                }
            } finally {
                monitor.done();
                UIUtils.asyncExec(scriptListener::endScriptProcessing);
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(false);
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            if (needsRefreshOnFinish()) {
                List<T> objectToRefresh = new ArrayList<>(selectedObjects);
                UIUtils.asyncExec(() -> {
                    try {
                        UIUtils.runInProgressDialog(monitor -> {
                            monitor.beginTask("Refresh objects", objectToRefresh.size());
                            for (T object : objectToRefresh) {
                                try {
                                    DBNDatabaseNode objectNode = DBNUtils.getNodeByObject(object);
                                    if (objectNode != null) {
                                        objectNode.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
                                    }
                                } catch (Exception e) {
                                    log.error("Error refreshing object '" + object.getName() + "'", e);
                                }
                            }
                            monitor.done();
                        });
                    } catch (InvocationTargetException e) {
                        DBWorkbench.getPlatformUI().showError("Objects refresh", "Error refreshing navigator objects", e);
                    }
                });
            }
        }
    });
    job.schedule();
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) java.util(java.util) IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) Table(org.eclipse.swt.widgets.Table) NotNull(org.jkiss.code.NotNull) IStatus(org.eclipse.core.runtime.IStatus) DBNUtils(org.jkiss.dbeaver.model.navigator.DBNUtils) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) Composite(org.eclipse.swt.widgets.Composite) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) GridData(org.eclipse.swt.layout.GridData) DBNEvent(org.jkiss.dbeaver.model.navigator.DBNEvent) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) TableItem(org.eclipse.swt.widgets.TableItem) DBUtils(org.jkiss.dbeaver.model.DBUtils) org.jkiss.dbeaver.model.exec(org.jkiss.dbeaver.model.exec) Status(org.eclipse.core.runtime.Status) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBIcon(org.jkiss.dbeaver.model.DBIcon) SWT(org.eclipse.swt.SWT) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) SWT(org.eclipse.swt.SWT) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSourceJob(org.jkiss.dbeaver.runtime.jobs.DataSourceJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 79 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

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, getExecutionContext()) {

        Exception objectProcessingError;

        @SuppressWarnings("rawtypes")
        @Override
        protected IStatus run(final DBRProgressMonitor monitor) {
            final DataSourceJob curJob = this;
            UIUtils.asyncExec(() -> 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;
                    UIUtils.asyncExec(() -> 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
                                UIUtils.syncExec(() -> {
                                    try {
                                        scriptListener.processObjectResults(object, null, resultSet);
                                    } catch (DBCException e) {
                                        objectProcessingError = e;
                                    }
                                });
                                if (objectProcessingError != null) {
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        objectProcessingError = e;
                    } finally {
                        UIUtils.asyncExec(() -> scriptListener.endObjectProcessing(object, objectProcessingError));
                    }
                    monitor.worked(1);
                }
            } finally {
                monitor.done();
                UIUtils.asyncExec(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 80 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.

the class SQLEditorHandlerOpenObjectConsole method openConsole.

void openConsole(IWorkbenchWindow workbenchWindow, DBRRunnableWithResult<String> generator, SQLNavigatorContext navigatorContext, String title, boolean doRun, ISelection currentSelection) throws Exception {
    UIUtils.runInUI(workbenchWindow, generator);
    String sql = CommonUtils.notEmpty(generator.getResult());
    DBPProject project = navigatorContext.getProject();
    SQLEditorHandlerOpenEditor.checkProjectIsOpen(project);
    IFolder folder = SQLEditorHandlerOpenEditor.getCurrentScriptFolder(currentSelection);
    IFile scriptFile = SQLEditorUtils.createNewScript(project, folder, navigatorContext);
    FileEditorInput sqlInput = new FileEditorInput(scriptFile);
    SQLEditor editor = (SQLEditor) workbenchWindow.getActivePage().openEditor(sqlInput, SQLEditor.class.getName());
    if (editor != null) {
        editor.getDocument().set(sql);
        AbstractJob execJob = new AbstractJob("Execute SQL in console") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                // So let's give it a chance and wait for 10 seconds
                for (int i = 0; i < 100; i++) {
                    if (editor.getExecutionContext() != null) {
                        break;
                    }
                    RuntimeUtils.pause(100);
                }
                return Status.OK_STATUS;
            }
        };
        if (doRun) {
            execJob.addJobChangeListener(new JobChangeAdapter() {

                @Override
                public void done(IJobChangeEvent event) {
                    UIUtils.syncExec(() -> editor.processSQL(false, false));
                }
            });
        }
        execJob.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) IFile(org.eclipse.core.resources.IFile) FileEditorInput(org.eclipse.ui.part.FileEditorInput) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)172 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)163 Job (org.eclipse.core.runtime.jobs.Job)46 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)45 IStatus (org.eclipse.core.runtime.IStatus)36 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)30 ArrayList (java.util.ArrayList)13 IFile (org.eclipse.core.resources.IFile)13 File (java.io.File)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 CoreException (org.eclipse.core.runtime.CoreException)12 DataSourceJob (org.jkiss.dbeaver.runtime.jobs.DataSourceJob)12 Status (org.eclipse.core.runtime.Status)11 IJobChangeListener (org.eclipse.core.runtime.jobs.IJobChangeListener)11 IOException (java.io.IOException)10 DBException (org.jkiss.dbeaver.DBException)10 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)10 IProject (org.eclipse.core.resources.IProject)9 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 Composite (org.eclipse.swt.widgets.Composite)7