Search in sources :

Example 91 with DBCExecutionContext

use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.

the class NavigatorHandlerLinkEditor method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IWorkbenchPage activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (activeEditor == null) {
        return null;
    }
    NavigatorViewBase navigatorView = NavigatorUtils.getActiveNavigatorView(event);
    if (navigatorView == null) {
        return null;
    }
    if (navigatorView instanceof ProjectExplorerView || (navigatorView instanceof ProjectNavigatorView && activeEditor instanceof ITextEditor)) {
        IFile file = EditorUtils.getFileFromInput(activeEditor.getEditorInput());
        if (file != null) {
            showResourceInNavigator(navigatorView, file);
        }
    } else if (activeEditor.getEditorInput() instanceof IDatabaseEditorInput) {
        IDatabaseEditorInput editorInput = (IDatabaseEditorInput) activeEditor.getEditorInput();
        DBNNode dbnNode = editorInput.getNavigatorNode();
        if (dbnNode != null) {
            navigatorView.showNode(dbnNode);
        }
    } else if (activeEditor instanceof IDataSourceContainerProvider) {
        DBPDataSourceContainer dsContainer = ((IDataSourceContainerProvider) activeEditor).getDataSourceContainer();
        @NotNull DBSObject activeObject = null;
        if (dsContainer != null) {
            if (activeEditor instanceof DBPContextProvider) {
                DBCExecutionContext executionContext = ((DBPContextProvider) activeEditor).getExecutionContext();
                if (executionContext != null) {
                    DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
                    if (contextDefaults != null) {
                        activeObject = contextDefaults.getDefaultSchema();
                        if (activeObject == null) {
                            activeObject = contextDefaults.getDefaultCatalog();
                        }
                    }
                }
            }
            if (activeObject == null) {
                DBPDataSource dataSource = dsContainer.getDataSource();
                if (dataSource != null) {
                    activeObject = DBUtils.getDefaultOrActiveObject(dataSource.getDefaultInstance());
                } else {
                    activeObject = dsContainer;
                }
            }
            DBSObject objectToSelect = activeObject;
            final NavigatorViewBase view = navigatorView;
            UIUtils.runInUI(activePage.getWorkbenchWindow(), monitor -> {
                DBSObject showObject = objectToSelect;
                if (showObject instanceof DBSInstance && !(showObject instanceof DBPDataSourceContainer)) {
                    showObject = objectToSelect.getParentObject();
                }
                if (showObject instanceof DBPDataSource) {
                    showObject = ((DBPDataSource) showObject).getContainer();
                }
                DBNDatabaseNode objectNode = view.getModel().getNodeByObject(monitor, showObject, true);
                if (objectNode != null) {
                    view.showNode(objectNode);
                }
            });
        }
    }
    activePage.activate(navigatorView);
    return null;
}
Also used : DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) ProjectExplorerView(org.jkiss.dbeaver.ui.navigator.project.ProjectExplorerView) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IFile(org.eclipse.core.resources.IFile) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance) IEditorPart(org.eclipse.ui.IEditorPart) NotNull(org.jkiss.code.NotNull) IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ProjectNavigatorView(org.jkiss.dbeaver.ui.navigator.project.ProjectNavigatorView) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) NavigatorViewBase(org.jkiss.dbeaver.ui.navigator.database.NavigatorViewBase) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 92 with DBCExecutionContext

use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.

the class NavigatorHandlerObjectGoto method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    DBCExecutionContext context = null;
    DBSObject container = null;
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart instanceof DBPContextProvider) {
        context = ((DBPContextProvider) activePart).getExecutionContext();
    } else if (GeneralUtils.adapt(activePart, INavigatorModelView.class) != null) {
        final ISelection selection = HandlerUtil.getCurrentSelection(event);
        if (selection instanceof IStructuredSelection) {
            Object element = ((IStructuredSelection) selection).getFirstElement();
            if (element instanceof DBSWrapper) {
                DBSObject object = ((DBSWrapper) element).getObject();
                if (object != null) {
                    container = object;
                    while (container instanceof DBSFolder) {
                        container = container.getParentObject();
                    }
                    context = DBUtils.getDefaultContext(object, true);
                }
            }
        }
    }
    if (context == null) {
        DBWorkbench.getPlatformUI().showError("Go to object", "No active datasource");
        return null;
    }
    IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
    GotoObjectDialog dialog = new GotoObjectDialog(HandlerUtil.getActiveShell(event), context, container);
    dialog.open();
    Object[] objectsToOpen = dialog.getResult();
    if (!ArrayUtils.isEmpty(objectsToOpen)) {
        Collection<DBNDatabaseNode> nodes = NavigatorHandlerObjectBase.getNodesByObjects(Arrays.asList(objectsToOpen));
        for (DBNDatabaseNode node : nodes) {
            NavigatorUtils.openNavigatorNode(node, workbenchWindow);
        }
    }
    return null;
}
Also used : DBSFolder(org.jkiss.dbeaver.model.struct.DBSFolder) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) GotoObjectDialog(org.jkiss.dbeaver.ui.navigator.dialogs.GotoObjectDialog) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 93 with DBCExecutionContext

use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.

the class NavigatorUtils method syncEditorWithNavigator.

