Search in sources :

Example 66 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor 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 67 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor 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)

Example 68 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class DriverDependenciesTree method showVersionEditor.

private void showVersionEditor(final TreeItem item) {
    disposeOldEditor();
    final DBPDriverDependencies.DependencyNode dependencyNode = (DBPDriverDependencies.DependencyNode) item.getData();
    if (dependencyNode == null || dependencyNode.library == null || !dependencyNode.library.isDownloadable()) {
        return;
    }
    final List<String> allVersions = new ArrayList<>();
    try {
        runnableContext.run(true, true, new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    allVersions.addAll(dependencyNode.library.getAvailableVersions(monitor));
                } catch (IOException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(filesTree.getShell(), "Versions", "Error reading versions", e.getTargetException());
        return;
    } catch (InterruptedException e) {
        return;
    }
    final String currentVersion = dependencyNode.library.getVersion();
    if (currentVersion != null && !allVersions.contains(currentVersion)) {
        allVersions.add(currentVersion);
    }
    final Combo editor = new Combo(filesTree, SWT.DROP_DOWN | SWT.READ_ONLY);
    int versionIndex = -1;
    for (int i = 0; i < allVersions.size(); i++) {
        String version = allVersions.get(i);
        editor.add(version);
        if (version.equals(currentVersion)) {
            versionIndex = i;
        }
    }
    if (versionIndex >= 0) {
        editor.select(versionIndex);
    }
    editor.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String newVersion = editor.getItem(editor.getSelectionIndex());
            disposeOldEditor();
            setLibraryVersion(dependencyNode.library, newVersion);
        }
    });
    treeEditor.setEditor(editor, item, 1);
    editor.setListVisible(true);
}
Also used : DBPDriverDependencies(org.jkiss.dbeaver.model.connection.DBPDriverDependencies) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Point(org.eclipse.swt.graphics.Point) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 69 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class DialogUtils method saveToFile.

public static void saveToFile(IValueController controller) {
    if (!(controller.getValue() instanceof DBDContent)) {
        log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
        return;
    }
    Shell shell = UIUtils.getShell(controller.getValueSite());
    final File saveFile = selectFileForSave(shell, controller.getValueName());
    if (saveFile == null) {
        return;
    }
    final DBDContent value = (DBDContent) controller.getValue();
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    DBDContentStorage storage = value.getContents(monitor);
                    if (ContentUtils.isTextContent(value)) {
                        try (Reader cr = storage.getContentReader()) {
                            ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
                        }
                    } else {
                        try (InputStream cs = storage.getContentStream()) {
                            ContentUtils.saveContentToFile(cs, saveFile, monitor);
                        }
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(shell, CoreMessages.model_jdbc_could_not_save_content, //$NON-NLS-2$
        CoreMessages.model_jdbc_could_not_save_content_to_file_ + saveFile.getAbsolutePath() + "'", e.getTargetException());
    } catch (InterruptedException e) {
    // do nothing
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) InputStream(java.io.InputStream) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Reader(java.io.Reader) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 70 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class ERDResourceHandler method createDiagram.

public static IFile createDiagram(final EntityDiagram copyFrom, final String title, IFolder folder, DBRProgressMonitor monitor) throws DBException {
    if (folder == null) {
        try {
            folder = getDiagramsFolder(DBeaverCore.getInstance().getProjectRegistry().getActiveProject(), true);
        } catch (CoreException e) {
            throw new DBException("Can't obtain folder for diagram", e);
        }
    }
    if (folder == null) {
        throw new DBException("Can't detect folder for diagram");
    }
    final IFile file = ContentUtils.getUniqueFile(folder, CommonUtils.escapeFileName(title), ERD_EXT);
    try {
        DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    EntityDiagram newDiagram = copyFrom == null ? new EntityDiagram(null, "<Diagram>") : copyFrom.copy();
                    newDiagram.setName(title);
                    newDiagram.setLayoutManualAllowed(true);
                    newDiagram.setLayoutManualDesired(true);
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    DiagramLoader.save(monitor, null, newDiagram, false, buffer);
                    InputStream data = new ByteArrayInputStream(buffer.toByteArray());
                    file.create(data, true, RuntimeUtils.getNestedMonitor(monitor));
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        if (monitor == null) {
            DBeaverUI.runInProgressService(runnable);
        } else {
            runnable.run(monitor);
        }
    } catch (InvocationTargetException e) {
        throw new DBException("Error creating diagram", e.getTargetException());
    } catch (InterruptedException e) {
    // interrupted
    }
    return file;
}
Also used : DBException(org.jkiss.dbeaver.DBException) EntityDiagram(org.jkiss.dbeaver.ext.erd.model.EntityDiagram) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)79 DBException (org.jkiss.dbeaver.DBException)45 InvocationTargetException (java.lang.reflect.InvocationTargetException)36 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)30 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)14 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)12 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)12 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)10 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 ArrayList (java.util.ArrayList)8 IStatus (org.eclipse.core.runtime.IStatus)8 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)6 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)6 GridData (org.eclipse.swt.layout.GridData)6 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)6 List (java.util.List)5 NotNull (org.jkiss.code.NotNull)5 File (java.io.File)4 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)4