Search in sources :

Example 11 with JDBCResultSet

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

the class GenericMetaModel method loadProcedures.

public void loadProcedures(DBRProgressMonitor monitor, @NotNull GenericObjectContainer container) throws DBException {
    Map<String, GenericPackage> packageMap = null;
    GenericDataSource dataSource = container.getDataSource();
    GenericMetaObject procObject = dataSource.getMetaObject(GenericConstants.OBJECT_PROCEDURE);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load procedures")) {
        // Read procedures
        JDBCResultSet dbResult = session.getMetaData().getProcedures(container.getCatalog() == null ? null : container.getCatalog().getName(), container.getSchema() == null ? null : container.getSchema().getName(), dataSource.getAllObjectsPattern());
        try {
            while (dbResult.next()) {
                if (monitor.isCanceled()) {
                    break;
                }
                String procedureCatalog = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_CAT);
                String procedureName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_NAME);
                String specificName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.SPECIFIC_NAME);
                int procTypeNum = GenericUtils.safeGetInt(procObject, dbResult, JDBCConstants.PROCEDURE_TYPE);
                String remarks = GenericUtils.safeGetString(procObject, dbResult, JDBCConstants.REMARKS);
                DBSProcedureType procedureType;
                switch(procTypeNum) {
                    case DatabaseMetaData.procedureNoResult:
                        procedureType = DBSProcedureType.PROCEDURE;
                        break;
                    case DatabaseMetaData.procedureReturnsResult:
                        procedureType = DBSProcedureType.FUNCTION;
                        break;
                    case DatabaseMetaData.procedureResultUnknown:
                        procedureType = DBSProcedureType.PROCEDURE;
                        break;
                    default:
                        procedureType = DBSProcedureType.UNKNOWN;
                        break;
                }
                if (specificName == null && procedureName.indexOf(';') != -1) {
                    // [JDBC: SQL Server native driver]
                    specificName = procedureName;
                    procedureName = procedureName.substring(0, procedureName.lastIndexOf(';'));
                }
                // Check for packages. Oracle (and may be some other databases) uses catalog name as storage for package name
                String packageName = null;
                GenericPackage procedurePackage = null;
                if (!CommonUtils.isEmpty(procedureCatalog) && CommonUtils.isEmpty(dataSource.getCatalogs())) {
                    // Catalog name specified while there are no catalogs in data source
                    packageName = procedureCatalog;
                }
                if (!CommonUtils.isEmpty(packageName)) {
                    if (packageMap == null) {
                        packageMap = new TreeMap<>();
                    }
                    procedurePackage = packageMap.get(packageName);
                    if (procedurePackage == null) {
                        procedurePackage = new GenericPackage(container, packageName, true);
                        packageMap.put(packageName, procedurePackage);
                        container.addPackage(procedurePackage);
                    }
                }
                final GenericProcedure procedure = createProcedureImpl(procedurePackage != null ? procedurePackage : container, procedureName, specificName, remarks, procedureType, null);
                if (procedurePackage != null) {
                    procedurePackage.addProcedure(procedure);
                } else {
                    container.addProcedure(procedure);
                }
            }
        } finally {
            dbResult.close();
        }
        try {
            // Try to read functions (note: this function appeared only in Java 1.6 so it maybe not implemented by many drivers)
            // Read procedures
            dbResult = session.getMetaData().getFunctions(container.getCatalog() == null ? null : container.getCatalog().getName(), container.getSchema() == null ? null : container.getSchema().getName(), dataSource.getAllObjectsPattern());
            try {
                while (dbResult.next()) {
                    if (monitor.isCanceled()) {
                        break;
                    }
                    String functionName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.FUNCTION_NAME);
                    if (functionName == null) {
                        // so let's skip yet another procedure list
                        continue;
                    }
                    String specificName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.SPECIFIC_NAME);
                    if (specificName == null && functionName.indexOf(';') != -1) {
                        // [JDBC: SQL Server native driver]
                        specificName = functionName;
                        functionName = functionName.substring(0, functionName.lastIndexOf(';'));
                    }
                    if (container.hasProcedure(functionName)) {
                        // Seems to be a duplicate
                        continue;
                    }
                    int funcTypeNum = GenericUtils.safeGetInt(procObject, dbResult, JDBCConstants.FUNCTION_TYPE);
                    String remarks = GenericUtils.safeGetString(procObject, dbResult, JDBCConstants.REMARKS);
                    GenericFunctionResultType functionResultType;
                    switch(funcTypeNum) {
                        //case DatabaseMetaData.functionResultUnknown: functionResultType = GenericFunctionResultType.UNKNOWN; break;
                        case DatabaseMetaData.functionNoTable:
                            functionResultType = GenericFunctionResultType.NO_TABLE;
                            break;
                        case DatabaseMetaData.functionReturnsTable:
                            functionResultType = GenericFunctionResultType.TABLE;
                            break;
                        default:
                            functionResultType = GenericFunctionResultType.UNKNOWN;
                            break;
                    }
                    final GenericProcedure procedure = createProcedureImpl(container, functionName, specificName, remarks, DBSProcedureType.FUNCTION, functionResultType);
                    container.addProcedure(procedure);
                }
            } finally {
                dbResult.close();
            }
        } catch (Throwable e) {
            log.debug("Can't read generic functions", e);
        }
    } catch (SQLException e) {
        throw new DBException(e, dataSource);
    }
}
Also used : 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) DBSProcedureType(org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType)

