Search in sources :

Example 56 with JDBCSession

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

the class PostgreSequence method loadAdditionalInfo.

private void loadAdditionalInfo(DBRProgressMonitor monitor) {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load sequence additional info")) {
        try (JDBCPreparedStatement dbSeqStat = session.prepareStatement("SELECT last_value,min_value,max_value,increment_by from " + getFullyQualifiedName(DBPEvaluationContext.DML))) {
            try (JDBCResultSet seqResults = dbSeqStat.executeQuery()) {
                if (seqResults.next()) {
                    additionalInfo.lastValue = JDBCUtils.safeGetLong(seqResults, 1);
                    additionalInfo.minValue = JDBCUtils.safeGetLong(seqResults, 2);
                    additionalInfo.maxValue = JDBCUtils.safeGetLong(seqResults, 3);
                    additionalInfo.incrementBy = JDBCUtils.safeGetLong(seqResults, 4);
                }
            }
        }
        additionalInfo.loaded = true;
    } catch (Exception e) {
        log.warn("Error reading sequence values", e);
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBException(org.jkiss.dbeaver.DBException)

Example 57 with JDBCSession

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession 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)

Example 58 with JDBCSession

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

the class PostgreTableForeign method readForeignInfo.

private void readForeignInfo(DBRProgressMonitor monitor) throws DBException {
    if (foreignServerId > 0) {
        return;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read foreign table info")) {
        try (JDBCPreparedStatement stat = session.prepareStatement("SELECT * FROM pg_catalog.pg_foreign_table WHERE ftrelid=?")) {
            stat.setLong(1, getObjectId());
            try (JDBCResultSet result = stat.executeQuery()) {
                if (result.next()) {
                    foreignServerId = JDBCUtils.safeGetLong(result, "ftserver");
                    foreignOptions = JDBCUtils.safeGetArray(result, "ftoptions");
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, 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 59 with JDBCSession

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

the class PostgreViewBase method getObjectDefinitionText.

@Override
@Property(hidden = true, editable = true, updatable = true, order = -1)
public String getObjectDefinitionText(DBRProgressMonitor monitor) throws DBException {
    if (source == null) {
        if (isPersisted()) {
            try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read view definition")) {
                String definition = JDBCUtils.queryString(session, "SELECT pg_get_viewdef(?, true)", getObjectId());
                this.source = PostgreUtils.getViewDDL(this, definition);
            } catch (SQLException e) {
                throw new DBException("Error reading view definition", e);
            }
        } else {
            source = "";
        }
    }
    return source;
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) Property(org.jkiss.dbeaver.model.meta.Property)

Example 60 with JDBCSession

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

the class MySQLDataSource method loadPrivileges.

private List<MySQLPrivilege> loadPrivileges(DBRProgressMonitor monitor) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load privileges")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW PRIVILEGES")) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                List<MySQLPrivilege> privileges = new ArrayList<>();
                while (dbResult.next()) {
                    MySQLPrivilege user = new MySQLPrivilege(this, dbResult);
                    privileges.add(user);
                }
                return privileges;
            }
        }
    } catch (SQLException ex) {
        throw new DBException(ex, this);
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Aggregations

JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)73 SQLException (java.sql.SQLException)67 DBException (org.jkiss.dbeaver.DBException)54 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)54 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)46 DBCException (org.jkiss.dbeaver.model.exec.DBCException)16 ArrayList (java.util.ArrayList)15 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)7 NotNull (org.jkiss.code.NotNull)5 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 DatabaseMetaData (java.sql.DatabaseMetaData)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 JDBCCallableStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)3 Matcher (java.util.regex.Matcher)2 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)2 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)2 JDBCDatabaseMetaData (org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData)2 Property (org.jkiss.dbeaver.model.meta.Property)2 DBSObjectType (org.jkiss.dbeaver.model.struct.DBSObjectType)2 DBSProcedureType (org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType)2