Search in sources :

Example 36 with DBCException

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

the class TableToolDialog method getScriptListener.

@Override
protected SQLScriptProgressListener<PostgreObject> getScriptListener() {
    return new SQLScriptStatusDialog<PostgreObject>(getShell(), getTitle() + " progress", null) {

        @Override
        protected void createStatusColumns(Tree objectTree) {
            TreeColumn msgColumn = new TreeColumn(objectTree, SWT.NONE);
            msgColumn.setText("Message");
        }

        @Override
        public void processObjectResults(@NotNull PostgreObject object, @Nullable DBCStatement statement, @Nullable DBCResultSet resultSet) throws DBCException {
            if (statement == null) {
                return;
            }
            TreeItem treeItem = getTreeItem(object);
            if (treeItem != null) {
                try {
                    int warnNum = 0;
                    SQLWarning warning = ((JDBCStatement) statement).getWarnings();
                    while (warning != null) {
                        if (warnNum == 0) {
                            treeItem.setText(1, warning.getMessage());
                        } else {
                            TreeItem warnItem = new TreeItem(treeItem, SWT.NONE);
                            warnItem.setText(0, "");
                            warnItem.setText(1, warning.getMessage());
                        }
                        warnNum++;
                        warning = warning.getNextWarning();
                    }
                    if (warnNum == 0) {
                        treeItem.setText(1, "Done");
                    }
                } catch (SQLException e) {
                // ignore
                }
                treeItem.setExpanded(true);
            }
        }

        @Override
        public void endObjectProcessing(@NotNull PostgreObject object, Exception error) {
            super.endObjectProcessing(object, error);
            if (error != null) {
                TreeItem treeItem = getTreeItem(object);
                if (treeItem != null) {
                    treeItem.setText(1, error.getMessage());
                }
            }
        }
    };
}
Also used : SQLWarning(java.sql.SQLWarning) TreeItem(org.eclipse.swt.widgets.TreeItem) SQLException(java.sql.SQLException) NotNull(org.jkiss.code.NotNull) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLScriptStatusDialog(org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) PostgreObject(org.jkiss.dbeaver.ext.postgresql.model.PostgreObject) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) Nullable(org.jkiss.code.Nullable)

Example 37 with DBCException

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

the class PostgreStructValueHandler method bindParameter.

@Override
protected void bindParameter(JDBCSession session, JDBCPreparedStatement statement, DBSTypedObject paramType, int paramIndex, Object value) throws DBCException, SQLException {
    if (value == null) {
        statement.setNull(paramIndex, Types.STRUCT);
    } else if (value instanceof DBDComposite) {
        DBDComposite struct = (DBDComposite) value;
        if (struct.isNull()) {
            statement.setNull(paramIndex, Types.STRUCT);
        } else if (struct instanceof JDBCComposite) {
            final Object[] values = ((JDBCComposite) struct).getValues();
            final String string = PostgreUtils.generateObjectString(values);
            statement.setObject(paramIndex, string, Types.OTHER);
        }
    } else {
        throw new DBCException("Struct parameter type '" + value.getClass().getName() + "' not supported");
    }
}
Also used : DBDComposite(org.jkiss.dbeaver.model.data.DBDComposite) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCComposite(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCComposite)

Example 38 with DBCException

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

the class PostgrePlanAnalyser method explain.

public void explain(DBCSession session) throws DBCException {
    JDBCSession connection = (JDBCSession) session;
    boolean oldAutoCommit = false;
    try {
        oldAutoCommit = connection.getAutoCommit();
        if (oldAutoCommit) {
            connection.setAutoCommit(false);
        }
        try (JDBCPreparedStatement dbStat = connection.prepareStatement("EXPLAIN (FORMAT XML, ANALYSE) " + query)) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    SQLXML planXML = dbResult.getSQLXML(1);
                    parsePlan(planXML);
                }
            } catch (XMLException e) {
                throw new DBCException("Can't parse plan XML", e);
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    } finally {
        // Rollback changes because EXPLAIN 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) SQLXML(java.sql.SQLXML) XMLException(org.jkiss.utils.xml.XMLException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 39 with DBCException

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

the class WMIClass method readData.

@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, DBDDataFilter dataFilter, long firstRow, long maxRows, long flags) throws DBCException {
    DBCStatistics statistics = new DBCStatistics();
    try {
        long startTime = System.currentTimeMillis();
        WMIObjectCollectorSink sink = new WMIObjectCollectorSink(session.getProgressMonitor(), getNamespace().getService(), firstRow, maxRows);
        getNamespace().getService().enumInstances(getName(), sink, WMIConstants.WBEM_FLAG_SHALLOW);
        statistics.setExecuteTime(System.currentTimeMillis() - startTime);
        startTime = System.currentTimeMillis();
        sink.waitForFinish();
        WMIResultSet resultSet = new WMIResultSet(session, this, sink.getObjectList());
        long resultCount = 0;
        try {
            dataReceiver.fetchStart(session, resultSet, firstRow, maxRows);
            while (resultSet.nextRow()) {
                resultCount++;
                dataReceiver.fetchRow(session, resultSet);
            }
        } finally {
            try {
                dataReceiver.fetchEnd(session, resultSet);
            } catch (DBCException e) {
                //$NON-NLS-1$
                log.error("Error while finishing result set fetch", e);
            }
            resultSet.close();
            dataReceiver.close();
        }
        statistics.setFetchTime(System.currentTimeMillis() - startTime);
        statistics.setRowsFetched(resultCount);
        return statistics;
    } catch (WMIException e) {
        throw new DBCException(e, getDataSource());
    }
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCStatistics(org.jkiss.dbeaver.model.exec.DBCStatistics) NotNull(org.jkiss.code.NotNull)

Example 40 with DBCException

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

the class PostgreTable method getSubInheritance.

@NotNull
public List<PostgreTableInheritance> getSubInheritance(@NotNull DBRProgressMonitor monitor) throws DBException {
    if (subTables == null) {
        try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table inheritance info")) {
            try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT i.*,c.relnamespace " + "FROM pg_catalog.pg_inherits i,pg_catalog.pg_class c " + "WHERE i.inhparent=? AND c.oid=i.inhrelid")) {
                dbStat.setLong(1, getObjectId());
                try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                    while (dbResult.next()) {
                        final long subSchemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
                        final long subTableId = JDBCUtils.safeGetLong(dbResult, "inhrelid");
                        PostgreSchema schema = getDatabase().getSchema(monitor, subSchemaId);
                        if (schema == null) {
                            log.warn("Can't find sub-table's schema '" + subSchemaId + "'");
                            continue;
                        }
                        PostgreTableBase subTable = schema.getTable(monitor, subTableId);
                        if (subTable == null) {
                            log.warn("Can't find sub-table '" + subTableId + "' in '" + schema.getName() + "'");
                            continue;
                        }
                        if (subTables == null) {
                            subTables = new ArrayList<>();
                        }
                        subTables.add(new PostgreTableInheritance(subTable, this, JDBCUtils.safeGetInt(dbResult, "inhseqno"), true));
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBCException(e, getDataSource());
        }
        if (subTables == null) {
            subTables = Collections.emptyList();
        }
    }
    return subTables;
}
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) NotNull(org.jkiss.code.NotNull)

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