Search in sources :

Example 1 with DBCExecutionContextDefaults

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

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

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

the class NavigatorUtils method addSetDefaultObjectAction.

private static void addSetDefaultObjectAction(IWorkbenchSite workbenchSite, IMenuManager manager, DBNNode selectedNode) {
    // Add "Set active object" menu
    boolean addSetActive = false;
    if (selectedNode.isPersisted() && selectedNode instanceof DBNDatabaseNode && !(selectedNode instanceof DBNDatabaseFolder) && ((DBNDatabaseNode) selectedNode).getObject() != null) {
        DBSObject selectedObject = ((DBNDatabaseNode) selectedNode).getObject();
        DBPDataSource dataSource = ((DBNDatabaseNode) selectedNode).getDataSource();
        if (dataSource != null) {
            DBCExecutionContext defaultContext = dataSource.getDefaultInstance().getDefaultContext(new VoidProgressMonitor(), false);
            DBCExecutionContextDefaults contextDefaults = defaultContext.getContextDefaults();
            if (contextDefaults != null) {
                if ((selectedObject instanceof DBSCatalog && contextDefaults.supportsCatalogChange() && contextDefaults.getDefaultCatalog() != selectedObject) || (selectedObject instanceof DBSSchema && contextDefaults.supportsSchemaChange() && contextDefaults.getDefaultSchema() != selectedObject)) {
                    addSetActive = true;
                }
            }
        }
    }
    if (addSetActive) {
        manager.add(ActionUtils.makeCommandContribution(workbenchSite, NavigatorCommands.CMD_OBJECT_SET_DEFAULT));
    }
    manager.add(new Separator());
}
Also used : DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 3 with DBCExecutionContextDefaults

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

the class SelectActiveSchemaHandler method updateElement.

@Override
public void updateElement(UIElement element, Map parameters) {
    if ("true".equals(parameters.get("noCustomLabel"))) {
        return;
    }
    IWorkbenchWindow workbenchWindow = element.getServiceLocator().getService(IWorkbenchWindow.class);
    if (workbenchWindow == null || workbenchWindow.getActivePage() == null) {
        return;
    }
    IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
    if (activeEditor == null) {
        return;
    }
    String schemaName = "< N/A >";
    DBIcon schemaIcon = DBIcon.TREE_SCHEMA;
    String schemaTooltip = UINavigatorMessages.toolbar_datasource_selector_combo_database_tooltip;
    DBPDataSourceContainer dataSource = DataSourceToolbarUtils.getCurrentDataSource(workbenchWindow);
    if (dataSource != null && dataSource.isConnected()) {
        // schemaName = "<no schema>";
        IEditorInput editorInput = activeEditor.getEditorInput();
        if (editorInput instanceof IDatabaseEditorInput) {
            if (editorInput instanceof DatabaseLazyEditorInput) {
                activeEditor.addPropertyListener(new IPropertyListener() {

                    @Override
                    public void propertyChanged(Object source, int propId) {
                        if (EntityEditor.PROP_TITLE == propId) {
                            DataSourceToolbarUtils.updateCommandsUI();
                            activeEditor.removePropertyListener(this);
                        }
                    }
                });
            }
            DBCExecutionContext executionContext = ((IDatabaseEditorInput) editorInput).getExecutionContext();
            if (executionContext != null) {
                DBSObject schemaObject = DBUtils.getSelectedObject(executionContext);
                if (schemaObject != null && DBUtils.getPublicObjectContainer(schemaObject) != dataSource) {
                    DBSObject schemaParent = schemaObject.getParentObject();
                    if (schemaParent instanceof DBSObjectContainer && !(schemaParent instanceof DBPDataSource)) {
                        schemaName = schemaObject.getName() + "@" + schemaParent.getName();
                    } else {
                        schemaName = schemaObject.getName();
                    }
                }
            }
        } else {
            DBCExecutionContext executionContext = getExecutionContextFromPart(activeEditor);
            DBCExecutionContextDefaults contextDefaults = null;
            if (executionContext != null) {
                contextDefaults = executionContext.getContextDefaults();
            }
            if (contextDefaults != null) {
                DBSCatalog defaultCatalog = contextDefaults.getDefaultCatalog();
                DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
                if (defaultCatalog != null && (defaultSchema != null || contextDefaults.supportsSchemaChange())) {
                    schemaName = defaultSchema == null ? "?" : defaultSchema.getName() + "@" + defaultCatalog.getName();
                    schemaIcon = DBIcon.TREE_SCHEMA;
                } else if (defaultCatalog != null) {
                    schemaName = defaultCatalog.getName();
                    schemaIcon = DBIcon.TREE_DATABASE;
                } else if (defaultSchema != null) {
                    schemaName = defaultSchema.getName();
                    schemaIcon = DBIcon.TREE_SCHEMA;
                }
            }
        }
    }
    element.setText(schemaName);
    element.setIcon(DBeaverIcons.getImageDescriptor(schemaIcon));
    element.setTooltip(schemaTooltip);
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DatabaseLazyEditorInput(org.jkiss.dbeaver.ui.editors.DatabaseLazyEditorInput) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) IPropertyListener(org.eclipse.ui.IPropertyListener) IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBIcon(org.jkiss.dbeaver.model.DBIcon) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IEditorInput(org.eclipse.ui.IEditorInput)

