Search in sources :

Example 71 with DBNDatabaseNode

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

the class PostgreFDWConfigWizardPageConfig method loadSettings.

private void loadSettings() {
    DBPDataSourceContainer targetDataSource = getWizard().getSelectedDataSource();
    if (targetDataSource == null) {
        setErrorMessage("No target data source");
        return;
    }
    // Fill FDW list
    try {
        getWizard().getRunnableContext().run(false, true, monitor -> {
            try {
                schemaList = new ArrayList<>();
                schemaList.addAll(getWizard().getDatabase().getSchemas(monitor));
                // Fill from both installed FDW and pre-configured FDW
                fdwList = new ArrayList<>();
                for (PostgreForeignDataWrapper fdw : CommonUtils.safeCollection(getWizard().getDatabase().getForeignDataWrappers(monitor))) {
                    PostgreFDWConfigWizard.FDWInfo fdwInfo = new PostgreFDWConfigWizard.FDWInfo();
                    fdwInfo.installedFDW = fdw;
                    fdwList.add(fdwInfo);
                }
                for (FDWConfigDescriptor fdw : FDWConfigRegistry.getInstance().getConfigDescriptors()) {
                    boolean found = false;
                    for (PostgreFDWConfigWizard.FDWInfo fdwInfo : fdwList) {
                        if (fdwInfo.getId().equals(fdw.getFdwId())) {
                            fdwInfo.fdwDescriptor = fdw;
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        PostgreFDWConfigWizard.FDWInfo fdwInfo = new PostgreFDWConfigWizard.FDWInfo();
                        fdwInfo.fdwDescriptor = fdw;
                        fdwList.add(fdwInfo);
                    }
                }
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        });
    } catch (InvocationTargetException e) {
        log.debug(e.getTargetException());
        setErrorMessage(e.getTargetException().getMessage());
        return;
    } catch (InterruptedException e) {
        return;
    }
    setErrorMessage(null);
    // Detect FDW from target container
    PostgreFDWConfigWizard.FDWInfo fdwInfo = getWizard().getSelectedFDW();
    if (fdwInfo == null) {
        FDWConfigDescriptor fdwConfig = FDWConfigRegistry.getInstance().findFirstMatch(targetDataSource);
        if (fdwConfig != null) {
            for (PostgreFDWConfigWizard.FDWInfo fdw : fdwList) {
                if (fdw.fdwDescriptor == fdwConfig) {
                    fdwInfo = fdw;
                    break;
                }
            }
        }
    }
    fdwCombo.removeAll();
    for (PostgreFDWConfigWizard.FDWInfo fdw : fdwList) {
        String fdwName = fdw.getId();
        if (!CommonUtils.isEmpty(fdw.getDescription())) {
            fdwName += " (" + fdw.getDescription() + ")";
        }
        fdwCombo.add(fdwName);
    }
    if (fdwInfo != null) {
        getWizard().setSelectedFDW(fdwInfo);
        fdwCombo.setText(fdwInfo.getId());
    }
    schemaCombo.removeAll();
    for (PostgreSchema schema : schemaList) {
        schemaCombo.add(schema.getName());
    }
    PostgreSchema selectedSchema = getWizard().getSelectedSchema();
    if (selectedSchema != null) {
        schemaCombo.setText(selectedSchema.getName());
    } else {
        PostgreSchema publicSchema = DBUtils.findObject(schemaList, PostgreConstants.PUBLIC_SCHEMA_NAME);
        if (publicSchema != null) {
            schemaCombo.setText(publicSchema.getName());
            getWizard().setSelectedSchema(publicSchema);
        }
    }
    refreshFDWProperties();
    if (CommonUtils.isEmpty(fdwServerText.getText())) {
        String fdwServerId = (fdwInfo == null ? targetDataSource.getDriver().getId() : fdwInfo.getId()) + "_srv";
        getWizard().setFdwServerId(fdwServerId);
        fdwServerText.setText(fdwServerId);
    }
    // Fill entities
    targetDataSourceText.setText(targetDataSource.getName());
    targetDriverText.setText(targetDataSource.getDriver().getName());
    entityTable.removeAll();
    for (DBNDatabaseNode entityNode : getWizard().getSelectedEntities()) {
        TableItem item = new TableItem(entityTable, SWT.NONE);
        item.setImage(0, DBeaverIcons.getImage(entityNode.getNodeIconDefault()));
        item.setText(0, entityNode.getNodeFullName());
    }
    UIUtils.packColumns(entityTable, false);
    propsEditor.repackColumns();
    updatePageCompletion();
}
Also used : DBException(org.jkiss.dbeaver.DBException) PostgreForeignDataWrapper(org.jkiss.dbeaver.ext.postgresql.model.PostgreForeignDataWrapper) InvocationTargetException(java.lang.reflect.InvocationTargetException) FDWConfigDescriptor(org.jkiss.dbeaver.ext.postgresql.model.fdw.FDWConfigDescriptor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) PostgreSchema(org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 72 with DBNDatabaseNode

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

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 73 with DBNDatabaseNode

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

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

the class EditForeignKeyPage method loadTableList.

private void loadTableList(DBNDatabaseNode newContainerNode) {
    tableList.removeAll();
    final List<DBNDatabaseNode> entities = new ArrayList<>();
    try {
        UIUtils.runInProgressService(monitor -> {
            try {
                loadEntities(monitor, entities, newContainerNode);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        });
    } catch (InvocationTargetException e) {
        DBWorkbench.getPlatformUI().showError("Error loading tables", "Error during table load", e);
    } catch (InterruptedException e) {
    // Ignore
    }
    for (DBNDatabaseNode entityNode : entities) {
        TableItem tableItem = new TableItem(tableList, SWT.LEFT);
        tableItem.setText(entityNode.getNodeName());
        tableItem.setImage(DBeaverIcons.getImage(entityNode.getNodeIconDefault()));
        tableItem.setData(entityNode);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) ArrayList(java.util.ArrayList) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 75 with DBNDatabaseNode

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

the class DatabaseLazyEditorInput method initializeRealInput.

public IDatabaseEditorInput initializeRealInput(final DBRProgressMonitor monitor) throws DBException {
    // Get the node path.
    if (project != null) {
        dataSourceContainer = project.getDataSourceRegistry().getDataSource(dataSourceId);
    }
    if (dataSourceContainer == null) {
        // $NON-NLS-2$
        log.error("Can't find data source '" + dataSourceId + "'");
        return null;
    }
    if (project == null) {
        project = dataSourceContainer.getRegistry().getProject();
    }
    final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
    navigatorModel.ensureProjectLoaded(project);
    // dataSourceContainer, project, nodePath, nodeName, activePageId, activeFolderId
    DBPDataSource dataSource;
    while (!dataSourceContainer.isConnected()) {
        try {
            dataSourceContainer.connect(monitor, true, true);
        } catch (final DBException e) {
            // Connection error
            final Integer result = new UITask<Integer>() {

                @Override
                protected Integer runTask() {
                    ConnectionLostDialog clDialog = new ConnectionLostDialog(UIUtils.getActiveWorkbenchShell(), dataSourceContainer, e, "Close");
                    return clDialog.open();
                }
            }.execute();
            if (result == IDialogConstants.STOP_ID) {
                // Close editor
                return null;
            } else if (result == IDialogConstants.RETRY_ID) {
                continue;
            } else {
                return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
            }
        }
        break;
    }
    try {
        dataSource = dataSourceContainer.getDataSource();
        if (dataSource == null) {
            throw new DBException("Connection to '" + dataSourceContainer.getName() + "' canceled");
        }
        final DBNNode[] editorNodeResult = new DBNNode[1];
        DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
            try {
                DBNDataSource dsNode = (DBNDataSource) navigatorModel.getNodeByObject(monitor, this.dataSourceContainer, true);
                if (dsNode == null) {
                    throw new DBException("Datasource '" + this.dataSourceContainer.getName() + "' navigator node not found");
                }
                dsNode.initializeNode(monitor, null);
                editorNodeResult[0] = navigatorModel.getNodeByPath(monitor, project, nodePath);
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            }
        });
        DBNNode node = editorNodeResult[0];
        if (node == null) {
            throw new DBException("Navigator node '" + nodePath + "' not found");
        }
        if (node instanceof DBNDatabaseNode) {
            DatabaseNodeEditorInput realInput = new DatabaseNodeEditorInput((DBNDatabaseNode) node);
            realInput.setDefaultFolderId(activeFolderId);
            realInput.setDefaultPageId(activePageId);
            return realInput;
        } else {
            throw new DBException("Database node has bad type: " + node.getClass().getName());
        }
    } catch (DBException e) {
        return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) UITask(org.jkiss.dbeaver.ui.UITask) ConnectionLostDialog(org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

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