Search in sources :

Example 71 with DBRProgressMonitor

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

the class ERDEditorEmbedded method loadDiagram.

@Override
protected synchronized void loadDiagram(final boolean refreshMetadata) {
    final DBSObject object = getRootObject();
    if (object == null) {
        return;
    }
    if (diagramLoadingJob != null) {
        // Do not start new one while old is running
        return;
    }
    diagramLoadingJob = LoadingJob.createService(new DatabaseLoadService<EntityDiagram>("Load diagram '" + object.getName() + "'", object.getDataSource()) {

        @Override
        public EntityDiagram evaluate(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            if (refreshMetadata && object instanceof DBPRefreshableObject) {
                try {
                    getEditorInput().getNavigatorNode().refreshNode(monitor, ERDEditorEmbedded.this);
                } catch (DBException e) {
                    log.warn("Error refreshing database metadata", e);
                }
            }
            try {
                return loadFromDatabase(monitor);
            } catch (DBException e) {
                log.error("Error loading ER diagram", e);
            }
            return null;
        }
    }, progressControl.createLoadVisualizer());
    diagramLoadingJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            diagramLoadingJob = null;
        }
    });
    diagramLoadingJob.schedule();
}
Also used : DBException(org.jkiss.dbeaver.DBException) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DatabaseLoadService(org.jkiss.dbeaver.model.runtime.load.DatabaseLoadService) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject)

Example 72 with DBRProgressMonitor

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

the class RuntimeUtils method runTask.

public static boolean runTask(final DBRRunnableWithProgress task, String taskName, final long waitTime) {
    final MonitoringTask monitoringTask = new MonitoringTask(task);
    Job monitorJob = new AbstractJob(taskName) {

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                monitoringTask.run(monitor);
            } catch (InvocationTargetException e) {
                log.error(getName() + " - error", e.getTargetException());
                return Status.OK_STATUS;
            } catch (InterruptedException e) {
            // do nothing
            }
            return Status.OK_STATUS;
        }
    };
    monitorJob.schedule();
    // Wait for job to finish
    long startTime = System.currentTimeMillis();
    if (waitTime > 0) {
        while (!monitoringTask.finished && System.currentTimeMillis() - startTime < waitTime) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
    return monitoringTask.finished;
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 73 with DBRProgressMonitor

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

the class DriverDescriptor method validateFilesPresence.

@NotNull
private List<File> validateFilesPresence(boolean resetVersions) {
    boolean localLibsExists = false;
    final List<DBPDriverLibrary> downloadCandidates = new ArrayList<>();
    for (DBPDriverLibrary library : libraries) {
        if (library.isDisabled()) {
            // Nothing we can do about it
            continue;
        }
        if (!library.matchesCurrentPlatform()) {
            // Wrong OS or architecture
            continue;
        }
        if (library.isDownloadable()) {
            boolean allExists = true;
            if (resetVersions) {
                allExists = false;
            } else {
                List<DriverFileInfo> files = resolvedFiles.get(library);
                if (files == null) {
                    allExists = false;
                } else {
                    for (DriverFileInfo file : files) {
                        if (file.file == null || !file.file.exists()) {
                            allExists = false;
                            break;
                        }
                    }
                }
            }
            if (!allExists) {
                downloadCandidates.add(library);
            }
        } else {
            localLibsExists = true;
        }
    }
    //        if (!CommonUtils.isEmpty(fileSources)) {
    //            for (DriverFileSource source : fileSources) {
    //                for (DriverFileSource.FileInfo fileInfo : source.getFiles()) {
    //                    DriverLibraryLocal libraryLocal = new DriverLibraryLocal(this, DBPDriverLibrary.FileType.jar, fileInfo.getName());
    //                    final File localFile = libraryLocal.getLocalFile();
    //                }
    //            }
    //        }
    boolean downloaded = false;
    if (!downloadCandidates.isEmpty() || (!localLibsExists && !fileSources.isEmpty())) {
        final DriverDependencies dependencies = new DriverDependencies(downloadCandidates);
        boolean downloadOk = new UITask<Boolean>() {

            @Override
            protected Boolean runTask() {
                return DriverDownloadDialog.downloadDriverFiles(null, DriverDescriptor.this, dependencies);
            }
        }.execute();
        if (!downloadOk) {
            return Collections.emptyList();
        }
        if (resetVersions) {
            resetDriverInstance();
        /*
                for (DBPDriverLibrary library : libraries) {
                    if (!library.isDisabled()) {
                        library.resetVersion();
                    }
                }
*/
        }
        downloaded = true;
        for (DBPDriverDependencies.DependencyNode node : dependencies.getLibraryMap()) {
            List<DriverFileInfo> info = new ArrayList<>();
            resolvedFiles.put(node.library, info);
            collectLibraryFiles(node, info);
        }
        providerDescriptor.getRegistry().saveDrivers();
    }
    List<File> result = new ArrayList<>();
    for (DBPDriverLibrary library : libraries) {
        if (library.isDisabled() || !library.matchesCurrentPlatform()) {
            // Wrong OS or architecture
            continue;
        }
        if (library.isDownloadable()) {
            List<DriverFileInfo> files = resolvedFiles.get(library);
            if (files != null) {
                for (DriverFileInfo file : files) {
                    result.add(file.file);
                }
            }
        } else {
            result.add(library.getLocalFile());
        }
    }
    // Now check driver version
    if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.UI_DRIVERS_VERSION_UPDATE) && !downloaded) {
        // TODO: implement new version check
        if (false) {
            try {
                DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

                    @Override
                    public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        try {
                            checkDriverVersion(monitor);
                        } catch (IOException e) {
                            throw new InvocationTargetException(e);
                        }
                    }
                });
            } catch (InvocationTargetException e) {
                log.error(e.getTargetException());
            } catch (InterruptedException e) {
            // ignore
            }
        }
    }
    return result;
}
Also used : IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) NotNull(org.jkiss.code.NotNull)

