Search in sources :

Example 21 with AbstractObjectReference

use of org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference 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)

Example 22 with AbstractObjectReference

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

the class OracleStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, final OracleSchema schema, String constrNameMask, DBSObjectType[] objectTypes, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    List<DBSObjectType> objectTypesList = Arrays.asList(objectTypes);
    final boolean hasFK = objectTypesList.contains(OracleObjectType.FOREIGN_KEY);
    final boolean hasConstraints = objectTypesList.contains(OracleObjectType.CONSTRAINT);
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " OWNER, TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE\n" + "FROM SYS.ALL_CONSTRAINTS\n" + "WHERE CONSTRAINT_NAME like ?" + (!hasFK ? " AND CONSTRAINT_TYPE<>'R'" : "") + (schema != null ? " AND OWNER=?" : ""))) {
        dbStat.setString(1, constrNameMask);
        if (schema != null) {
            dbStat.setString(2, schema.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_OWNER);
                final String tableName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_TABLE_NAME);
                final String constrName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_NAME);
                final String constrType = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_TYPE);
                final DBSEntityConstraintType type = OracleTableConstraint.getConstraintType(constrType);
                objects.add(new AbstractObjectReference(constrName, dataSource.getSchema(session.getProgressMonitor(), schemaName), null, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleTableForeignKey.class : OracleTableConstraint.class, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleObjectType.FOREIGN_KEY : OracleObjectType.CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        OracleSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Constraint schema '" + schemaName + "' not found");
                        }
                        OracleTable table = tableSchema.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Constraint table '" + tableName + "' not found in catalog '" + tableSchema.getName() + "'");
                        }
                        DBSObject constraint = null;
                        if (hasFK && type == DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getForeignKey(monitor, constrName);
                        }
                        if (hasConstraints && type != DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getConstraint(monitor, constrName);
                        }
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
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 23 with AbstractObjectReference

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

the class ExasolStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // don't use parameter marks because of performance
    String sql = "";
    if (schema == null) {
        sql = String.format(sqlProceduresAll, ExasolUtils.quoteString(objectNameMask));
    } else {
        sql = String.format(sqlProcedureSchema, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(objectNameMask));
    }
    try (JDBCStatement dbstat = session.createStatement()) {
        try (JDBCResultSet dbResult = dbstat.executeQuery(sql)) {
            int num = maxResults;
            while (dbResult.next() && num-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "SCRIPT_SCHEMA");
                final String scriptName = JDBCUtils.safeGetString(dbResult, "SCRIPT_NAME");
                references.add(new AbstractObjectReference(scriptName, dataSource.getSchema(monitor, schemaName), null, ExasolScript.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        ExasolSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Table schema '" + schemaName + "' not found");
                        }
                        ExasolScript script = tableSchema.scriptCache.getObject(monitor, tableSchema, scriptName);
                        if (script == null) {
                            throw new DBException("Script '" + script + "'  not found in schema '" + schemaName + "'");
                        }
                        return script;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 24 with AbstractObjectReference

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

the class ExasolStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references, String constType) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // don't use parameter marks because of performance
    String sql = "";
    if (schema == null) {
        sql = String.format(sqlConstraintsAll, ExasolUtils.quoteString(objectNameMask), constType);
    } else {
        sql = String.format(sqlConstraintsSchema, ExasolUtils.quoteString(schema.getName()), constType, ExasolUtils.quoteString(objectNameMask));
    }
    try (JDBCStatement dbstat = session.createStatement()) {
        try (JDBCResultSet dbResult = dbstat.executeQuery(sql)) {
            int num = maxResults;
            while (dbResult.next() && num-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "CONSTRAINT_SCHEMA");
                final String tableName = JDBCUtils.safeGetString(dbResult, "CONSTRAINT_TABLE");
                final String constName = JDBCUtils.safeGetString(dbResult, "CONSTRAINT_NAME");
                final Class<?> classType;
                if (constType.equals("PRIMARY KEY")) {
                    classType = ExasolTableUniqueKey.class;
                } else if (constType.equals("FOREIGN KEY")) {
                    classType = ExasolTableForeignKey.class;
                } else {
                    throw new DBException("Unkown constraint type" + constType);
                }
                references.add(new AbstractObjectReference(constName, dataSource.getSchema(monitor, schemaName), null, classType, RelationalObjectType.TYPE_CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        ExasolSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Table schema '" + schemaName + "' not found");
                        }
                        ExasolTable table = tableSchema.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Table '" + tableName + "' not found in schema  '" + schemaName + "' not found");
                        }
                        if (classType.equals(ExasolTableForeignKey.class)) {
                            ExasolTableForeignKey foreignKey = (ExasolTableForeignKey) table.getAssociation(monitor, constName);
                            if (foreignKey == null)
                                throw new DBException("Foreign Key  '" + constName + "' for Table '" + tableName + "' not found in schema '" + schemaName + "'");
                            return foreignKey;
                        } else {
                            ExasolTableUniqueKey primaryKey = table.getConstraint(monitor, constName);
                            if (primaryKey == null)
                                throw new DBException("Primary Key '" + constName + "' for Table '" + tableName + "' not found in schema '" + schemaName + "'");
                            return primaryKey;
                        }
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 25 with AbstractObjectReference

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

the class OracleStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, final OracleSchema schema, String constrNameMask, DBSObjectType[] objectTypes, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    List<DBSObjectType> objectTypesList = Arrays.asList(objectTypes);
    final boolean hasFK = objectTypesList.contains(OracleObjectType.FOREIGN_KEY);
    final boolean hasConstraints = objectTypesList.contains(OracleObjectType.CONSTRAINT);
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " OWNER, TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE\n" + "FROM " + OracleUtils.getAdminAllViewPrefix(monitor, (OracleDataSource) session.getDataSource(), "CONSTRAINTS") + "\n" + "WHERE CONSTRAINT_NAME like ?" + (!hasFK ? " AND CONSTRAINT_TYPE<>'R'" : "") + (schema != null ? " AND OWNER=?" : ""))) {
        dbStat.setString(1, constrNameMask.toUpperCase());
        if (schema != null) {
            dbStat.setString(2, schema.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_OWNER);
                final String tableName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_TABLE_NAME);
                final String constrName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_NAME);
                final String constrType = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_TYPE);
                final DBSEntityConstraintType type = OracleTableConstraint.getConstraintType(constrType);
                objects.add(new AbstractObjectReference(constrName, dataSource.getSchema(session.getProgressMonitor(), schemaName), null, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleTableForeignKey.class : OracleTableConstraint.class, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleObjectType.FOREIGN_KEY : OracleObjectType.CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        OracleSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Constraint schema '" + schemaName + "' not found");
                        }
                        OracleTable table = tableSchema.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Constraint table '" + tableName + "' not found in catalog '" + tableSchema.getName() + "'");
                        }
                        DBSObject constraint = null;
                        if (hasFK && type == DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getForeignKey(monitor, constrName);
                        }
                        if (hasConstraints && type != DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getConstraint(monitor, constrName);
                        }
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
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)

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