Search in sources :

Example 61 with DBRProgressMonitor

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

the class EditForeignKeyPage method handleRefTableSelect.

private void handleRefTableSelect(ISelection selection) {
    DBNDatabaseNode refTableNode = null;
    if (!selection.isEmpty() && selection instanceof IStructuredSelection && ((IStructuredSelection) selection).size() == 1) {
        final Object element = ((IStructuredSelection) selection).getFirstElement();
        if (element instanceof DBNDatabaseNode && ((DBNDatabaseNode) element).getObject() instanceof DBSTable && ((DBNDatabaseNode) element).getObject().isPersisted()) {
            refTableNode = (DBNDatabaseNode) element;
        }
    }
    if (refTableNode != null) {
        if (refTableNode.getObject() == curRefTable) {
            // The same selection
            return;
        } else {
            curRefTable = (DBSTable) refTableNode.getObject();
        }
    }
    uniqueKeyCombo.removeAll();
    try {
        curConstraints = new ArrayList<>();
        curConstraint = null;
        if (refTableNode != null) {
            final DBSTable refTable = (DBSTable) refTableNode.getObject();
            DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

                @Override
                public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        // Cache own table columns
                        ownTable.getAttributes(monitor);
                        // Cache ref table columns
                        refTable.getAttributes(monitor);
                        // Get constraints
                        final Collection<? extends DBSTableConstraint> constraints = refTable.getConstraints(monitor);
                        if (!CommonUtils.isEmpty(constraints)) {
                            for (DBSTableConstraint constraint : constraints) {
                                if (constraint.getConstraintType().isUnique()) {
                                    curConstraints.add(constraint);
                                }
                            }
                        }
                    } catch (DBException e) {
                        throw new InvocationTargetException(e);
                    }
                }
            });
        }
        for (DBSTableConstraint constraint : curConstraints) {
            uniqueKeyCombo.add(constraint.getName());
        }
        uniqueKeyCombo.select(0);
        uniqueKeyCombo.setEnabled(curConstraints.size() > 1);
        if (curConstraints.size() == 1) {
            curConstraint = curConstraints.get(0);
        }
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(getShell(), CoreMessages.dialog_struct_edit_fk_error_load_constraints_title, CoreMessages.dialog_struct_edit_fk_error_load_constraints_message, e.getTargetException());
    } catch (InterruptedException e) {
    // do nothing
    }
    handleUniqueKeySelect();
    updatePageState();
}
Also used : DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Collection(java.util.Collection) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 62 with DBRProgressMonitor

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

the class MySQLUserEditorGeneral method activatePart.

@Override
public void activatePart() {
    if (isLoaded) {
        return;
    }
    isLoaded = true;
    LoadingJob.createService(new DatabaseLoadService<List<MySQLPrivilege>>(MySQLMessages.editors_user_editor_general_service_load_catalog_privileges, getExecutionContext()) {

        @Override
        public List<MySQLPrivilege> evaluate(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                final List<MySQLPrivilege> privList = getDatabaseObject().getDataSource().getPrivilegesByKind(monitor, MySQLPrivilege.Kind.ADMIN);
                for (Iterator<MySQLPrivilege> iterator = privList.iterator(); iterator.hasNext(); ) {
                    MySQLPrivilege priv = iterator.next();
                    // Remove proxy (it is not singleton)
                    if (priv.getName().equalsIgnoreCase("proxy")) {
                        iterator.remove();
                    }
                }
                return privList;
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        }
    }, pageControl.createLoadVisualizer()).schedule();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DatabaseLoadService(org.jkiss.dbeaver.model.runtime.load.DatabaseLoadService) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) MySQLPrivilege(org.jkiss.dbeaver.ext.mysql.model.MySQLPrivilege) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 63 with DBRProgressMonitor

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

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

the class GenericConnectionPage method createEmbeddedDatabase.

private void createEmbeddedDatabase() {
    String paramCreate = CommonUtils.toString(site.getDriver().getDriverParameter(GenericConstants.PARAM_CREATE_URL_PARAM));
    DataSourceDescriptor dataSource = (DataSourceDescriptor) site.getActiveDataSource();
    final DataSourceDescriptor testDataSource = new DataSourceDescriptor(site.getDataSourceRegistry(), dataSource.getId(), dataSource.getDriver(), new DBPConnectionConfiguration(dataSource.getConnectionConfiguration()));
    saveSettings(testDataSource);
    DBPConnectionConfiguration cfg = testDataSource.getConnectionConfiguration();
    cfg.setUrl(cfg.getUrl() + paramCreate);
    String databaseName = cfg.getDatabaseName();
    testDataSource.setName(databaseName);
    if (!UIUtils.confirmAction(getShell(), "Create Database", "Are you sure you want to create database '" + databaseName + "'?")) {
        testDataSource.dispose();
        return;
    }
    try {
        site.getRunnableContext().run(true, true, new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    createEmbeddedDatabase(monitor, testDataSource);
                } catch (DBException e1) {
                    throw new InvocationTargetException(e1);
                }
            }
        });
        MessageDialog.openInformation(getShell(), "Database Create", "Database '" + databaseName + "' created!");
    } catch (InvocationTargetException e1) {
        UIUtils.showErrorDialog(getShell(), "Create database", "Error creating database", e1.getTargetException());
    } catch (InterruptedException e1) {
    // Just ignore
    }
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBException(org.jkiss.dbeaver.DBException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 65 with DBRProgressMonitor

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

the class DriverPropertiesDialogPage method setVisible.

@Override
public void setVisible(boolean visible) {
    super.setVisible(visible);
    // Set props model
    if (visible && propsControl != null) {
        final DBPDataSourceContainer activeDataSource = site.getActiveDataSource();
        if (prevConnectionInfo == activeDataSource.getConnectionConfiguration()) {
            return;
        }
        final DBPConnectionConfiguration tmpConnectionInfo = new DBPConnectionConfiguration();
        final DataSourceDescriptor tempDataSource = new DataSourceDescriptor(site.getDataSourceRegistry(), activeDataSource.getId(), (DriverDescriptor) activeDataSource.getDriver(), tmpConnectionInfo);
        hostPage.saveSettings(tempDataSource);
        tmpConnectionInfo.getProperties().putAll(activeDataSource.getConnectionConfiguration().getProperties());
        try {
            getSite().getRunnableContext().run(true, true, new DBRRunnableWithProgress() {

                @Override
                public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    monitor.beginTask("Loading driver properties", 1);
                    try {
                        propertySource = propsControl.makeProperties(monitor, site.getDriver(), tmpConnectionInfo);
                    } finally {
                        monitor.done();
                    }
                }
            });
        } catch (InvocationTargetException e) {
            setErrorMessage(e.getTargetException().getMessage());
        } catch (InterruptedException e) {
        // ignore
        }
        propsControl.loadProperties(propertySource);
        prevConnectionInfo = activeDataSource.getConnectionConfiguration();
        tempDataSource.dispose();
    }
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

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