Search in sources :

Example 66 with JDBCResultSet

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

the class PostgreDataType method readEnumValues.

private void readEnumValues(JDBCSession session) throws DBException {
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT e.enumlabel \n" + "FROM pg_catalog.pg_enum e\n" + "WHERE e.enumtypid=?")) {
        dbStat.setLong(1, getObjectId());
        try (JDBCResultSet rs = dbStat.executeQuery()) {
            List<String> values = new ArrayList<>();
            while (rs.nextRow()) {
                values.add(JDBCUtils.safeGetString(rs, 1));
            }
            enumValues = values.toArray();
        }
    } catch (SQLException e) {
        throw new DBException("Error reading enum values", e, getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList)

Example 67 with JDBCResultSet

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet 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 68 with JDBCResultSet

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

the class PostgreStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String columnNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load constraints
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.attname,x.attrelid,x.atttypid,c.relnamespace " + "FROM pg_catalog.pg_attribute x, pg_catalog.pg_class c\n" + "WHERE c.oid=x.attrelid AND x.attname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND c.relnamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.attname LIMIT " + maxResults)) {
        dbStat.setString(1, columnNameMask);
        if (!CommonUtils.isEmpty(schema)) {
            PostgreUtils.setArrayParameter(dbStat, 2, schema);
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
                final long tableId = JDBCUtils.safeGetLong(dbResult, "attrelid");
                final String attributeName = JDBCUtils.safeGetString(dbResult, "attname");
                final PostgreSchema constrSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                if (constrSchema == null) {
                    log.debug("Schema '" + schemaId + "' not found");
                    continue;
                }
                objects.add(new AbstractObjectReference(attributeName, constrSchema, null, PostgreTableBase.class, RelationalObjectType.TYPE_TABLE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        final PostgreTableBase table = PostgreUtils.getObjectById(monitor, constrSchema.tableCache, constrSchema, tableId);
                        if (table == null) {
                            throw new DBException("Table '" + tableId + "' not found in schema '" + constrSchema.getName() + "'");
                        }
                        return table.getAttribute(monitor, attributeName);
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 69 with JDBCResultSet

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet 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 70 with JDBCResultSet

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

Aggregations

JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)82 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)66 SQLException (java.sql.SQLException)57 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)54 DBException (org.jkiss.dbeaver.DBException)53 ArrayList (java.util.ArrayList)20 DBCException (org.jkiss.dbeaver.model.exec.DBCException)15 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)12 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)10 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)4 NotNull (org.jkiss.code.NotNull)3 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)3 DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 Matcher (java.util.regex.Matcher)2 DB2Parameter (org.jkiss.dbeaver.ext.db2.info.DB2Parameter)2 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2