Search in sources :

Example 11 with DBCExecutionContextDefaults

use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.

the class ContextDefaultObjectsReader method run.

@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
    DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
    DBSObjectContainer objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
    if (objectContainer == null) {
        return;
    }
    DBCExecutionContextDefaults contextDefaults = null;
    if (executionContext != null) {
        contextDefaults = executionContext.getContextDefaults();
    }
    if (contextDefaults == null) {
        return;
    }
    try {
        monitor.beginTask("Read default objects", 1);
        currentDatabaseInstanceName = null;
        Class<? extends DBSObject> childType = objectContainer.getPrimaryChildType(monitor);
        if (childType == null || !DBSObjectContainer.class.isAssignableFrom(childType)) {
            enabled = false;
        } else {
            enabled = true;
            DBSObjectContainer defObject = null;
            if (DBSCatalog.class.isAssignableFrom(childType)) {
                defObject = contextDefaults.getDefaultCatalog();
            }
            if (defObject != null) {
                Class<? extends DBSObject> catalogChildrenType = defObject.getPrimaryChildType(monitor);
                if (catalogChildrenType != null && DBSSchema.class.isAssignableFrom(catalogChildrenType)) {
                    currentDatabaseInstanceName = defObject.getName();
                    if (contextDefaults.supportsSchemaChange()) {
                        objectContainer = defObject;
                    } else if (!contextDefaults.supportsCatalogChange()) {
                        // Nothing can be changed
                        objectContainer = null;
                    }
                    DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
                    if (defaultSchema != null) {
                        defObject = defaultSchema;
                    }
                }
            }
            objectList = objectContainer == null ? (defObject == null ? Collections.emptyList() : Collections.singletonList(defObject)) : objectContainer.getChildren(monitor);
            defaultObject = defObject;
            if (readNodes) {
                // Cache navigator nodes
                if (objectList != null) {
                    for (DBSObject child : objectList) {
                        if (DBUtils.getAdapter(DBSObjectContainer.class, child) != null) {
                            DBNDatabaseNode node = navigatorModel.getNodeByObject(monitor, child, false);
                            if (node != null) {
                                nodeList.add(node);
                            }
                        }
                    }
                }
            }
        }
    } catch (DBException e) {
        throw new InvocationTargetException(e);
    } finally {
        monitor.done();
    }
}
Also used : DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel)

Example 12 with DBCExecutionContextDefaults

use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.

the class SelectDatabaseDialog method createUpperControls.

@Override
protected void createUpperControls(Composite dialogArea) {
    DBPDataSource dataSource = dataSourceContainer.getDataSource();
    if (currentInstanceName != null && dataSource != null) {
        DBSObjectContainer instanceContainer = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
        DBCExecutionContextDefaults contextDefaults = null;
        DBCExecutionContext defaultContext = DBUtils.getDefaultContext(instanceContainer, true);
        if (defaultContext != null) {
            contextDefaults = defaultContext.getContextDefaults();
        }
        if (instanceContainer != null && contextDefaults != null && contextDefaults.supportsCatalogChange()) {
            createInstanceSelector(dialogArea, instanceContainer);
        }
    }
}
Also used : DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 13 with DBCExecutionContextDefaults

use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.

the class DBTaskUtils method extractContext.

public static DBTTaskContext extractContext(@NotNull DBCExecutionContext executionContext) {
    DBTTaskContext context = new DBTTaskContext();
    DBCExecutionContextDefaults defaults = executionContext.getContextDefaults();
    if (defaults != null) {
        DBSCatalog defaultCatalog = defaults.getDefaultCatalog();
        if (defaultCatalog != null) {
            context.setDefaultCatalog(defaultCatalog.getName());
        }
        DBSSchema defaultSchema = defaults.getDefaultSchema();
        if (defaultSchema != null) {
            context.setDefaultSchema(defaultSchema.getName());
        }
    }
    DBCTransactionManager txnManager = DBUtils.getTransactionManager(executionContext);
    if (txnManager != null) {
        try {
            context.setAutoCommit(txnManager.isAutoCommit());
            DBPTransactionIsolation isolation = txnManager.getTransactionIsolation();
            if (isolation != null) {
                context.setTransactionIsolation(isolation.getCode());
            }
        } catch (Throwable e) {
            log.debug(e);
        }
    }
    return context;
}
Also used : DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) DBPTransactionIsolation(org.jkiss.dbeaver.model.DBPTransactionIsolation) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 14 with DBCExecutionContextDefaults

use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.

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 15 with DBCExecutionContextDefaults

use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.

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)

Aggregations

DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)20 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)16 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)14 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)12 DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)8 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)8 DBException (org.jkiss.dbeaver.DBException)6 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)6 IFile (org.eclipse.core.resources.IFile)4 IEditorPart (org.eclipse.ui.IEditorPart)4 DBPTransactionIsolation (org.jkiss.dbeaver.model.DBPTransactionIsolation)4 DBCException (org.jkiss.dbeaver.model.exec.DBCException)4 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)4 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)4 IDatabaseEditorInput (org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput)4 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IEditorInput (org.eclipse.ui.IEditorInput)2 IPropertyListener (org.eclipse.ui.IPropertyListener)2