Search in sources :

Example 1 with DBCException

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

the class DataSourceAutoCommitHandler method updateElement.

@Override
public void updateElement(UIElement element, Map parameters) {
    IWorkbenchWindow workbenchWindow = element.getServiceLocator().getService(IWorkbenchWindow.class);
    if (workbenchWindow == null || workbenchWindow.getActivePage() == null) {
        return;
    }
    IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
    if (activeEditor == null) {
        return;
    }
    boolean autoCommit = true;
    DBPTransactionIsolation isolation = null;
    DBCExecutionContext context = getExecutionContext(activeEditor);
    if (context != null && context.isConnected()) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
        if (txnManager != null) {
            try {
                // Change auto-commit mode
                autoCommit = txnManager.isAutoCommit();
                isolation = txnManager.getTransactionIsolation();
            } catch (DBCException e) {
                log.warn(e);
            }
        }
    } else if (activeEditor instanceof IDataSourceContainerProvider) {
        DBPDataSourceContainer container = ((IDataSourceContainerProvider) activeEditor).getDataSourceContainer();
        if (container != null) {
            autoCommit = container.isDefaultAutoCommit();
            isolation = container.getActiveTransactionsIsolation();
        }
    }
    element.setChecked(autoCommit);
    // Update command image
    element.setIcon(DBeaverIcons.getImageDescriptor(autoCommit ? UIIcon.TXN_COMMIT_AUTO : UIIcon.TXN_COMMIT_MANUAL));
    String isolationName = isolation == null ? "?" : isolation.getTitle();
    element.setText(autoCommit ? "Switch to manual commit (" + isolationName + ")" : "Switch to auto-commit");
    element.setTooltip(autoCommit ? "Manual commit (" + isolationName + ")" : "Auto-commit");
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IDataSourceContainerProvider(org.jkiss.dbeaver.model.IDataSourceContainerProvider) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPTransactionIsolation(org.jkiss.dbeaver.model.DBPTransactionIsolation) DBCException(org.jkiss.dbeaver.model.exec.DBCException) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 2 with DBCException

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

the class ComplexObjectEditor method extractValue.

public Object extractValue() {
    DBDComplexValue complexValue = getInput();
    final ComplexElement[] items = childrenMap.get(complexValue);
    if (complexValue instanceof DBDValueCloneable) {
        try {
            complexValue = (DBDComplexValue) ((DBDValueCloneable) complexValue).cloneValue(VoidProgressMonitor.INSTANCE);
        } catch (DBCException e) {
            log.error("Error cloning complex value", e);
        }
    }
    if (complexValue instanceof DBDComposite) {
        for (int i = 0; i < items.length; i++) {
            ((DBDComposite) complexValue).setAttributeValue(((CompositeField) items[i]).attribute, items[i].value);
        }
    } else if (complexValue instanceof DBDCollection) {
        if (items != null) {
            final Object[] newValues = new Object[items.length];
            for (int i = 0; i < items.length; i++) {
                newValues[i] = items[i].value;
            }
            ((DBDCollection) complexValue).setContents(newValues);
        }
    }
    return complexValue;
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) Point(org.eclipse.swt.graphics.Point)

Example 3 with DBCException

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

the class ExasolTable method read.