Example 74 with DBRProgressMonitor

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

the class GenerateSQLContributor method makeResultSetContributions.

private void makeResultSetContributions(List<IContributionItem> menu, IResultSetSelection rss) {
    final IResultSetController rsv = rss.getController();
    DBSDataContainer dataContainer = rsv.getDataContainer();
    final List<DBDAttributeBinding> visibleAttributes = rsv.getModel().getVisibleAttributes();
    final DBSEntity entity = rsv.getModel().getSingleSource();
    if (dataContainer != null && !visibleAttributes.isEmpty() && entity != null) {
        final Collection<ResultSetRow> selectedRows = rss.getSelectedRows();
        if (!CommonUtils.isEmpty(selectedRows)) {
            menu.add(makeAction("SELECT by Unique Key", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
                        sql.append("SELECT ");
                        boolean hasAttr = false;
                        for (DBSAttributeBase attr : getValueAttributes(monitor, object, keyAttributes)) {
                            if (hasAttr)
                                sql.append(", ");
                            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
                            hasAttr = true;
                        }
                        sql.append("\nFROM ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
                        sql.append("\nWHERE ");
                        hasAttr = false;
                        for (DBDAttributeBinding binding : keyAttributes) {
                            if (hasAttr)
                                sql.append(" AND ");
                            sql.append(DBUtils.getObjectFullName(binding.getAttribute(), DBPEvaluationContext.DML)).append("=");
                            appendAttributeValue(rsv, sql, binding, firstRow);
                            hasAttr = true;
                        }
                        sql.append(";\n");
                    }
                }
            }));
            menu.add(makeAction("INSERT", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        Collection<? extends DBSAttributeBase> allAttributes = getAllAttributes(monitor, object);
                        sql.append("INSERT INTO ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
                        sql.append("\n(");
                        boolean hasAttr = false;
                        for (DBSAttributeBase attr : allAttributes) {
                            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                                continue;
                            }
                            if (hasAttr)
                                sql.append(", ");
                            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
                            hasAttr = true;
                        }
                        sql.append(")\nVALUES(");
                        hasAttr = false;
                        for (DBSAttributeBase attr : allAttributes) {
                            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                                continue;
                            }
                            if (hasAttr)
                                sql.append(", ");
                            DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
                            if (binding == null) {
                                appendDefaultValue(sql, attr);
                            } else {
                                appendAttributeValue(rsv, sql, binding, firstRow);
                            }
                            hasAttr = true;
                        }
                        sql.append(");\n");
                    }
                }
            }));
            menu.add(makeAction("UPDATE", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
                        Collection<? extends DBSAttributeBase> valueAttributes = getValueAttributes(monitor, object, keyAttributes);
                        sql.append("UPDATE ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
                        sql.append("\nSET ");
                        boolean hasAttr = false;
                        for (DBSAttributeBase attr : valueAttributes) {
                            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                                continue;
                            }
                            if (hasAttr)
                                sql.append(", ");
                            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
                            DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
                            if (binding == null) {
                                appendDefaultValue(sql, attr);
                            } else {
                                appendAttributeValue(rsv, sql, binding, firstRow);
                            }
                            hasAttr = true;
                        }
                        sql.append("\nWHERE ");
                        hasAttr = false;
                        for (DBDAttributeBinding attr : keyAttributes) {
                            if (hasAttr)
                                sql.append(" AND ");
                            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
                            appendAttributeValue(rsv, sql, attr, firstRow);
                            hasAttr = true;
                        }
                        sql.append(";\n");
                    }
                }
            }));
            menu.add(makeAction("DELETE by Unique Key", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
                        sql.append("DELETE FROM ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
                        sql.append("\nWHERE ");
                        boolean hasAttr = false;
                        for (DBDAttributeBinding binding : keyAttributes) {
                            if (hasAttr)
                                sql.append(" AND ");
                            sql.append(DBUtils.getObjectFullName(binding.getAttribute(), DBPEvaluationContext.DML)).append("=");
                            appendAttributeValue(rsv, sql, binding, firstRow);
                            hasAttr = true;
                        }
                        sql.append(";\n");
                    }
                }
            }));
        }
    }
}
Also used : ResultSetModel(org.jkiss.dbeaver.ui.controls.resultset.ResultSetModel) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 75 with DBRProgressMonitor

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

