Search in sources :

Example 1 with AbstractObjectReference

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

the class PostgreStructureAssistant method findTablesByMask.

private void findTablesByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String tableNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.oid,x.relname,x.relnamespace,x.relkind FROM pg_catalog.pg_class x " + "WHERE x.relkind in('r','v','m') AND x.relname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND x.relnamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.relname LIMIT " + maxResults)) {
        dbStat.setString(1, tableNameMask);
        if (!CommonUtils.isEmpty(schema)) {
            PostgreUtils.setArrayParameter(dbStat, 2, schema);
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
                final long tableId = JDBCUtils.safeGetLong(dbResult, "oid");
                final String tableName = JDBCUtils.safeGetString(dbResult, "relname");
                final PostgreClass.RelKind tableType = PostgreClass.RelKind.valueOf(JDBCUtils.safeGetString(dbResult, "relkind"));
                final PostgreSchema tableSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                objects.add(new AbstractObjectReference(tableName, tableSchema, null, tableType == PostgreClass.RelKind.r ? PostgreTable.class : (tableType == PostgreClass.RelKind.v ? PostgreView.class : PostgreMaterializedView.class), RelationalObjectType.TYPE_TABLE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        PostgreTableBase table = tableSchema.getTable(monitor, tableId);
                        if (table == null) {
                            throw new DBException("Table '" + tableName + "' not found in schema '" + tableSchema.getName() + "'");
                        }
                        return table;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) 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)

Example 2 with AbstractObjectReference

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

the class PostgreStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String constrNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load constraints
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.oid,x.conname,x.connamespace FROM pg_catalog.pg_constraint x " + "WHERE x.conname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND x.connamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.conname LIMIT " + maxResults)) {
        dbStat.setString(1, constrNameMask);
        if (!CommonUtils.isEmpty(schema)) {
            PostgreUtils.setArrayParameter(dbStat, 2, schema);
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "connamespace");
                final long constrId = JDBCUtils.safeGetLong(dbResult, "oid");
                final String constrName = JDBCUtils.safeGetString(dbResult, "conname");
                final PostgreSchema constrSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                objects.add(new AbstractObjectReference(constrName, constrSchema, null, PostgreTableConstraintBase.class, RelationalObjectType.TYPE_TABLE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        final PostgreTableConstraintBase constraint = PostgreUtils.getObjectById(monitor, constrSchema.constraintCache, constrSchema, constrId);
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in schema '" + constrSchema.getName() + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
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 3 with AbstractObjectReference

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

the class PostgreStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String procNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load procedures
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT DISTINCT x.oid,x.proname,x.pronamespace FROM pg_catalog.pg_proc x " + "WHERE x.proname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND x.pronamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.proname LIMIT " + maxResults)) {
        dbStat.setString(1, procNameMask);
        if (!CommonUtils.isEmpty(schema)) {
            PostgreUtils.setArrayParameter(dbStat, 2, schema);
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "pronamespace");
                final String procName = JDBCUtils.safeGetString(dbResult, "proname");
                final long procId = JDBCUtils.safeGetLong(dbResult, "oid");
                final PostgreSchema procSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                objects.add(new AbstractObjectReference(procName, procSchema, null, PostgreProcedure.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        PostgreProcedure procedure = procSchema.getProcedure(monitor, procId);
                        if (procedure == null) {
                            throw new DBException("Procedure '" + procName + "' not found in schema '" + procSchema.getName() + "'");
                        }
                        return procedure;
                    }
                });
            }
        }
    }
}
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 4 with AbstractObjectReference

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

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 (objectType == OracleObjectType.TABLE) {
                oracleObjectTypes.add(OracleObjectType.VIEW);
                oracleObjectTypes.add(OracleObjectType.MATERIALIZED_VIEW);
            }
        }
    }
    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)
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " DISTINCT OWNER,OBJECT_NAME,OBJECT_TYPE FROM (SELECT OWNER,OBJECT_NAME,OBJECT_TYPE FROM ALL_OBJECTS WHERE " + "OBJECT_TYPE IN (" + objectTypeClause + ") AND OBJECT_NAME LIKE ? " + (schema == null ? "" : " AND OWNER=?") + "UNION ALL\n" + "SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " O.OWNER,O.OBJECT_NAME,O.OBJECT_TYPE\n" + "FROM ALL_SYNONYMS S,ALL_OBJECTS O\n" + "WHERE O.OWNER=S.TABLE_OWNER AND O.OBJECT_NAME=S.TABLE_NAME AND S.OWNER='PUBLIC' AND 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 != OracleObjectType.SYNONYM && objectType.isBrowsable() && oracleObjectTypes.contains(objectType)) {
                    OracleSchema objectSchema = 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;
                        }
                    });
                }
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) 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)

Example 5 with AbstractObjectReference

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

the class MySQLStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String procNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load procedures
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_ROUTINE_SCHEMA + "," + MySQLConstants.COL_ROUTINE_NAME + " FROM " + MySQLConstants.META_TABLE_ROUTINES + " WHERE " + MySQLConstants.COL_ROUTINE_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_ROUTINE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_ROUTINE_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, procNameMask.toLowerCase(Locale.ENGLISH));
        if (catalog != null) {
            dbStat.setString(2, catalog.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String catalogName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_ROUTINE_SCHEMA);
                final String procName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_ROUTINE_NAME);
                objects.add(new AbstractObjectReference(procName, dataSource.getCatalog(catalogName), null, MySQLProcedure.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog procCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (procCatalog == null) {
                            throw new DBException("Procedure catalog '" + catalogName + "' not found");
                        }
                        MySQLProcedure procedure = procCatalog.getProcedure(monitor, procName);
                        if (procedure == null) {
                            throw new DBException("Procedure '" + procName + "' not found in catalog '" + procCatalog.getName() + "'");
                        }
                        return procedure;
                    }
                });
            }
        }
    }
}
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)

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