Search in sources :

Example 1 with GenericMetaObject

use of org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject 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 2 with GenericMetaObject

use of org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject in project dbeaver by serge-rider.

the class GenericStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, GenericCatalog catalog, GenericSchema schema, String procNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    final GenericMetaObject procObject = getDataSource().getMetaObject(GenericConstants.OBJECT_PROCEDURE);
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try (JDBCResultSet dbResult = session.getMetaData().getProcedures(catalog == null ? null : catalog.getName(), schema == null ? null : schema.getName(), procNameMask)) {
        while (dbResult.next()) {
            if (monitor.isCanceled()) {
                break;
            }
            String catalogName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_CAT);
            String schemaName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_SCHEM);
            String procName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.PROCEDURE_NAME);
            String uniqueName = GenericUtils.safeGetStringTrimmed(procObject, dbResult, JDBCConstants.SPECIFIC_NAME);
            if (CommonUtils.isEmpty(procName)) {
                continue;
            }
            if (CommonUtils.isEmpty(uniqueName)) {
                uniqueName = procName;
            }
            objects.add(new ProcedureReference(findContainer(session.getProgressMonitor(), catalog, schema, catalogName, schemaName), catalogName, procName, uniqueName));
            if (objects.size() >= maxResults) {
                break;
            }
        }
    }
}
Also used : GenericMetaObject(org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 3 with GenericMetaObject

use of org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject in project dbeaver by serge-rider.

the class GenericStructureAssistant method findTablesByMask.

private void findTablesByMask(JDBCSession session, GenericCatalog catalog, GenericSchema schema, String tableNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    final GenericMetaObject tableObject = getDataSource().getMetaObject(GenericConstants.OBJECT_TABLE);
    final DBRProgressMonitor monitor = session.getProgressMonitor();
    try (JDBCResultSet dbResult = session.getMetaData().getTables(catalog == null ? null : catalog.getName(), schema == null ? null : schema.getName(), tableNameMask, null)) {
        while (dbResult.next()) {
            if (monitor.isCanceled()) {
                break;
            }
            String catalogName = GenericUtils.safeGetStringTrimmed(tableObject, dbResult, JDBCConstants.TABLE_CAT);
            String schemaName = GenericUtils.safeGetStringTrimmed(tableObject, dbResult, JDBCConstants.TABLE_SCHEM);
            String tableName = GenericUtils.safeGetStringTrimmed(tableObject, dbResult, JDBCConstants.TABLE_NAME);
            if (CommonUtils.isEmpty(tableName)) {
                continue;
            }
            objects.add(new TableReference(findContainer(session.getProgressMonitor(), catalog, schema, catalogName, schemaName), tableName, GenericUtils.safeGetString(tableObject, dbResult, JDBCConstants.REMARKS)));
            if (objects.size() >= maxResults) {
                break;
            }
        }
    }
}
Also used : GenericMetaObject(org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 4 with GenericMetaObject

use of org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject in project dbeaver by serge-rider.

the class GenericDataSource method initialize.

@Override
public void initialize(@NotNull DBRProgressMonitor monitor) throws DBException {
    super.initialize(monitor);
    Object omitCatalog = getContainer().getDriver().getDriverParameter(GenericConstants.PARAM_OMIT_CATALOG);
    Object omitTypeCache = getContainer().getDriver().getDriverParameter(GenericConstants.PARAM_OMIT_TYPE_CACHE);
    if (omitTypeCache == null || !CommonUtils.toBoolean(omitTypeCache)) {
        // Cache data types
        try {
            dataTypeCache.getAllObjects(monitor, this);
        } catch (DBException e) {
            log.warn("Can't fetch database data types", e);
        }
    } else {
        // Use basic data types
        dataTypeCache.fillStandardTypes(this);
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read generic metadata")) {
        // Read metadata
        JDBCDatabaseMetaData metaData = session.getMetaData();
        boolean catalogsFiltered = false;
        if (omitCatalog == null || !CommonUtils.toBoolean(omitCatalog)) {
            // Read catalogs
            monitor.subTask("Extract catalogs");
            monitor.worked(1);
            final GenericMetaObject catalogObject = getMetaObject(GenericConstants.OBJECT_CATALOG);
            final DBSObjectFilter catalogFilters = getContainer().getObjectFilter(GenericCatalog.class, null, false);
            final List<String> catalogNames = new ArrayList<>();
            try {
                try (JDBCResultSet dbResult = metaData.getCatalogs()) {
                    int totalCatalogs = 0;
                    while (dbResult.next()) {
                        String catalogName = GenericUtils.safeGetString(catalogObject, dbResult, JDBCConstants.TABLE_CAT);
                        if (CommonUtils.isEmpty(catalogName)) {
                            // Some drivers uses TABLE_QUALIFIER instead of catalog
                            catalogName = GenericUtils.safeGetStringTrimmed(catalogObject, dbResult, JDBCConstants.TABLE_QUALIFIER);
                            if (CommonUtils.isEmpty(catalogName)) {
                                continue;
                            }
                        }
                        totalCatalogs++;
                        if (catalogFilters == null || catalogFilters.matches(catalogName)) {
                            catalogNames.add(catalogName);
                            monitor.subTask("Extract catalogs - " + catalogName);
                        } else {
                            catalogsFiltered = true;
                        }
                        if (monitor.isCanceled()) {
                            break;
                        }
                    }
                    if (totalCatalogs == 1) {
                        // Just one catalog. Looks like DB2 or PostgreSQL
                        // Let's just skip it and use only schemas
                        // It's ok to use "%" instead of catalog name anyway
                        catalogNames.clear();
                    }
                }
            } catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
                // Just skip it
                log.debug(e);
            } catch (SQLException e) {
                // Error reading catalogs - just warn about it
                log.warn("Can't read catalog list", e);
            }
            if (!catalogNames.isEmpty() || catalogsFiltered) {
                this.catalogs = new ArrayList<>();
                for (String catalogName : catalogNames) {
                    GenericCatalog catalog = new GenericCatalog(this, catalogName);
                    this.catalogs.add(catalog);
                }
            }
        }
        if (CommonUtils.isEmpty(catalogs) && !catalogsFiltered) {
            // Catalogs not supported - try to read root schemas
            monitor.subTask("Extract schemas");
            monitor.worked(1);
            List<GenericSchema> tmpSchemas = loadSchemas(session, null);
            if (tmpSchemas != null) {
                this.schemas = tmpSchemas;
            }
            if (CommonUtils.isEmpty(schemas)) {
                this.structureContainer = new DataSourceObjectContainer();
            }
        }
        determineSelectedEntity(session);
    } catch (SQLException ex) {
        throw new DBException("Error reading metadata", ex, this);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) GenericMetaObject(org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject) ArrayList(java.util.ArrayList) GenericMetaObject(org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)

Example 5 with GenericMetaObject

use of org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject in project dbeaver by serge-rider.

the class GenericDataSource method loadSchemas.

List<GenericSchema> loadSchemas(JDBCSession session, GenericCatalog catalog) throws DBException {
    try {
        final GenericMetaObject schemaObject = getMetaObject(GenericConstants.OBJECT_SCHEMA);
        final DBSObjectFilter schemaFilters = getContainer().getObjectFilter(GenericSchema.class, null, false);
        final List<GenericSchema> tmpSchemas = new ArrayList<>();
        JDBCResultSet dbResult = null;
        boolean catalogSchemas = false;
        if (catalog != null) {
            try {
                dbResult = session.getMetaData().getSchemas(catalog.getName(), schemaFilters != null && schemaFilters.hasSingleMask() ? schemaFilters.getSingleMask() : getAllObjectsPattern());
                catalogSchemas = true;
            } catch (Throwable e) {
                // This method not supported (may be old driver version)
                // Use general schema reading method
                log.debug("Error reading schemas in catalog '" + catalog.getName() + "' - " + e.getMessage());
            }
        }
        if (dbResult == null) {
            dbResult = session.getMetaData().getSchemas();
        }
        try {
            while (dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                String schemaName = GenericUtils.safeGetString(schemaObject, dbResult, JDBCConstants.TABLE_SCHEM);
                if (CommonUtils.isEmpty(schemaName)) {
                    // some drivers uses TABLE_OWNER column instead of TABLE_SCHEM
                    schemaName = GenericUtils.safeGetString(schemaObject, dbResult, JDBCConstants.TABLE_OWNER);
                }
                if (CommonUtils.isEmpty(schemaName)) {
                    continue;
                }
                if (schemaFilters != null && !schemaFilters.matches(schemaName)) {
                    // Doesn't match filter
                    continue;
                }
                String catalogName = GenericUtils.safeGetString(schemaObject, dbResult, JDBCConstants.TABLE_CATALOG);
                if (!CommonUtils.isEmpty(catalogName)) {
                    if (catalog == null) {
                        // Invalid schema's catalog or schema without catalog (then do not use schemas as structure)
                        log.debug("Catalog name (" + catalogName + ") found for schema '" + schemaName + "' while schema doesn't have parent catalog");
                    } else if (!catalog.getName().equals(catalogName)) {
                        if (!catalogSchemas) {
                            // Just skip it - we have list of all existing schemas and this one belongs to another catalog
                            continue;
                        }
                        log.debug("Catalog name '" + catalogName + "' differs from schema's catalog '" + catalog.getName() + "'");
                    }
                }
                session.getProgressMonitor().subTask("Schema " + schemaName);
                GenericSchema schema;
                if (catalog == null) {
                    schema = new GenericSchema(this, schemaName);
                } else {
                    schema = new GenericSchema(catalog, schemaName);
                }
                tmpSchemas.add(schema);
            }
        } finally {
            dbResult.close();
        }
        if (catalog == null && tmpSchemas.size() == 1 && (schemaFilters == null || schemaFilters.isNotApplicable())) {
            // Only one schema and no catalogs
            // Most likely it is a fake one, let's skip it
            // Anyway using "%" instead is ok
            tmpSchemas.clear();
        }
        return tmpSchemas;
    } catch (UnsupportedOperationException e) {
        // Schemas are not supported
        log.debug(e);
        return null;
    } catch (SQLFeatureNotSupportedException e) {
        // Schemas are not supported
        log.debug(e);
        return null;
    } catch (Exception ex) {
        // Schemas do not supported - just ignore this error
        log.warn("Can't read schema list", ex);
        return null;
    }
}
Also used : GenericMetaObject(org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject) ArrayList(java.util.ArrayList) DBException(org.jkiss.dbeaver.DBException)

Aggregations

GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)7 DBException (org.jkiss.dbeaver.DBException)4 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)4 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)2 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)2 Matcher (java.util.regex.Matcher)1 GenericMetaColumn (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaColumn)1 JDBCDatabaseMetaData (org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData)1 DBSForeignKeyDefferability (org.jkiss.dbeaver.model.struct.rdb.DBSForeignKeyDefferability)1 DBSForeignKeyModifyRule (org.jkiss.dbeaver.model.struct.rdb.DBSForeignKeyModifyRule)1 DBSProcedureParameterKind (org.jkiss.dbeaver.model.struct.rdb.DBSProcedureParameterKind)1