Search in sources :

Example 1 with DBPEvaluationContext

use of org.jkiss.dbeaver.model.DBPEvaluationContext in project dbeaver by serge-rider.

the class MySQLStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String constrNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load columns
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + "," + MySQLConstants.COL_COLUMN_NAME + " FROM " + MySQLConstants.META_TABLE_COLUMNS + " WHERE " + MySQLConstants.COL_COLUMN_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_COLUMN_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, constrNameMask.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_TABLE_SCHEMA);
                final String tableName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_NAME);
                final String columnName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_NAME);
                objects.add(new AbstractObjectReference(columnName, dataSource.getCatalog(catalogName), null, MySQLTableColumn.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @NotNull
                    @Override
                    public String getFullyQualifiedName(DBPEvaluationContext context) {
                        return DBUtils.getQuotedIdentifier(dataSource, catalogName) + '.' + DBUtils.getQuotedIdentifier(dataSource, tableName) + '.' + DBUtils.getQuotedIdentifier(dataSource, columnName);
                    }

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (tableCatalog == null) {
                            throw new DBException("Column catalog '" + catalogName + "' not found");
                        }
                        MySQLTableBase table = tableCatalog.getTableCache().getObject(monitor, tableCatalog, tableName);
                        if (table == null) {
                            throw new DBException("Column table '" + tableName + "' not found in catalog '" + tableCatalog.getName() + "'");
                        }
                        MySQLTableColumn column = table.getAttribute(monitor, columnName);
                        if (column == null) {
                            throw new DBException("Column '" + columnName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return column;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) NotNull(org.jkiss.code.NotNull) 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 DBPEvaluationContext

use of org.jkiss.dbeaver.model.DBPEvaluationContext 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 (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 3 with DBPEvaluationContext

use of org.jkiss.dbeaver.model.DBPEvaluationContext 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 4 with DBPEvaluationContext

use of org.jkiss.dbeaver.model.DBPEvaluationContext in project dbeaver by dbeaver.

the class MySQLStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String constrNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load columns
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + "," + MySQLConstants.COL_COLUMN_NAME + " FROM " + MySQLConstants.META_TABLE_COLUMNS + " WHERE " + MySQLConstants.COL_COLUMN_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_COLUMN_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, constrNameMask.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_TABLE_SCHEMA);
                final String tableName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_NAME);
                final String columnName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_NAME);
                objects.add(new AbstractObjectReference(columnName, dataSource.getCatalog(catalogName), null, MySQLTableColumn.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @NotNull
                    @Override
                    public String getFullyQualifiedName(DBPEvaluationContext context) {
                        return DBUtils.getQuotedIdentifier(dataSource, catalogName) + '.' + DBUtils.getQuotedIdentifier(dataSource, tableName) + '.' + DBUtils.getQuotedIdentifier(dataSource, columnName);
                    }

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (tableCatalog == null) {
                            throw new DBException("Column catalog '" + catalogName + "' not found");
                        }
                        MySQLTableBase table = tableCatalog.getTableCache().getObject(monitor, tableCatalog, tableName);
                        if (table == null) {
                            throw new DBException("Column table '" + tableName + "' not found in catalog '" + tableCatalog.getName() + "'");
                        }
                        MySQLTableColumn column = table.getAttribute(monitor, columnName);
                        if (column == null) {
                            throw new DBException("Column '" + columnName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return column;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) NotNull(org.jkiss.code.NotNull) 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

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