Search in sources :

Example 1 with DB2ObjectType

use of org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType in project dbeaver by serge-rider.

the class DB2StructureAssistant 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<DB2ObjectType> db2ObjectTypes = new ArrayList<>(objectTypes.length);
    for (DBSObjectType dbsObjectType : objectTypes) {
        db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
    }
    DB2Schema schema = parentObject instanceof DB2Schema ? (DB2Schema) parentObject : null;
    if (schema == null && !globalSearch) {
        schema = dataSource.getDefaultObject();
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Find objects by name")) {
        return searchAllObjects(session, schema, objectNameMask, db2ObjectTypes, caseSensitive, maxResults);
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : 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) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) NotNull(org.jkiss.code.NotNull)

Example 2 with DB2ObjectType

use of org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType in project dbeaver by serge-rider.

the class DB2GranteeAuthCache method fetchObject.

@Override
protected DB2AuthBase fetchObject(@NotNull JDBCSession session, @NotNull DB2Grantee db2Grantee, @NotNull JDBCResultSet resultSet) throws SQLException, DBException {
    DB2DataSource db2DataSource = db2Grantee.getDataSource();
    DBRProgressMonitor monitor = session.getProgressMonitor();
    String objectSchemaName = JDBCUtils.safeGetStringTrimmed(resultSet, "OBJ_SCHEMA");
    String objectName = JDBCUtils.safeGetStringTrimmed(resultSet, "OBJ_NAME");
    DB2ObjectType objectType = CommonUtils.valueOf(DB2ObjectType.class, JDBCUtils.safeGetString(resultSet, "OBJ_TYPE"));
    switch(objectType) {
        case COLUMN:
            String columnName = JDBCUtils.safeGetStringTrimmed(resultSet, "USAGEAUTH");
            DB2TableColumn db2TableColumn = DB2Utils.findColumnBySchemaNameAndTableNameAndName(monitor, db2DataSource, objectSchemaName, objectName, columnName);
            return new DB2AuthColumn(monitor, db2Grantee, db2TableColumn, resultSet);
        case INDEX:
            DB2Index db2Index = DB2Utils.findIndexBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthIndex(monitor, db2Grantee, db2Index, resultSet);
        case MODULE:
            DB2Module db2Module = DB2Utils.findModuleBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthModule(monitor, db2Grantee, db2Module, resultSet);
        case PACKAGE:
            DB2Package db2Package = DB2Utils.findPackageBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthPackage(monitor, db2Grantee, db2Package, resultSet);
        case PROCEDURE:
            // Can be a Function or a Procedure
            DB2RoutineType routineType = CommonUtils.valueOf(DB2RoutineType.class, JDBCUtils.safeGetStringTrimmed(resultSet, "USAGEAUTH"));
            switch(routineType) {
                case F:
                    DB2Routine db2Udf = DB2Utils.findUDFBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthUDF(monitor, db2Grantee, db2Udf, resultSet);
                case M:
                    DB2Routine db2Method = DB2Utils.findMethodBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthMethod(monitor, db2Grantee, db2Method, resultSet);
                case P:
                    DB2Routine db2Procedure = DB2Utils.findProcedureBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthProcedure(monitor, db2Grantee, db2Procedure, resultSet);
                default:
                    throw new DBException(routineType + " is not a valid DB2RoutineType");
            }
        case SCHEMA:
            DB2Schema db2Schema = db2DataSource.getSchema(monitor, objectName);
            return new DB2AuthSchema(monitor, db2Grantee, db2Schema, resultSet);
        case SEQUENCE:
            DB2Sequence db2Sequence = DB2Utils.findSequenceBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthSequence(monitor, db2Grantee, db2Sequence, resultSet);
        case TABLE:
            // Can be a Table, a View or an MQT..
            DB2TableBase db2TableBase = DB2Utils.findTableBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            if (db2TableBase != null) {
                return new DB2AuthTable(monitor, db2Grantee, db2TableBase, resultSet);
            } else {
                db2TableBase = DB2Utils.findViewBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                if (db2TableBase != null) {
                    return new DB2AuthView(monitor, db2Grantee, db2TableBase, resultSet);
                } else {
                    db2TableBase = DB2Utils.findMQTBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthMaterializedQueryTable(monitor, db2Grantee, db2TableBase, resultSet);
                }
            }
        case TABLESPACE:
            DB2Tablespace db2Tablespace = db2DataSource.getTablespace(monitor, objectName);
            return new DB2AuthTablespace(monitor, db2Grantee, db2Tablespace, resultSet);
        case VARIABLE:
            DB2Variable db2Variable = db2DataSource.getVariable(monitor, objectName);
            return new DB2AuthVariable(monitor, db2Grantee, db2Variable, resultSet);
        case XML_SCHEMA:
            Long xmlSchemaId = Long.valueOf(objectName);
            DB2XMLSchema db2XmlSchema = DB2Utils.findXMLSchemaByById(monitor, db2DataSource, xmlSchemaId);
            return new DB2AuthXMLSchema(monitor, db2Grantee, db2XmlSchema, resultSet);
        default:
            throw new DBException("Programming error: " + objectType + " is not supported yet and the SELECT statement must exclude it");
    }
}
Also used : DB2Routine(org.jkiss.dbeaver.ext.db2.model.DB2Routine) DBException(org.jkiss.dbeaver.DBException) DB2Sequence(org.jkiss.dbeaver.ext.db2.model.DB2Sequence) DB2ObjectType(org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType) DB2TableBase(org.jkiss.dbeaver.ext.db2.model.DB2TableBase) DB2Index(org.jkiss.dbeaver.ext.db2.model.DB2Index) DB2Package(org.jkiss.dbeaver.ext.db2.model.DB2Package) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) DB2Tablespace(org.jkiss.dbeaver.ext.db2.model.DB2Tablespace) DB2DataSource(org.jkiss.dbeaver.ext.db2.model.DB2DataSource) DB2XMLSchema(org.jkiss.dbeaver.ext.db2.model.DB2XMLSchema) DB2TableColumn(org.jkiss.dbeaver.ext.db2.model.DB2TableColumn) DB2Module(org.jkiss.dbeaver.ext.db2.model.module.DB2Module) DB2RoutineType(org.jkiss.dbeaver.ext.db2.model.dict.DB2RoutineType) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DB2Variable(org.jkiss.dbeaver.ext.db2.model.DB2Variable)