Example 12 with JDBCResultSet

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

the class H2MetaModel method getViewDDL.

@Override
public String getViewDDL(DBRProgressMonitor monitor, GenericTable sourceObject) throws DBException {
    GenericDataSource dataSource = sourceObject.getDataSource();
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read H2 view source")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS " + "WHERE TABLE_SCHEMA=? AND TABLE_NAME=?")) {
            dbStat.setString(1, sourceObject.getContainer().getName());
            dbStat.setString(2, sourceObject.getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.nextRow()) {
                    return dbResult.getString(1);
                }
                return "-- H2 view definition not found";
            }
        }
    } 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 13 with JDBCResultSet

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

the class H2MetaModel 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")) {
            List<GenericSequence> result = new ArrayList<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, "SEQUENCE_NAME");
                    if (name == null) {
                        continue;
                    }
                    String description = JDBCUtils.safeGetString(dbResult, "REMARKS");
                    GenericSequence sequence = new GenericSequence(container, name, description, JDBCUtils.safeGetLong(dbResult, "CURRENT_VALUE"), JDBCUtils.safeGetLong(dbResult, "MIN_VALUE"), JDBCUtils.safeGetLong(dbResult, "MAX_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 14 with JDBCResultSet

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

the class HSQLMetaModel method getViewDDL.

@Override
public String getViewDDL(DBRProgressMonitor monitor, GenericTable sourceObject) throws DBException {
    GenericDataSource dataSource = sourceObject.getDataSource();
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read HSQLDB view source")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS " + "WHERE TABLE_SCHEMA=? AND TABLE_NAME=?")) {
            dbStat.setString(1, sourceObject.getContainer().getName());
            dbStat.setString(2, sourceObject.getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.nextRow()) {
                    return dbResult.getString(1);
                }
                return "-- HSQLDB view definition not found";
            }
        }
    } 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 15 with JDBCResultSet

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

the class HSQLMetaModel method loadProcedures.

@Override
public void loadProcedures(DBRProgressMonitor monitor, @NotNull GenericObjectContainer container) throws DBException {
    GenericDataSource dataSource = container.getDataSource();
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Read HSQLDB procedure source")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA=?")) {
            dbStat.setString(1, container.getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.nextRow()) {
                    DBSProcedureType routineType = DBSProcedureType.PROCEDURE;
                    try {
                        routineType = DBSProcedureType.valueOf(JDBCUtils.safeGetString(dbResult, "ROUTINE_TYPE"));
                    } catch (IllegalArgumentException e) {
                        log.warn(e);
                    }
                    final GenericProcedure procedure = createProcedureImpl(container, JDBCUtils.safeGetString(dbResult, "ROUTINE_NAME"), JDBCUtils.safeGetString(dbResult, "SPECIFIC_NAME"), null, routineType, null);
                    container.addProcedure(procedure);
                }
            }
        }
    } 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) DBSProcedureType(org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType)

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