Search in sources :

Example 66 with JDBCSession

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

the class FireBirdMetaModel method loadSequences.

@Override
public List<GenericSequence> loadSequences(@NotNull DBRProgressMonitor monitor, @NotNull GenericStructContainer container) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read sequences")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT RDB$GENERATOR_NAME,RDB$DESCRIPTION FROM RDB$GENERATORS")) {
            List<GenericSequence> result = new ArrayList<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, 1);
                    if (name == null) {
                        continue;
                    }
                    name = name.trim();
                    String description = JDBCUtils.safeGetString(dbResult, 2);
                    GenericSequence sequence = new GenericSequence(container, name, description, -1, 0, -1, 1);
                    result.add(sequence);
                }
            }
            // Obtain sequence values
            for (GenericSequence sequence : result) {
                try (JDBCPreparedStatement dbSeqStat = session.prepareStatement("SELECT GEN_ID(" + sequence.getName() + ", 0) from RDB$DATABASE")) {
                    try (JDBCResultSet seqResults = dbSeqStat.executeQuery()) {
                        seqResults.next();
                        sequence.setLastValue(JDBCUtils.safeGetLong(seqResults, 1));
                    }
                }
            }
            return result;
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
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) ArrayList(java.util.ArrayList)

Example 67 with JDBCSession

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

the class InformixMetaModel method loadTriggers.

@Override
public List<? extends GenericTrigger> loadTriggers(DBRProgressMonitor monitor, @NotNull GenericStructContainer container, @Nullable GenericTable table) throws DBException {
    assert table != null;
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read triggers")) {
        String query = "SELECT T1.trigname \n" + "FROM informix.systriggers AS T1, informix.systables AS T2 \n" + "WHERE T2.tabid = T1.tabid AND T2.tabname = ?";
        try (JDBCPreparedStatement dbStat = session.prepareStatement(query)) {
            dbStat.setString(2, table.getName());
            List<GenericTrigger> result = new ArrayList<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, 1);
                    if (name == null) {
                        continue;
                    }
                    name = name.trim();
                    GenericTrigger trigger = new GenericTrigger(container, table, name, null);
                    result.add(trigger);
                }
            }
            return result;
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
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) ArrayList(java.util.ArrayList)

Example 68 with JDBCSession

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

the class SQLServerMetaModel method extractSource.

private String extractSource(DBRProgressMonitor monitor, GenericDataSource dataSource, String catalog, String schema, String name) throws DBException {
    ServerType serverType = getServerType(monitor, dataSource);
    String systemSchema = getSystemSchema(serverType);
    catalog = DBUtils.getQuotedIdentifier(dataSource, catalog);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read source code")) {
        String mdQuery = serverType == ServerType.SQL_SERVER ? catalog + "." + systemSchema + ".sp_helptext '" + schema + "." + name + "'" : "SELECT sc.text\n" + "FROM " + catalog + "." + systemSchema + ".sysobjects so\n" + "INNER JOIN " + catalog + "." + systemSchema + ".syscomments sc on sc.id = so.id\n" + "WHERE user_name(so.uid)=? AND so.name=?";
        try (JDBCPreparedStatement dbStat = session.prepareStatement(mdQuery)) {
            if (serverType == ServerType.SYBASE) {
                dbStat.setString(1, schema);
                dbStat.setString(2, name);
            }
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                StringBuilder sql = new StringBuilder();
                while (dbResult.nextRow()) {
                    sql.append(dbResult.getString(1));
                }
                return sql.toString();
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, dataSource);
    }
}
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)

Example 69 with JDBCSession

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

the class HSQLMetaModel method loadSequences.

@Override
public List<GenericSequence> loadSequences(@NotNull DBRProgressMonitor monitor, @NotNull GenericStructContainer container) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read sequences")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM INFORMATION_SCHEMA.SEQUENCES WHERE SEQUENCE_SCHEMA=?")) {
            dbStat.setString(1, container.getName());
            List<GenericSequence> result = new ArrayList<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, "SEQUENCE_NAME");
                    if (name == null) {
                        continue;
                    }
                    GenericSequence sequence = new GenericSequence(container, name, null, JDBCUtils.safeGetLong(dbResult, "NEXT_VALUE"), JDBCUtils.safeGetLong(dbResult, "MINIMUM_VALUE"), JDBCUtils.safeGetLong(dbResult, "MAXIMUM_VALUE"), JDBCUtils.safeGetLong(dbResult, "INCREMENT"));
                    result.add(sequence);
                }
            }
            return result;
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
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) ArrayList(java.util.ArrayList)

Example 70 with JDBCSession

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

the class ExasolStructureAssistant method findObjectsByMask.

@NotNull
@Override
public List<DBSObjectReference> findObjectsByMask(DBRProgressMonitor monitor, DBSObject parentObject, DBSObjectType[] objectTypes, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults) throws DBException {
    LOG.debug(objectNameMask);
    List<ExasolObjectType> exasolObjectTypes = new ArrayList<>(objectTypes.length);
    for (DBSObjectType dbsObjectType : objectTypes) {
        exasolObjectTypes.add((ExasolObjectType) dbsObjectType);
    }
    ExasolSchema schema = parentObject instanceof ExasolSchema ? (ExasolSchema) parentObject : null;
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Find objects by name")) {
        return searchAllObjects(session, schema, objectNameMask, exasolObjectTypes, caseSensitive, maxResults);
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : ExasolObjectType(org.jkiss.dbeaver.ext.exasol.editors.ExasolObjectType) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) NotNull(org.jkiss.code.NotNull)

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