Search in sources :

Example 1 with DBSProcedureType

use of org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType 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 2 with DBSProcedureType

use of org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType 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

SQLException (java.sql.SQLException)2 DBException (org.jkiss.dbeaver.DBException)2 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)2 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)2 DBSProcedureType (org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType)2 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)1