Search in sources :

Example 6 with JDBCSession

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

the class GenericCatalog method getSchemas.

public Collection<GenericSchema> getSchemas(DBRProgressMonitor monitor) throws DBException {
    if (schemas == null && !isInitialized) {
        try (JDBCSession session = DBUtils.openMetaSession(monitor, this.getDataSource(), "Load catalog schemas")) {
            this.schemas = this.getDataSource().loadSchemas(session, this);
            this.isInitialized = true;
        }
    }
    return schemas;
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)

Example 7 with JDBCSession

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

the class GenericProcedure method loadProcedureColumns.

private void loadProcedureColumns(DBRProgressMonitor monitor) throws DBException {
    Collection<? extends GenericProcedure> procedures = getContainer().getProcedures(monitor, getName());
    if (procedures == null || !procedures.contains(this)) {
        throw new DBException("Internal error - cannot read columns for procedure '" + getName() + "' because its not found in container");
    }
    Iterator<? extends GenericProcedure> procIter = procedures.iterator();
    GenericProcedure procedure = null;
    final GenericMetaObject pcObject = getDataSource().getMetaObject(GenericConstants.OBJECT_PROCEDURE_COLUMN);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load procedure columns")) {
        final JDBCResultSet dbResult;
        if (functionResultType == null) {
            dbResult = session.getMetaData().getProcedureColumns(getCatalog() == null ? this.getPackage() == null || !this.getPackage().isNameFromCatalog() ? null : this.getPackage().getName() : getCatalog().getName(), getSchema() == null ? null : getSchema().getName(), getName(), getDataSource().getAllObjectsPattern());
        } else {
            dbResult = session.getMetaData().getFunctionColumns(getCatalog() == null ? null : getCatalog().getName(), getSchema() == null ? null : getSchema().getName(), getName(), getDataSource().getAllObjectsPattern());
        }
        try {
            int previousPosition = -1;
            while (dbResult.next()) {
                String columnName = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.COLUMN_NAME);
                int columnTypeNum = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.COLUMN_TYPE);
                int valueType = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.DATA_TYPE);
                String typeName = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.TYPE_NAME);
                int columnSize = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.LENGTH);
                boolean notNull = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.NULLABLE) == DatabaseMetaData.procedureNoNulls;
                int scale = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.SCALE);
                int precision = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.PRECISION);
                //int radix = GenericUtils.safeGetInt(dbResult, JDBCConstants.RADIX);
                String remarks = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.REMARKS);
                int position = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.ORDINAL_POSITION);
                DBSProcedureParameterKind parameterType;
                if (functionResultType == null) {
                    switch(columnTypeNum) {
                        case DatabaseMetaData.procedureColumnIn:
                            parameterType = DBSProcedureParameterKind.IN;
                            break;
                        case DatabaseMetaData.procedureColumnInOut:
                            parameterType = DBSProcedureParameterKind.INOUT;
                            break;
                        case DatabaseMetaData.procedureColumnOut:
                            parameterType = DBSProcedureParameterKind.OUT;
                            break;
                        case DatabaseMetaData.procedureColumnReturn:
                            parameterType = DBSProcedureParameterKind.RETURN;
                            break;
                        case DatabaseMetaData.procedureColumnResult:
                            parameterType = DBSProcedureParameterKind.RESULTSET;
                            break;
                        default:
                            parameterType = DBSProcedureParameterKind.UNKNOWN;
                            break;
                    }
                } else {
                    switch(columnTypeNum) {
                        case DatabaseMetaData.functionColumnIn:
                            parameterType = DBSProcedureParameterKind.IN;
                            break;
                        case DatabaseMetaData.functionColumnInOut:
                            parameterType = DBSProcedureParameterKind.INOUT;
                            break;
                        case DatabaseMetaData.functionColumnOut:
                            parameterType = DBSProcedureParameterKind.OUT;
                            break;
                        case DatabaseMetaData.functionReturn:
                            parameterType = DBSProcedureParameterKind.RETURN;
                            break;
                        case DatabaseMetaData.functionColumnResult:
                            parameterType = DBSProcedureParameterKind.RESULTSET;
                            break;
                        default:
                            parameterType = DBSProcedureParameterKind.UNKNOWN;
                            break;
                    }
                }
                if (CommonUtils.isEmpty(columnName) && parameterType == DBSProcedureParameterKind.RETURN) {
                    columnName = "RETURN";
                }
                if (position == 0) {
                    // Some drivers do not return ordinal position (PostgreSQL) but
                    // position is contained in column name
                    Matcher numberMatcher = PATTERN_COL_NAME_NUMERIC.matcher(columnName);
                    if (numberMatcher.matches()) {
                        position = Integer.parseInt(numberMatcher.group(1));
                    }
                }
                if (procedure == null || (previousPosition >= 0 && position <= previousPosition && procIter.hasNext())) {
                    procedure = procIter.next();
                }
                GenericProcedureParameter column = new GenericProcedureParameter(procedure, columnName, typeName, valueType, position, columnSize, scale, precision, notNull, remarks, parameterType);
                procedure.addColumn(column);
                previousPosition = position;
            }
        } finally {
            dbResult.close();
        }
    } catch (SQLException e) {
        throw new DBException(e, getDataSource());
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) GenericMetaObject(org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) DBSProcedureParameterKind(org.jkiss.dbeaver.model.struct.rdb.DBSProcedureParameterKind) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 8 with JDBCSession

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

