Search in sources :

Example 26 with AbstractObjectReference

use of org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference in project dbeaver by serge-rider.

the class OracleStructureAssistant method searchAllObjects.

private void searchAllObjects(final JDBCSession session, final OracleSchema schema, String objectNameMask, DBSObjectType[] objectTypes, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    StringBuilder objectTypeClause = new StringBuilder(100);
    final List<OracleObjectType> oracleObjectTypes = new ArrayList<>(objectTypes.length + 2);
    for (DBSObjectType objectType : objectTypes) {
        if (objectType instanceof OracleObjectType) {
            oracleObjectTypes.add((OracleObjectType) objectType);
            if (objectType == OracleObjectType.PROCEDURE) {
                oracleObjectTypes.add(OracleObjectType.FUNCTION);
            }
        } else if (DBSProcedure.class.isAssignableFrom(objectType.getTypeClass())) {
            oracleObjectTypes.add(OracleObjectType.FUNCTION);
            oracleObjectTypes.add(OracleObjectType.PROCEDURE);
        }
    }
    oracleObjectTypes.add(OracleObjectType.SYNONYM);
    for (OracleObjectType objectType : oracleObjectTypes) {
        if (objectTypeClause.length() > 0)
            objectTypeClause.append(",");
        objectTypeClause.append("'").append(objectType.getTypeName()).append("'");
    }
    if (objectTypeClause.length() == 0) {
        return;
    }
    // Always search for synonyms
    objectTypeClause.append(",'").append(OracleObjectType.SYNONYM.getTypeName()).append("'");
    // Seek for objects (join with public synonyms)
    OracleDataSource dataSource = (OracleDataSource) session.getDataSource();
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint(dataSource) + " DISTINCT OWNER,OBJECT_NAME,OBJECT_TYPE FROM " + "   (SELECT OWNER,OBJECT_NAME,OBJECT_TYPE FROM " + OracleUtils.getAdminAllViewPrefix(session.getProgressMonitor(), dataSource, "OBJECTS") + " WHERE " + "OBJECT_TYPE IN (" + objectTypeClause + ") AND " + (!caseSensitive ? "UPPER(OBJECT_NAME)" : "OBJECT_NAME") + " LIKE ? " + (schema == null ? "" : " AND OWNER=?") + "UNION ALL\n" + "SELECT " + OracleUtils.getSysCatalogHint(dataSource) + " O.OWNER,O.OBJECT_NAME,O.OBJECT_TYPE\n" + "FROM " + OracleUtils.getAdminAllViewPrefix(session.getProgressMonitor(), dataSource, "SYNONYMS") + " S," + OracleUtils.getAdminAllViewPrefix(session.getProgressMonitor(), dataSource, "OBJECTS") + " O\n" + "WHERE O.OWNER=S.TABLE_OWNER AND O.OBJECT_NAME=S.TABLE_NAME AND S.OWNER='PUBLIC' AND " + (!caseSensitive ? "UPPER(S.SYNONYM_NAME)" : "S.SYNONYM_NAME") + "  LIKE ?)" + "\nORDER BY OBJECT_NAME")) {
        if (!caseSensitive) {
            objectNameMask = objectNameMask.toUpperCase();
        }
        dbStat.setString(1, objectNameMask);
        if (schema != null) {
            dbStat.setString(2, schema.getName());
        }
        dbStat.setString(schema != null ? 3 : 2, objectNameMask);
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (objects.size() < maxResults && dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "OWNER");
                final String objectName = JDBCUtils.safeGetString(dbResult, "OBJECT_NAME");
                final String objectTypeName = JDBCUtils.safeGetString(dbResult, "OBJECT_TYPE");
                final OracleObjectType objectType = OracleObjectType.getByType(objectTypeName);
                if (objectType != null && objectType.isBrowsable() && oracleObjectTypes.contains(objectType)) {
                    OracleSchema objectSchema = this.dataSource.getSchema(session.getProgressMonitor(), schemaName);
                    if (objectSchema == null) {
                        log.debug("Schema '" + schemaName + "' not found. Probably was filtered");
                        continue;
                    }
                    objects.add(new AbstractObjectReference(objectName, objectSchema, null, objectType.getTypeClass(), objectType) {

                        @Override
                        public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                            OracleSchema tableSchema = (OracleSchema) getContainer();
                            DBSObject object = objectType.findObject(session.getProgressMonitor(), tableSchema, objectName);
                            if (object == null) {
                                throw new DBException(objectTypeName + " '" + objectName + "' not found in schema '" + tableSchema.getName() + "'");
                            }
                            return object;
                        }

                        @NotNull
                        @Override
                        public String getFullyQualifiedName(DBPEvaluationContext context) {
                            if (objectType == OracleObjectType.SYNONYM && OracleConstants.USER_PUBLIC.equals(schemaName)) {
                                return DBUtils.getQuotedIdentifier(dataSource, objectName);
                            }
                            return super.getFullyQualifiedName(context);
                        }
                    });
                }
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) ArrayList(java.util.ArrayList) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) NotNull(org.jkiss.code.NotNull) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSProcedure(org.jkiss.dbeaver.model.struct.rdb.DBSProcedure)