the class SQLEditor method runPostExecuteActions.

private void runPostExecuteActions(@Nullable SQLQueryResult result) {
    final DBCExecutionContext executionContext = getExecutionContext();
    if (executionContext != null) {
        final DBPDataSource dataSource = executionContext.getDataSource();
        // Dump server output
        DBCServerOutputReader outputReader = DBUtils.getAdapter(DBCServerOutputReader.class, dataSource);
        if (outputReader == null && result != null) {
            outputReader = new DefaultServerOutputReader(result);
        }
        if (outputReader != null && outputReader.isServerOutputEnabled()) {
            dumpServerOutput(executionContext, outputReader);
        }
        // Refresh active object
        if (result == null || !result.hasError() && getActivePreferenceStore().getBoolean(SQLPreferenceConstants.REFRESH_DEFAULTS_AFTER_EXECUTE)) {
            final DBSObjectSelector objectSelector = DBUtils.getAdapter(DBSObjectSelector.class, dataSource);
            if (objectSelector != null) {
                new AbstractJob("Refresh default object") {

                    @Override
                    protected IStatus run(DBRProgressMonitor monitor) {
                        try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.UTIL, "Refresh default object")) {
                            objectSelector.refreshDefaultObject(session);
                        } catch (Exception e) {
                            log.error(e);
                        }
                        return Status.OK_STATUS;
                    }
                }.schedule();
            }
        }
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DefaultServerOutputReader(org.jkiss.dbeaver.model.impl.DefaultServerOutputReader) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException)

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