private void read(DBRProgressMonitor monitor) throws DBCException {
    JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read Table Details");
    try (JDBCStatement stmt = session.createStatement()) {
        String sql = String.format(readAdditionalInfo, ExasolUtils.quoteString(this.getSchema().getName()), ExasolUtils.quoteString(this.getName()), ExasolUtils.quoteString(this.getSchema().getName()), ExasolUtils.quoteString(this.getName()), ExasolUtils.quoteString(this.getSchema().getName()), ExasolUtils.quoteString(this.getName()));
        try (JDBCResultSet dbResult = stmt.executeQuery(sql)) {
            dbResult.next();
            this.hasDistKey = JDBCUtils.safeGetBoolean(dbResult, "TABLE_HAS_DISTRIBUTION_KEY");
            this.lastCommit = JDBCUtils.safeGetTimestamp(dbResult, "LAST_COMMIT");
            this.sizeRaw = JDBCUtils.safeGetLong(dbResult, "RAW_OBJECT_SIZE");
            this.sizeCompressed = JDBCUtils.safeGetLong(dbResult, "MEM_OBJECT_SIZE");
            this.deletePercentage = JDBCUtils.safeGetFloat(dbResult, "DELETE_PERCENTAGE");
            this.createTime = JDBCUtils.safeGetTimestamp(dbResult, "CREATED");
            this.hasRead = true;
        }
    } catch (SQLException e) {
        throw new DBCException(e, getDataSource());
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 4 with DBCException

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

the class ExasolPlanAnalyser method explain.

public void explain(DBCSession session) throws DBCException {
    rootNodes = new ArrayList<>();
    JDBCSession connection = (JDBCSession) session;
    boolean oldAutoCommit = false;
    try {
        oldAutoCommit = connection.getAutoCommit();
        if (oldAutoCommit)
            connection.setAutoCommit(false);
        //alter session
        JDBCUtils.executeSQL(connection, "ALTER SESSION SET PROFILE = 'ON'");
        //execute query
        JDBCUtils.executeSQL(connection, query);
        //alter session
        JDBCUtils.executeSQL(connection, "ALTER SESSION SET PROFILE = 'OFF'");
        //rollback in case of DML
        connection.rollback();
        //alter session
        JDBCUtils.executeSQL(connection, "FLUSH STATISTICS");
        connection.commit();
        //retrieve execute info
        try (JDBCPreparedStatement stmt = connection.prepareStatement("SELECT * FROM EXA_USER_PROFILE_LAST_DAY WHERE SESSION_ID = CURRENT_SESSION AND STMT_ID = (select max(stmt_id) from EXA_USER_PROFILE_LAST_DAY where sql_text = ?)")) {
            stmt.setString(1, query);
            try (JDBCResultSet dbResult = stmt.executeQuery()) {
                while (dbResult.next()) {
                    ExasolPlanNode node = new ExasolPlanNode(null, dbResult);
                    rootNodes.add(node);
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    } finally {
        //rollback changes because profile actually executes query and it could be INSERT/UPDATE
        try {
            connection.rollback();
            if (oldAutoCommit)
                connection.setAutoCommit(true);
        } catch (SQLException e) {
            LOG.error("Error closing plan analyser", e);
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 5 with DBCException

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

the class StreamTransferConsumer method initExporter.

private void initExporter(DBCSession session) throws DBCException {
    if (settings.getFormatterProfile() != null) {
        session.setDataFormatterProfile(settings.getFormatterProfile());
    }
    exportSite = new StreamExportSite();
    // Open output streams
    boolean outputClipboard = settings.isOutputClipboard();
    outputFile = outputClipboard ? null : makeOutputFile();
    try {
        if (outputClipboard) {
            this.outputBuffer = new StringWriter(2048);
            this.writer = new PrintWriter(this.outputBuffer, true);
        } else {
            this.outputStream = new BufferedOutputStream(new FileOutputStream(outputFile), 10000);
            if (settings.isCompressResults()) {
                zipStream = new ZipOutputStream(this.outputStream);
                zipStream.putNextEntry(new ZipEntry(getOutputFileName()));
                StreamTransferConsumer.this.outputStream = zipStream;
            }
            this.writer = new PrintWriter(new OutputStreamWriter(this.outputStream, settings.getOutputEncoding()), true);
        }
        // Check for BOM
        if (!outputClipboard && settings.isOutputEncodingBOM()) {
            byte[] bom = GeneralUtils.getCharsetBOM(settings.getOutputEncoding());
            if (bom != null) {
                outputStream.write(bom);
                outputStream.flush();
            }
        }
    } catch (IOException e) {
        closeExporter();
        throw new DBCException("Data transfer IO error", e);
    }
    try {
        // init exporter
        processor.init(exportSite);
    } catch (DBException e) {
        throw new DBCException("Can't initialize data exporter", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) ZipEntry(java.util.zip.ZipEntry) DBCException(org.jkiss.dbeaver.model.exec.DBCException) ZipOutputStream(java.util.zip.ZipOutputStream)

Aggregations

DBCException (org.jkiss.dbeaver.model.exec.DBCException)60 SQLException (java.sql.SQLException)28 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)16 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)14 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)13 DBException (org.jkiss.dbeaver.DBException)11 NotNull (org.jkiss.code.NotNull)9 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)5 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)5 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)4 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Tree (org.eclipse.swt.widgets.Tree)3 TreeColumn (org.eclipse.swt.widgets.TreeColumn)3 TreeItem (org.eclipse.swt.widgets.TreeItem)3 Nullable (org.jkiss.code.Nullable)3 DBPPlatform (org.jkiss.dbeaver.model.app.DBPPlatform)3 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3