public static boolean syncEditorWithNavigator(INavigatorModelView navigatorView, IEditorPart activeEditor) {
    if (!(activeEditor instanceof IDataSourceContainerProviderEx)) {
        return false;
    }
    IDataSourceContainerProviderEx dsProvider = (IDataSourceContainerProviderEx) activeEditor;
    Viewer navigatorViewer = navigatorView.getNavigatorViewer();
    if (navigatorViewer == null) {
        return false;
    }
    DBNNode selectedNode = getSelectedNode(navigatorViewer.getSelection());
    if (!(selectedNode instanceof DBNDatabaseNode)) {
        return false;
    }
    final DBPDataSourceContainer ds = ((DBNDatabaseNode) selectedNode).getDataSourceContainer();
    if (ds == null) {
        return false;
    }
    if (dsProvider.getDataSourceContainer() != ds) {
        dsProvider.setDataSourceContainer(ds);
    }
    if (activeEditor instanceof DBPContextProvider) {
        // Now check if we can change default object
        DBSObject dbObject = ((DBNDatabaseNode) selectedNode).getObject();
        if (dbObject instanceof DBSCatalog || dbObject instanceof DBSSchema) {
            DBCExecutionContext navExecutionContext = null;
            try {
                navExecutionContext = DBUtils.getOrOpenDefaultContext(dbObject, false);
            } catch (DBCException ignored) {
            }
            DBCExecutionContext editorExecutionContext = ((DBPContextProvider) activeEditor).getExecutionContext();
            if (navExecutionContext != null && editorExecutionContext != null) {
                DBCExecutionContextDefaults editorContextDefaults = editorExecutionContext.getContextDefaults();
                if (editorContextDefaults != null) {
                    RuntimeUtils.runTask(monitor -> {
                        try {
                            monitor.beginTask("Change default object", 1);
                            if (dbObject instanceof DBSCatalog && dbObject != editorContextDefaults.getDefaultCatalog()) {
                                monitor.subTask("Change default catalog");
                                editorContextDefaults.setDefaultCatalog(monitor, (DBSCatalog) dbObject, null);
                            } else if (dbObject instanceof DBSSchema && dbObject != editorContextDefaults.getDefaultSchema()) {
                                monitor.subTask("Change default schema");
                                editorContextDefaults.setDefaultSchema(monitor, (DBSSchema) dbObject);
                            }
                            monitor.worked(1);
                            monitor.done();
                        } catch (DBCException e) {
                            throw new InvocationTargetException(e);
                        }
                    }, "Set active object", dbObject.getDataSource().getContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_OPEN_TIMEOUT));
                }
            }
        }
    }
    return true;
}
Also used : DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 94 with DBCExecutionContext

use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.

the class NavigatorHandlerSetDefaultObject method markObjectAsActive.

@SuppressWarnings("unchecked")
private void markObjectAsActive(final DBNDatabaseNode databaseNode, IEditorPart activeEditor) {
    DBNNode parentNode = databaseNode.getParentNode();
    if (parentNode instanceof DBNDatabaseItem) {
        markObjectAsActive((DBNDatabaseItem) parentNode, activeEditor);
        return;
    }
    DBSObject object = databaseNode.getObject();
    DBPDataSource dataSource = object.getDataSource();
    final DBCExecutionContext editorContext;
    if (activeEditor instanceof DBPContextProvider) {
        editorContext = ((DBPContextProvider) activeEditor).getExecutionContext();
    } else {
        editorContext = null;
    }
    TasksJob.runTask("Change default object", monitor -> {
        try {
            DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
                try {
                    DBCExecutionContext defaultContext = dataSource.getDefaultInstance().getDefaultContext(monitor, false);
                    DBCExecutionContext[] contextsToChange;
                    if (editorContext != null && editorContext != defaultContext && editorContext.getDataSource() == defaultContext.getDataSource()) {
                        contextsToChange = new DBCExecutionContext[] { defaultContext, editorContext };
                    } else {
                        contextsToChange = new DBCExecutionContext[] { defaultContext };
                    }
                    for (DBCExecutionContext executionContext : contextsToChange) {
                        DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
                        if (contextDefaults != null) {
                            if (object instanceof DBSCatalog && contextDefaults.supportsCatalogChange()) {
                                contextDefaults.setDefaultCatalog(monitor, (DBSCatalog) object, null);
                            } else if (object instanceof DBSSchema && contextDefaults.supportsSchemaChange()) {
                                contextDefaults.setDefaultSchema(monitor, (DBSSchema) object);
                            } else {
                                throw new DBCException("Internal error: active object change not supported");
                            }
                        }
                    }
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            });
        } catch (Exception e) {
            throw new InvocationTargetException(e);
        }
    });
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(org.eclipse.core.commands.ExecutionException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DBNDatabaseItem(org.jkiss.dbeaver.model.navigator.DBNDatabaseItem) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 95 with DBCExecutionContext

use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.

the class PostgreFDWConfigWizard method installFDW.

private void installFDW(DBRProgressMonitor monitor) throws DBException {
    monitor.beginTask("Generate FDW script", 2);
    monitor.subTask("Read actions");
    List<DBEPersistAction> actions = generateScript(monitor);
    monitor.subTask("Execute script");
    DBCExecutionContext context = DBUtils.getDefaultContext(getDatabase(), false);
    DBExecUtils.executeScript(monitor, context, "Install FDW", actions);
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction)

Aggregations

DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)107 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)27 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)22 DBException (org.jkiss.dbeaver.DBException)21 DBPContextProvider (org.jkiss.dbeaver.model.DBPContextProvider)20 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)16 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)15 GridData (org.eclipse.swt.layout.GridData)12 IEditorPart (org.eclipse.ui.IEditorPart)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)12 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)12 ArrayList (java.util.ArrayList)10 Composite (org.eclipse.swt.widgets.Composite)10 DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)10 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)10 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)8 SelectionEvent (org.eclipse.swt.events.SelectionEvent)8 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)8 NotNull (org.jkiss.code.NotNull)8