Search in sources :

Example 16 with DBCException

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

the class PostgreTable method getSuperInheritance.

@NotNull
public List<PostgreTableInheritance> getSuperInheritance(DBRProgressMonitor monitor) throws DBException {
    if (superTables == 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.inhrelid=? AND c.oid=i.inhparent " + "ORDER BY i.inhseqno")) {
                dbStat.setLong(1, getObjectId());
                try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                    while (dbResult.next()) {
                        final long parentSchemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
                        final long parentTableId = JDBCUtils.safeGetLong(dbResult, "inhparent");
                        PostgreSchema schema = getDatabase().getSchema(monitor, parentSchemaId);
                        if (schema == null) {
                            log.warn("Can't find parent table's schema '" + parentSchemaId + "'");
                            continue;
                        }
                        PostgreTableBase parentTable = schema.getTable(monitor, parentTableId);
                        if (parentTable == null) {
                            log.warn("Can't find parent table '" + parentTableId + "' in '" + schema.getName() + "'");
                            continue;
                        }
                        if (superTables == null) {
                            superTables = new ArrayList<>();
                        }
                        superTables.add(new PostgreTableInheritance(this, parentTable, JDBCUtils.safeGetInt(dbResult, "inhseqno"), true));
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBCException(e, getDataSource());
        }
        if (superTables == null) {
            superTables = Collections.emptyList();
        }
    }
    return superTables;
}
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)

Example 17 with DBCException

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

the class OracleUtils method getSource.

public static String getSource(DBRProgressMonitor monitor, OracleSourceObject sourceObject, boolean body, boolean insertCreateReplace) throws DBCException {
    if (sourceObject.getSourceType().isCustom()) {
        log.warn("Can't read source for custom source objects");
        return "-- ???? CUSTOM SOURCE";
    }
    final String sourceType = sourceObject.getSourceType().name();
    final OracleSchema sourceOwner = sourceObject.getSchema();
    if (sourceOwner == null) {
        log.warn("No source owner for object '" + sourceObject.getName() + "'");
        return null;
    }
    monitor.beginTask("Load sources for '" + sourceObject.getName() + "'...", 1);
    String sysViewName = OracleConstants.VIEW_DBA_SOURCE;
    if (!sourceObject.getDataSource().isViewAvailable(monitor, OracleConstants.SCHEMA_SYS, sysViewName)) {
        sysViewName = OracleConstants.VIEW_ALL_SOURCE;
    }
    try (final JDBCSession session = DBUtils.openMetaSession(monitor, sourceOwner.getDataSource(), "Load source code for " + sourceType + " '" + sourceObject.getName() + "'")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT TEXT FROM " + OracleConstants.SCHEMA_SYS + "." + sysViewName + " " + "WHERE TYPE=? AND OWNER=? AND NAME=? " + "ORDER BY LINE")) {
            dbStat.setString(1, body ? sourceType + " BODY" : sourceType);
            dbStat.setString(2, sourceOwner.getName());
            dbStat.setString(3, sourceObject.getName());
            dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                StringBuilder source = null;
                int lineCount = 0;
                while (dbResult.next()) {
                    if (monitor.isCanceled()) {
                        break;
                    }
                    final String line = dbResult.getString(1);
                    if (source == null) {
                        source = new StringBuilder(200);
                    }
                    source.append(line);
                    lineCount++;
                    monitor.subTask("Line " + lineCount);
                }
                if (source == null) {
                    return null;
                }
                if (insertCreateReplace) {
                    return insertCreateReplace(sourceObject, body, source.toString());
                } else {
                    return source.toString();
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, sourceOwner.getDataSource());
    } finally {
        monitor.done();
    }
}
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 18 with DBCException

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

the class OracleUtils method getObjectStatus.

public static boolean getObjectStatus(DBRProgressMonitor monitor, OracleStatefulObject object, OracleObjectType objectType) throws DBCException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, object.getDataSource(), "Refresh state of " + objectType.getTypeName() + " '" + object.getName() + "'")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT STATUS FROM SYS.ALL_OBJECTS WHERE OBJECT_TYPE=? AND OWNER=? AND OBJECT_NAME=?")) {
            dbStat.setString(1, objectType.getTypeName());
            dbStat.setString(2, object.getSchema().getName());
            dbStat.setString(3, DBObjectNameCaseTransformer.transformObjectName(object, object.getName()));
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    return "VALID".equals(dbResult.getString("STATUS"));
                } else {
                    log.warn(objectType.getTypeName() + " '" + object.getName() + "' not found in system dictionary");
                    return false;
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, object.getDataSource());
    }
}
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 19 with DBCException

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

the class OraclePlanAnalyser method explain.

public void explain(JDBCSession session) throws DBCException {
    String planStmtId = SecurityUtils.generateUniqueId();
    try {
        // Detect plan table
        String planTableName = dataSource.getPlanTableName(session);
        if (planTableName == null) {
            throw new DBCException("Plan table not found - query can't be explained");
        }
        // Delete previous statement rows
        // (actually there should be no statement with this id -
        // but let's do it, just in case)
        JDBCPreparedStatement dbStat = session.prepareStatement("DELETE FROM " + planTableName + " WHERE STATEMENT_ID=? ");
        try {
            dbStat.setString(1, planStmtId);
            dbStat.execute();
        } finally {
            dbStat.close();
        }
        // Explain plan
        StringBuilder explainSQL = new StringBuilder();
        explainSQL.append("EXPLAIN PLAN ").append("\n").append("SET STATEMENT_ID = '").append(planStmtId).append("'\n").append("INTO ").append(planTableName).append("\n").append("FOR ").append(query);
        dbStat = session.prepareStatement(explainSQL.toString());
        try {
            dbStat.execute();
        } finally {
            dbStat.close();
        }
        // Read explained plan
        dbStat = session.prepareStatement("SELECT * FROM " + planTableName + " WHERE STATEMENT_ID=? ORDER BY ID");
        try {
            dbStat.setString(1, planStmtId);
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                rootNodes = new ArrayList<>();
                IntKeyMap<OraclePlanNode> allNodes = new IntKeyMap<>();
                while (dbResult.next()) {
                    OraclePlanNode node = new OraclePlanNode(dataSource, allNodes, dbResult);
                    allNodes.put(node.getId(), node);
                    if (node.getParent() == null) {
                        rootNodes.add(node);
                    }
                }
            }
        } finally {
            dbStat.close();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) IntKeyMap(org.jkiss.utils.IntKeyMap) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 20 with DBCException

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

the class OracleObjectValidateAction method handleExecute.

@Override
public void handleExecute(DBCSession session, Throwable error) throws DBCException {
    if (error != null) {
        return;
    }
    DBCCompileLog log = new DBCCompileLogBase();
    CompileHandler.logObjectErrors((JDBCSession) session, log, object, getObjectType());
    if (!log.getErrorStack().isEmpty()) {
        StringBuilder message = new StringBuilder();
        message.append("Error during ").append(getObjectType().getTypeName()).append(" '").append(object.getName()).append("' validation:");
        for (DBCCompileError e : log.getErrorStack()) {
            message.append("\n");
            message.append(e.toString());
        }
        throw new DBCException(message.toString());
    }
}
Also used : DBCCompileLogBase(org.jkiss.dbeaver.model.exec.compile.DBCCompileLogBase) DBCCompileLog(org.jkiss.dbeaver.model.exec.compile.DBCCompileLog) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCCompileError(org.jkiss.dbeaver.model.exec.compile.DBCCompileError)

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