Search in sources :

Example 96 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class DBVEntityForeignKey method getRealReferenceConstraint.

@NotNull
public DBSEntityConstraint getRealReferenceConstraint(@NotNull DBRProgressMonitor monitor) throws DBException {
    if (refEntityId == null) {
        throw new DBException("Ref entity ID not set for virtual FK " + getName());
    }
    DBNNode refNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByPath(monitor, refEntityId);
    if (!(refNode instanceof DBNDatabaseNode)) {
        throw new DBException("Can't find reference node " + refEntityId + " for virtual foreign key");
    }
    DBSObject object = ((DBNDatabaseNode) refNode).getObject();
    if (object instanceof DBSEntity) {
        List<DBSEntityConstraint> constraints = DBVUtils.getAllConstraints(monitor, (DBSEntity) object);
        DBSObject refEntityConstraint = DBUtils.findObject(constraints, refConstraintId);
        if (refEntityConstraint == null) {
            throw new DBException("Can't find constraint " + refConstraintId + " in entity " + refEntityId);
        }
        return (DBSEntityConstraint) refEntityConstraint;
    } else {
        throw new DBException("Object " + refEntityId + " is not an entity");
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) NotNull(org.jkiss.code.NotNull)

Example 97 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class DBVEntityForeignKey method setReferencedConstraint.

public synchronized void setReferencedConstraint(DBRProgressMonitor monitor, DBSEntityConstraint constraint) throws DBException {
    DBSEntity refEntity = constraint.getParentObject();
    if (refEntity instanceof DBVEntity) {
        refEntity = ((DBVEntity) refEntity).getRealEntity(monitor);
    }
    DBNDatabaseNode refNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(monitor, refEntity, true);
    if (refNode == null) {
        log.warn("Can't find navigator node for object " + DBUtils.getObjectFullId(refEntity));
        return;
    }
    if (refEntityId != null) {
        DBVModel.removeFromCache(this);
    }
    this.refEntityId = refNode.getNodeItemPath();
    this.refConstraintId = constraint.getName();
    if (refEntityId != null) {
        DBVModel.addToCache(this);
    }
}
Also used : DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 98 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class DBVModel method handleEntityRename.

private static void handleEntityRename(DBSEntity object, String oldName, String newName) {
    DBNDatabaseNode objectNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(object);
    if (objectNode != null) {
        String objectNodePath = objectNode.getNodeItemPath();
        renameEntityInGlobalCache(objectNodePath, oldName, newName);
    }
    if (object.getDataSource() != null) {
        DBVModel vModel = object.getDataSource().getContainer().getVirtualModel();
        DBVEntity vEntity = vModel.findEntity(object, oldName, false);
        if (vEntity != null) {
            vEntity.handleRename(oldName, newName);
            vEntity.persistConfiguration();
        }
    }
}
Also used : DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 99 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class SQLToolTaskWizard method refreshOnFinish.

private void refreshOnFinish() throws Exception {
    try {
        getContainer().run(true, true, monitor -> {
            monitor.beginTask("Object refreshing", objectList.size());
            for (DBSObject object : objectList) {
                try {
                    DBNDatabaseNode objectNode = DBNUtils.getNodeByObject(object);
                    if (objectNode != null) {
                        objectNode.refreshNode(new DefaultProgressMonitor(monitor), DBNEvent.FORCE_REFRESH);
                        if (monitor.isCanceled()) {
                            break;
                        }
                        monitor.worked(1);
                    }
                } catch (Exception e) {
                    log.error("Error refreshing object '" + object.getName() + "'", e);
                }
            }
            monitor.done();
        });
    } catch (InvocationTargetException e) {
        throw new DBCException("Refresh error", e.getTargetException());
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DefaultProgressMonitor(org.jkiss.dbeaver.model.runtime.DefaultProgressMonitor) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) DBCException(org.jkiss.dbeaver.model.exec.DBCException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 100 with DBNDatabaseNode

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

Aggregations

DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)222 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)85 DBException (org.jkiss.dbeaver.DBException)83 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)81 InvocationTargetException (java.lang.reflect.InvocationTargetException)53 ArrayList (java.util.ArrayList)44 GridData (org.eclipse.swt.layout.GridData)36 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)29 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)28 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)27 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)26 ISelection (org.eclipse.jface.viewers.ISelection)21 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)21 DBNDatabaseFolder (org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)19 Collection (java.util.Collection)18 List (java.util.List)17 Composite (org.eclipse.swt.widgets.Composite)17 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)17 DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)17 SWT (org.eclipse.swt.SWT)15