Search in sources :

Example 61 with DBCExecutionContext

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

the class DataSourceAutoCommitHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    DBCExecutionContext context = getActiveExecutionContext(event, true);
    if (context != null) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
        if (txnManager != null) {
            try {
                final DBPDataSourceContainer container = context.getDataSource().getContainer();
                boolean newAutocommit = !container.isDefaultAutoCommit();
                if (context.isConnected()) {
                    // Get flag from connection
                    newAutocommit = !txnManager.isAutoCommit();
                }
                boolean autoCommit = newAutocommit;
                new AbstractJob("Set auto-commit") {

                    @Override
                    protected IStatus run(DBRProgressMonitor monitor) {
                        monitor.beginTask("Change connection auto-commit to " + autoCommit, 1);
                        try {
                            monitor.subTask("Change context '" + context.getContextName() + "' auto-commit state");
                            txnManager.setAutoCommit(monitor, autoCommit);
                        } catch (Exception e) {
                            return GeneralUtils.makeExceptionStatus(e);
                        } finally {
                            monitor.done();
                        }
                        return Status.OK_STATUS;
                    }
                }.schedule();
            } catch (DBException e) {
                DBWorkbench.getPlatformUI().showError("Auto-Commit", "Error while toggle auto-commit", e);
            }
        }
    }
    return null;
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) DBCException(org.jkiss.dbeaver.model.exec.DBCException) ExecutionException(org.eclipse.core.commands.ExecutionException) DBException(org.jkiss.dbeaver.DBException) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 62 with DBCExecutionContext

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

the class PendingTransactionsDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    contextTree = new Tree(composite, SWT.FULL_SELECTION | SWT.BORDER);
    contextTree.setHeaderVisible(true);
    contextTree.setLinesVisible(true);
    TreeColumn colName = new TreeColumn(contextTree, SWT.NONE);
    colName.setText("Connection");
    TreeColumn colTxn = new TreeColumn(contextTree, SWT.RIGHT);
    colTxn.setText("Transaction");
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = contextTree.getHeaderHeight() + contextTree.getItemHeight() * 5;
    contextTree.setLayoutData(gd);
    contextTree.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (e.item != null && e.item.getData() instanceof DBCExecutionContext) {
                selectedContext = (DBCExecutionContext) e.item.getData();
            } else {
                selectedContext = null;
            }
            boolean hasTransaction = selectedContext != null && QMUtils.isTransactionActive(selectedContext, false);
            commitButton.setEnabled(hasTransaction);
            rollbackButton.setEnabled(hasTransaction);
            logViewer.setFilter(createContextFilter(selectedContext));
            logViewer.refresh();
        }
    });
    closeOnFocusLost(contextTree);
    {
        Composite controlPanel = UIUtils.createPlaceholder(composite, 3, 5);
        controlPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        final Button showAllCheck = UIUtils.createCheckbox(controlPanel, "Show all connections", "Show all datasource connections. Otherwise shows only transactional connections.", false, 1);
        showAllCheck.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                loadContexts(showAllCheck.getSelection());
            }
        });
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.grabExcessHorizontalSpace = true;
        showAllCheck.setLayoutData(gd);
        commitButton = UIUtils.createPushButton(controlPanel, "Commit", DBeaverIcons.getImage(UIIcon.TXN_COMMIT));
        commitButton.setEnabled(false);
        commitButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                endTransaction(true);
            }
        });
        rollbackButton = UIUtils.createPushButton(controlPanel, "Rollback", DBeaverIcons.getImage(UIIcon.TXN_ROLLBACK));
        rollbackButton.setEnabled(false);
        rollbackButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                endTransaction(false);
            }
        });
        closeOnFocusLost(showAllCheck, commitButton, rollbackButton);
    }
    super.createTransactionLogPanel(composite);
    loadContexts(false);
    return parent;
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 63 with DBCExecutionContext

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

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

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

the class DataSourceTransactionLogHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Shell activeShell = HandlerUtil.getActiveShell(event);
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    DBCExecutionContext context = null;
    if (editor instanceof DBPContextProvider) {
        context = ((DBPContextProvider) editor).getExecutionContext();
    }
    TransactionLogDialog.showDialog(activeShell, context);
    return null;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) IEditorPart(org.eclipse.ui.IEditorPart)

Example 65 with DBCExecutionContext

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

the class DatabaseConsumerPageMapping method showDDL.

private void showDDL(DatabaseMappingContainer mapping) {
    final DatabaseConsumerSettings settings = getDatabaseConsumerSettings();
    final DBSObjectContainer container = settings.getContainer();
    if (container == null) {
        return;
    }
    DBPDataSource dataSource = container.getDataSource();
    final DBEPersistAction[][] ddl = new DBEPersistAction[1][];
    try {
        getWizard().getRunnableContext().run(true, true, monitor -> {
            monitor.beginTask(DTUIMessages.database_consumer_page_mapping_monitor_task, 1);
            try {
                DBCExecutionContext executionContext = DBUtils.getDefaultContext(dataSource, true);
                ddl[0] = DatabaseTransferUtils.generateTargetTableDDL(monitor, executionContext, container, mapping);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
            monitor.done();
        });
    } catch (InvocationTargetException e) {
        DBWorkbench.getPlatformUI().showError(DTUIMessages.database_consumer_page_mapping_title_target_DDL, DTUIMessages.database_consumer_page_mapping_message_error_generating_target_DDL, e);
        return;
    } catch (InterruptedException e) {
        return;
    }
    DBEPersistAction[] persistActions = ddl[0];
    if (ArrayUtils.isEmpty(persistActions)) {
        UIUtils.showMessageBox(getShell(), "No schema changes", "No changes are needed for this mapping", SWT.ICON_INFORMATION);
        return;
    }
    UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
    if (serviceSQL != null) {
        String sql = SQLUtils.generateScript(dataSource, persistActions, false);
        int result = serviceSQL.openSQLViewer(DBUtils.getDefaultContext(container, true), DTUIMessages.database_consumer_page_mapping_sqlviewer_title, null, sql, dataSource.getContainer().hasModifyPermission(DBPDataSourcePermission.PERMISSION_EDIT_METADATA), false);
        if (result == IDialogConstants.PROCEED_ID) {
            if (UIUtils.confirmAction(getShell(), "Create target objects", "Database metadata will be modified by creating new table(s) and column(s).\nAre you sure you want to proceed?")) {
                // Create target objects
                if (applySchemaChanges(dataSource, mapping, persistActions)) {
                    autoAssignMappings();
                    updateMappingsAndButtons();
                }
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL)

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