Example 3 with DB2ObjectType

use of org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType in project dbeaver by serge-rider.

the class DB2StructureAssistant method searchColumns.

private void searchColumns(JDBCSession session, DB2Schema schema, String searchObjectNameMask, List<DB2ObjectType> objectTypes, int maxResults, List<DBSObjectReference> objects, int nbResults) throws SQLException, DBException {
    String sql;
    if (schema != null) {
        sql = SQL_COLS_SCHEMA;
    } else {
        sql = SQL_COLS_ALL;
    }
    int n = 1;
    try (JDBCPreparedStatement dbStat = session.prepareStatement(sql)) {
        if (schema != null) {
            dbStat.setString(n++, schema.getName());
        }
        dbStat.setString(n++, searchObjectNameMask);
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        String tableSchemaName;
        String tableOrViewName;
        String columnName;
        DB2Schema db2Schema;
        DB2Table db2Table;
        DB2View db2View;
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                if (nbResults++ >= maxResults) {
                    return;
                }
                tableSchemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABSCHEMA");
                tableOrViewName = JDBCUtils.safeGetString(dbResult, "TABNAME");
                columnName = JDBCUtils.safeGetString(dbResult, "COLNAME");
                db2Schema = dataSource.getSchema(session.getProgressMonitor(), tableSchemaName);
                if (db2Schema == null) {
                    LOG.debug("Schema '" + tableSchemaName + "' not found. Probably was filtered");
                    continue;
                }
                // Try with table, then view
                db2Table = db2Schema.getTable(session.getProgressMonitor(), tableOrViewName);
                if (db2Table != null) {
                    objects.add(new DB2ObjectReference(columnName, db2Table, DB2ObjectType.COLUMN));
                } else {
                    db2View = db2Schema.getView(session.getProgressMonitor(), tableOrViewName);
                    if (db2View != null) {
                        objects.add(new DB2ObjectReference(columnName, db2View, DB2ObjectType.COLUMN));
                    }
                }
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DB2Table(org.jkiss.dbeaver.ext.db2.model.DB2Table) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DB2View(org.jkiss.dbeaver.ext.db2.model.DB2View) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema)

Example 4 with DB2ObjectType

use of org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType in project dbeaver by serge-rider.

the class DB2StructureAssistant method searchTables.

// --------------
// Helper Classes
// --------------
private void searchTables(JDBCSession session, DB2Schema schema, String searchObjectNameMask, List<DB2ObjectType> db2ObjectTypes, int maxResults, List<DBSObjectReference> objects, int nbResults) throws SQLException, DBException {
    String baseSQL;
    if (schema != null) {
        baseSQL = "SELECT TABSCHEMA,TABNAME,TYPE FROM SYSCAT.TABLES\n" + "WHERE TABSCHEMA =? AND TABNAME LIKE ? AND TYPE IN (%s)\n" + "WITH UR";
    } else {
        baseSQL = "SELECT TABSCHEMA,TABNAME,TYPE FROM SYSCAT.TABLES\n" + "WHERE TABNAME LIKE ? AND TYPE IN (%s)\n" + "WITH UR";
    }
    String sql = buildTableSQL(baseSQL, db2ObjectTypes);
    int n = 1;
    try (JDBCPreparedStatement dbStat = session.prepareStatement(sql)) {
        if (schema != null) {
            dbStat.setString(n++, schema.getName());
        //dbStat.setString(n++, DB2Constants.SYSTEM_CATALOG_SCHEMA);
        }
        dbStat.setString(n++, searchObjectNameMask);
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        String schemaName;
        String objectName;
        DB2Schema db2Schema;
        DB2TableType tableType;
        DB2ObjectType objectType;
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                if (nbResults++ >= maxResults) {
                    break;
                }
                schemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABSCHEMA");
                objectName = JDBCUtils.safeGetString(dbResult, "TABNAME");
                tableType = CommonUtils.valueOf(DB2TableType.class, JDBCUtils.safeGetString(dbResult, "TYPE"));
                db2Schema = dataSource.getSchema(session.getProgressMonitor(), schemaName);
                if (db2Schema == null) {
                    LOG.debug("Schema '" + schemaName + "' not found. Probably was filtered");
                    continue;
                }
                objectType = tableType.getDb2ObjectType();
                objects.add(new DB2ObjectReference(objectName, db2Schema, objectType));
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) DB2TableType(org.jkiss.dbeaver.ext.db2.model.dict.DB2TableType)

Aggregations

DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)4 DBException (org.jkiss.dbeaver.DBException)2 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)2 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 NotNull (org.jkiss.code.NotNull)1 DB2ObjectType (org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType)1 DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)1 DB2Index (org.jkiss.dbeaver.ext.db2.model.DB2Index)1 DB2Package (org.jkiss.dbeaver.ext.db2.model.DB2Package)1 DB2Routine (org.jkiss.dbeaver.ext.db2.model.DB2Routine)1 DB2Sequence (org.jkiss.dbeaver.ext.db2.model.DB2Sequence)1 DB2Table (org.jkiss.dbeaver.ext.db2.model.DB2Table)1 DB2TableBase (org.jkiss.dbeaver.ext.db2.model.DB2TableBase)1 DB2TableColumn (org.jkiss.dbeaver.ext.db2.model.DB2TableColumn)1 DB2Tablespace (org.jkiss.dbeaver.ext.db2.model.DB2Tablespace)1 DB2Variable (org.jkiss.dbeaver.ext.db2.model.DB2Variable)1 DB2View (org.jkiss.dbeaver.ext.db2.model.DB2View)1 DB2XMLSchema (org.jkiss.dbeaver.ext.db2.model.DB2XMLSchema)1