the class FireBirdUtils method getProcedureSource.

public static String getProcedureSource(DBRProgressMonitor monitor, GenericProcedure procedure) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, procedure.getDataSource(), "Load procedure source code")) {
        DatabaseMetaData fbMetaData = session.getOriginal().getMetaData();
        String source = (String) fbMetaData.getClass().getMethod("getProcedureSourceCode", String.class).invoke(fbMetaData, procedure.getName());
        if (CommonUtils.isEmpty(source)) {
            return null;
        }
        return getProcedureSourceWithHeader(monitor, procedure, source);
    } catch (SQLException e) {
        throw new DBException("Can't read source code of procedure '" + procedure.getName() + "'", e);
    } catch (Exception e) {
        log.debug(e);
        return null;
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException)

Example 9 with JDBCSession

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

the class DerbyMetaModel method loadSequences.

@Override
public List<GenericSequence> loadSequences(@NotNull DBRProgressMonitor monitor, @NotNull GenericStructContainer container) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read procedure definition")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT seq.SEQUENCENAME,seq.CURRENTVALUE,seq.MINIMUMVALUE,seq.MAXIMUMVALUE,seq.INCREMENT\n" + "FROM sys.SYSSEQUENCES seq,sys.SYSSCHEMAS s\n" + "WHERE seq.SCHEMAID=s.SCHEMAID AND s.SCHEMANAME=?")) {
            dbStat.setString(1, container.getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                List<GenericSequence> result = new ArrayList<GenericSequence>();
                while (dbResult.next()) {
                    GenericSequence sequence = new GenericSequence(container, JDBCUtils.safeGetString(dbResult, 1), "", JDBCUtils.safeGetLong(dbResult, 2), JDBCUtils.safeGetLong(dbResult, 3), JDBCUtils.safeGetLong(dbResult, 4), JDBCUtils.safeGetLong(dbResult, 5));
                    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 10 with JDBCSession

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

the class JDBCDataSource method initialize.

@Override
public void initialize(@NotNull DBRProgressMonitor monitor) throws DBException {
    if (!container.getDriver().isEmbedded() && container.getPreferenceStore().getBoolean(ModelPreferences.META_SEPARATE_CONNECTION)) {
        synchronized (allContexts) {
            this.metaContext = new JDBCExecutionContext(this, "Metadata");
            this.metaContext.connect(monitor, true, null, false);
        }
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, ModelMessages.model_jdbc_read_database_meta_data)) {
        JDBCDatabaseMetaData metaData = session.getMetaData();
        if (this.sqlDialect instanceof JDBCSQLDialect) {
            try {
                ((JDBCSQLDialect) this.sqlDialect).initDriverSettings(this, metaData);
            } catch (Throwable e) {
                log.error("Error initializing dialect driver settings", e);
            }
        }
        try {
            databaseMajorVersion = metaData.getDatabaseMajorVersion();
            databaseMinorVersion = metaData.getDatabaseMinorVersion();
        } catch (Throwable e) {
            log.error("Error determining server version", e);
        }
        try {
            dataSourceInfo = createDataSourceInfo(metaData);
        } catch (Throwable e) {
            log.error("Error obtaining database info");
        }
    } catch (SQLException ex) {
        throw new DBException("Error getting JDBC meta data", ex, this);
    } finally {
        if (dataSourceInfo == null) {
            log.warn("NULL datasource info was created");
            dataSourceInfo = new JDBCDataSourceInfo(container);
        }
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) JDBCDatabaseMetaData(org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData)

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