Search in sources :

Example 66 with DBCExecutionContext

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

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 67 with DBCExecutionContext

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

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)

Example 68 with DBCExecutionContext

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

the class DataSourceTransactionModeContributor method fillContributionItems.

@Override
protected void fillContributionItems(final List<IContributionItem> menuItems) {
    IEditorPart activePart = UIUtils.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    DBCExecutionContext executionContext = AbstractDataSourceHandler.getExecutionContextFromPart(activePart);
    DBPDataSource dataSource = null;
    if (executionContext != null) {
        dataSource = executionContext.getDataSource();
    }
    if (dataSource == null) {
        return;
    }
    final DBPDataSourceInfo dsInfo = dataSource.getInfo();
    DBCTransactionManager txnManager = DBUtils.getTransactionManager(executionContext);
    if (txnManager != null) {
        boolean autoCommit = false;
        try {
            autoCommit = txnManager.isAutoCommit();
        } catch (DBCException e) {
            log.warn("Can't determine current auto-commit mode", e);
        }
        // Transactions
        DBPTransactionIsolation txnLevelCurrent = null;
        try {
            txnLevelCurrent = txnManager.getTransactionIsolation();
        } catch (DBCException ex) {
            log.warn("Can't determine current transaction isolation level", ex);
        }
        menuItems.add(ActionUtils.makeActionContribution(new TransactionAutoCommitAction(executionContext, true, autoCommit, txnLevelCurrent), true));
        menuItems.add(ActionUtils.makeActionContribution(new TransactionAutoCommitAction(executionContext, false, !autoCommit, txnLevelCurrent), true));
        ISmartTransactionManager smartTransactionManager = DBUtils.getAdapter(ISmartTransactionManager.class, activePart);
        menuItems.add(ActionUtils.makeActionContribution(new SmartAutoCommitAction(dataSource, smartTransactionManager), true));
        menuItems.add(new Separator());
        for (DBPTransactionIsolation txi : CommonUtils.safeCollection(dsInfo.getSupportedTransactionsIsolation())) {
            if (!txi.isEnabled()) {
                continue;
            }
            menuItems.add(ActionUtils.makeActionContribution(new TransactionIsolationAction(executionContext, txi, txi.equals(txnLevelCurrent)), true));
        }
    }
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) IEditorPart(org.eclipse.ui.IEditorPart) Separator(org.eclipse.jface.action.Separator) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 69 with DBCExecutionContext

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

the class TransactionInfoDialog method createTransactionLogPanel.

protected void createTransactionLogPanel(Composite composite) {
    DBCExecutionContext context = getCurrentContext();
    QMEventFilter filter = context == null ? VOID_FILTER : createContextFilter(context);
    logViewer = new QueryLogViewer(composite, activeEditor.getSite(), filter, false, true);
    logViewer.setUseDefaultFilter(false);
    final Object gd = logViewer.getControl().getLayoutData();
    if (gd instanceof GridData) {
        ((GridData) gd).heightHint = logViewer.getControl().getHeaderHeight() + logViewer.getControl().getItemHeight() * 5;
    }
    showAllCheck = UIUtils.createCheckbox(composite, CoreMessages.transaction_info_dialog_checkbox_show_all_queries, CoreMessages.transaction_info_dialog_label_show_all_transaction_queries, false, 1);
    showAllCheck.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            updateTransactionFilter();
        }
    });
    showPreviousCheck = UIUtils.createCheckbox(composite, CoreMessages.transaction_info_dialog_checkbox_show_previous_transactions, CoreMessages.transaction_info_dialog_label_otherwise, false, 1);
    showPreviousCheck.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            updateTransactionFilter();
        }
    });
    closeOnFocusLost(logViewer.getSearchText(), logViewer.getControl(), showAllCheck, showPreviousCheck);
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) QMEventFilter(org.jkiss.dbeaver.model.qm.QMEventFilter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) QMMObject(org.jkiss.dbeaver.model.qm.meta.QMMObject) QueryLogViewer(org.jkiss.dbeaver.ui.controls.querylog.QueryLogViewer)

Example 70 with DBCExecutionContext

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

the class TransactionInfoDialog method updateTransactionFilter.

protected void updateTransactionFilter() {
    DBCExecutionContext context = getCurrentContext();
    QMEventFilter filter = context == null ? VOID_FILTER : createContextFilter(context);
    logViewer.setFilter(filter);
    logViewer.refresh();
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) QMEventFilter(org.jkiss.dbeaver.model.qm.QMEventFilter)

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