Example 4 with DBCExecutionContextDefaults

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

the class SQLScriptExecuteHandler method runScripts.

private void runScripts(DBRProgressMonitor monitor, DBTTask task, SQLScriptExecuteSettings settings, Log log, PrintStream logStream) throws DBException {
    List<DBPDataSourceContainer> dataSources = settings.getDataSources();
    for (String filePath : settings.getScriptFiles()) {
        IFile sqlFile = SQLScriptExecuteSettings.getWorkspaceFile(filePath);
        try (InputStream sqlStream = sqlFile.getContents(true)) {
            try (Reader fileReader = new InputStreamReader(sqlStream, sqlFile.getCharset())) {
                String sqlScriptContent = IOUtils.readToString(fileReader);
                try {
                    for (DBPDataSourceContainer dataSourceContainer : dataSources) {
                        if (!dataSourceContainer.isConnected()) {
                            dataSourceContainer.connect(monitor, true, true);
                        }
                        DBPDataSource dataSource = dataSourceContainer.getDataSource();
                        if (dataSource == null) {
                            throw new DBException("Can't obtain data source connection");
                        }
                        DBCExecutionContext executionContext = dataSource.getDefaultInstance().getDefaultContext(monitor, false);
                        log.debug("> Execute script [" + filePath + "] in [" + dataSourceContainer.getName() + "]");
                        DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
                        if (contextDefaults != null) {
                            DBSCatalog defaultCatalog = contextDefaults.getDefaultCatalog();
                            if (defaultCatalog != null) {
                                log.debug("> Default catalog: " + defaultCatalog.getName());
                            }
                            DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
                            if (defaultSchema != null) {
                                log.debug("> Default schema: " + defaultSchema.getName());
                            }
                        }
                        processScript(monitor, task, settings, executionContext, filePath, sqlScriptContent, log, logStream);
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        } catch (Throwable e) {
            Throwable error = e instanceof InvocationTargetException ? ((InvocationTargetException) e).getTargetException() : e;
            throw new DBException("Error executing script '" + filePath + "'", error);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) IFile(org.eclipse.core.resources.IFile) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 5 with DBCExecutionContextDefaults

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

the class ConnectionPageInitialization method loadDatabaseSettings.

private void loadDatabaseSettings(DBRProgressMonitor monitor, DBPDataSource dataSource) {
    DBPDataSourceContainer dataSourceContainer = dataSource.getContainer();
    Collection<DBPTransactionIsolation> txnLevels = CommonUtils.safeCollection(dataSource.getInfo().getSupportedTransactionsIsolation());
    Integer levelCode = dataSourceContainer.getDefaultTransactionsIsolation();
    UIUtils.syncExec(() -> {
        autocommit.setSelection(dataSourceContainer.isDefaultAutoCommit());
        // isolationLevel.setEnabled(!autocommit.getSelection());
        supportedLevels.clear();
        DBPTransactionIsolation defaultLevel = null;
        {
            if (levelCode != null && !CommonUtils.isEmpty(txnLevels)) {
                for (DBPTransactionIsolation level : txnLevels) {
                    if (level.getCode() == levelCode) {
                        defaultLevel = level;
                        break;
                    }
                }
            }
        }
        isolationLevel.removeAll();
        supportedLevels.clear();
        for (DBPTransactionIsolation level : txnLevels) {
            if (!level.isEnabled()) {
                continue;
            }
            isolationLevel.add(level.getTitle());
            supportedLevels.add(level);
            if (level.equals(defaultLevel)) {
                isolationLevel.select(isolationLevel.getItemCount() - 1);
            }
        }
    });
    if (dataSource instanceof DBSObjectContainer) {
        DBCExecutionContext executionContext = DBUtils.getDefaultContext(dataSource, true);
        DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
        DBSObjectContainer catalogContainer = DBUtils.getChangeableObjectContainer(contextDefaults, (DBSObjectContainer) dataSource, DBSCatalog.class);
        if (catalogContainer != null) {
            loadSelectableObject(monitor, catalogContainer, defaultCatalog, contextDefaults, true);
        }
        DBSObjectContainer schemaContainer = DBUtils.getChangeableObjectContainer(contextDefaults, (DBSObjectContainer) dataSource, DBSSchema.class);
        loadSelectableObject(monitor, schemaContainer, defaultSchema, contextDefaults, false);
    }
    txnOptionsLoaded = true;
}
Also used : DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBPTransactionIsolation(org.jkiss.dbeaver.model.DBPTransactionIsolation) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

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