Example 27 with AbstractObjectReference

use of org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference in project dbeaver by serge-rider.

the class HANAStructureAssistant method findViewColumnsByMask.

private void findViewColumnsByMask(JDBCSession session, GenericSchema parentSchema, String objectNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> result) throws SQLException, DBException {
    String stmt = "SELECT SCHEMA_NAME, VIEW_NAME, COLUMN_NAME, COMMENTS FROM SYS.VIEW_COLUMNS WHERE";
    stmt += caseSensitive ? " COLUMN_NAME LIKE ?" : " UPPER(COLUMN_NAME) LIKE ?";
    if (parentSchema != null)
        stmt += " AND SCHEMA_NAME = ?";
    stmt += " ORDER BY SCHEMA_NAME, VIEW_NAME, COLUMN_NAME LIMIT " + maxResults;
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try (JDBCPreparedStatement dbStat = session.prepareStatement(stmt)) {
        dbStat.setString(1, caseSensitive ? objectNameMask : objectNameMask.toUpperCase());
        if (parentSchema != null)
            dbStat.setString(2, parentSchema.getName());
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int numResults = maxResults;
            while (dbResult.next() && numResults-- > 0) {
                if (monitor.isCanceled())
                    break;
                String schemaName = dbResult.getString(1);
                String objectName = dbResult.getString(2);
                String columnName = dbResult.getString(3);
                String description = dbResult.getString(4);
                GenericSchema schema = parentSchema != null ? parentSchema : dataSource.getSchema(schemaName);
                if (schema == null)
                    // filtered
                    continue;
                result.add(new AbstractObjectReference(objectName, schema, description, GenericTableColumn.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        GenericTableBase object = ((GenericObjectContainer) getContainer()).getTable(monitor, getName());
                        if (object == null) {
                            throw new DBException("Can't find object '" + getName() + "' in '" + DBUtils.getFullQualifiedName(dataSource, getContainer()) + "'");
                        }
                        GenericTableColumn column = object.getAttribute(monitor, columnName);
                        if (column == null) {
                            throw new DBException("Column '" + columnName + "' not found in table '" + object.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return column;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 28 with AbstractObjectReference

use of org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference in project dbeaver by serge-rider.

the class HANAStructureAssistant method findViewsByMask.

private void findViewsByMask(JDBCSession session, GenericSchema parentSchema, String objectNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> result) throws SQLException, DBException {
    String stmt = "SELECT SCHEMA_NAME, VIEW_NAME, COMMENTS FROM SYS.VIEWS WHERE";
    stmt += caseSensitive ? " VIEW_NAME LIKE ?" : " UPPER(VIEW_NAME) LIKE ?";
    if (parentSchema != null)
        stmt += " AND SCHEMA_NAME = ?";
    stmt += " ORDER BY SCHEMA_NAME, VIEW_NAME LIMIT " + maxResults;
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try (JDBCPreparedStatement dbStat = session.prepareStatement(stmt)) {
        dbStat.setString(1, caseSensitive ? objectNameMask : objectNameMask.toUpperCase());
        if (parentSchema != null)
            dbStat.setString(2, parentSchema.getName());
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int numResults = maxResults;
            while (dbResult.next() && numResults-- > 0) {
                if (monitor.isCanceled())
                    break;
                String schemaName = dbResult.getString(1);
                String objectName = dbResult.getString(2);
                String description = dbResult.getString(3);
                GenericSchema schema = parentSchema != null ? parentSchema : dataSource.getSchema(schemaName);
                if (schema == null)
                    // filtered
                    continue;
                result.add(new AbstractObjectReference(objectName, schema, description, GenericTable.class, RelationalObjectType.TYPE_VIEW) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        GenericTableBase object = ((GenericObjectContainer) getContainer()).getTable(monitor, getName());
                        if (object == null) {
                            throw new DBException("Can't find object '" + getName() + "' in '" + DBUtils.getFullQualifiedName(dataSource, getContainer()) + "'");
                        }
                        return object;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 29 with AbstractObjectReference

use of org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference in project dbeaver by serge-rider.

the class HANAStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, GenericSchema parentSchema, String objectNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> result) throws SQLException, DBException {
    String stmt = "SELECT SCHEMA_NAME, PROCEDURE_NAME FROM SYS.PROCEDURES WHERE";
    stmt += caseSensitive ? " PROCEDURE_NAME LIKE ?" : " UPPER(PROCEDURE_NAME) LIKE ?";
    if (parentSchema != null)
        stmt += " AND SCHEMA_NAME = ?";
    stmt += " ORDER BY SCHEMA_NAME, PROCEDURE_NAME LIMIT " + maxResults;
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try (JDBCPreparedStatement dbStat = session.prepareStatement(stmt)) {
        dbStat.setString(1, caseSensitive ? objectNameMask : objectNameMask.toUpperCase());
        if (parentSchema != null)
            dbStat.setString(2, parentSchema.getName());
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int numResults = maxResults;
            while (dbResult.next() && numResults-- > 0) {
                if (monitor.isCanceled())
                    break;
                String schemaName = dbResult.getString(1);
                String objectName = dbResult.getString(2);
                String description = null;
                GenericSchema schema = parentSchema != null ? parentSchema : dataSource.getSchema(schemaName);
                if (schema == null)
                    // filtered
                    continue;
                result.add(new AbstractObjectReference(objectName, schema, description, GenericProcedure.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        GenericProcedure object = ((GenericObjectContainer) getContainer()).getProcedure(monitor, getName());
                        if (object == null) {
                            throw new DBException("Can't find object '" + getName() + "' in '" + DBUtils.getFullQualifiedName(dataSource, getContainer()) + "'");
                        }
                        return object;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 30 with AbstractObjectReference

use of org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference in project dbeaver by serge-rider.

the class SQLServerStructureAssistant method searchAllObjects.

private void searchAllObjects(final JDBCSession session, final SQLServerDatabase database, final SQLServerSchema schema, String objectNameMask, DBSObjectType[] objectTypes, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws DBException {
    final List<SQLServerObjectType> supObjectTypes = new ArrayList<>(objectTypes.length + 2);
    for (DBSObjectType objectType : objectTypes) {
        if (objectType instanceof SQLServerObjectType) {
            supObjectTypes.add((SQLServerObjectType) objectType);
        } else if (objectType == RelationalObjectType.TYPE_PROCEDURE) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerProcedure.class));
        } else if (objectType == RelationalObjectType.TYPE_TABLE) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerTable.class));
        } else if (objectType == RelationalObjectType.TYPE_CONSTRAINT) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerTableCheckConstraint.class));
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerTableForeignKey.class));
        } else if (objectType == RelationalObjectType.TYPE_VIEW) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerView.class));
        }
    }
    if (supObjectTypes.isEmpty()) {
        return;
    }
    StringBuilder objectTypeClause = new StringBuilder(100);
    for (SQLServerObjectType objectType : supObjectTypes) {
        if (objectTypeClause.length() > 0)
            objectTypeClause.append(",");
        objectTypeClause.append("'").append(objectType.getTypeID()).append("'");
    }
    if (objectTypeClause.length() == 0) {
        return;
    }
    // Seek for objects (join with public synonyms)
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM " + SQLServerUtils.getSystemTableName(database, "all_objects") + " o " + "\nWHERE o.type IN (" + objectTypeClause.toString() + ") AND o.name LIKE ?" + (schema == null ? "" : " AND o.schema_id=? ") + "\nORDER BY o.name")) {
        dbStat.setString(1, objectNameMask);
        if (schema != null) {
            dbStat.setLong(2, schema.getObjectId());
        }
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (objects.size() < maxResults && dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "schema_id");
                final String objectName = JDBCUtils.safeGetString(dbResult, "name");
                final String objectTypeName = JDBCUtils.safeGetStringTrimmed(dbResult, "type");
                final SQLServerObjectType objectType = SQLServerObjectType.valueOf(objectTypeName);
                {
                    SQLServerSchema objectSchema = schemaId == 0 ? null : database.getSchema(session.getProgressMonitor(), schemaId);
                    objects.add(new AbstractObjectReference(objectName, objectSchema != null ? objectSchema : database, null, objectType.getTypeClass(), objectType) {

                        @Override
                        public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                            DBSObject object = objectType.findObject(session.getProgressMonitor(), database, objectSchema, objectName);
                            if (object == null) {
                                throw new DBException(objectTypeName + " '" + objectName + "' not found");
                            }
                            return object;
                        }
                    });
                }
            }
        }
    } catch (Throwable e) {
        throw new DBException("Error while searching in system catalog", e, dataSource);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Aggregations

DBException (org.jkiss.dbeaver.DBException)34 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)34 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)34 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)34 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)30 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)30 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)4 NotNull (org.jkiss.code.NotNull)3 DBPEvaluationContext (org.jkiss.dbeaver.model.DBPEvaluationContext)3 ArrayList (java.util.ArrayList)2 DBSObjectType (org.jkiss.dbeaver.model.struct.DBSObjectType)1 DBSProcedure (org.jkiss.dbeaver.model.struct.rdb.DBSProcedure)1