Search in sources :

Example 6 with AbstractObjectReference

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

the class MySQLStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String constrNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load constraints
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + "," + MySQLConstants.COL_CONSTRAINT_NAME + "," + MySQLConstants.COL_CONSTRAINT_TYPE + " FROM " + MySQLConstants.META_TABLE_TABLE_CONSTRAINTS + " WHERE " + MySQLConstants.COL_CONSTRAINT_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_CONSTRAINT_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 constrName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CONSTRAINT_NAME);
                final String constrType = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CONSTRAINT_TYPE);
                final boolean isFK = MySQLConstants.CONSTRAINT_FOREIGN_KEY.equals(constrType);
                objects.add(new AbstractObjectReference(constrName, dataSource.getCatalog(catalogName), null, isFK ? MySQLTableForeignKey.class : MySQLTableConstraint.class, RelationalObjectType.TYPE_CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (tableCatalog == null) {
                            throw new DBException("Constraint catalog '" + catalogName + "' not found");
                        }
                        MySQLTable table = tableCatalog.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Constraint table '" + tableName + "' not found in catalog '" + tableCatalog.getName() + "'");
                        }
                        DBSObject constraint;
                        if (isFK) {
                            constraint = table.getAssociation(monitor, constrName);
                        } else {
                            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 : 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 7 with AbstractObjectReference

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

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 8 with AbstractObjectReference

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

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 9 with AbstractObjectReference

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

the class ExasolStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(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
    try (JDBCStatement dbstat = session.createStatement()) {
        try (JDBCResultSet dbResult = dbstat.executeQuery(String.format(SQL_COLS_SCHEMA, (schema == null ? "%" : ExasolUtils.quoteString(schema.getName())), ExasolUtils.quoteString(objectNameMask)))) {
            int num = maxResults;
            while (dbResult.next() && num-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "TABLE_SCHEM");
                final String tableName = JDBCUtils.safeGetString(dbResult, "COLUMN_TABLE");
                final String columnName = JDBCUtils.safeGetString(dbResult, "COLUMN_NAME");
                references.add(new AbstractObjectReference(columnName, dataSource.getSchema(monitor, schemaName), null, ExasolTableBase.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @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.getTableCache().getObject(monitor, tableSchema, tableName);
                        if (table == null) {
                            ExasolView view = tableSchema.getViewCache().getObject(monitor, tableSchema, tableName);
                            if (view == null)
                                throw new DBException("nor Table or view with name '" + tableName + "'  found in schema '" + schemaName + "'");
                            return view;
                        }
                        return table;
                    }
                });
            }
        }
    }
}
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 10 with AbstractObjectReference

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

the class ExasolStructureAssistant method findTableObjectByName.

private void findTableObjectByName(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references, String type) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // don't use parameter marks because of performance
    String sql = "";
    if (schema == null) {
        sql = String.format(SQL_TABLES_ALL, ExasolUtils.quoteString(objectNameMask), type);
    } else {
        sql = String.format(SQL_TABLES_SCHEMA, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(objectNameMask), type);
    }
    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, "TABLE_SCHEM");
                final String tableName = JDBCUtils.safeGetString(dbResult, "COLUMN_TABLE");
                references.add(new AbstractObjectReference(tableName, dataSource.getSchema(monitor, schemaName), null, ExasolTableBase.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @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");
                        }
                        if (type == "VIEW") {
                            ExasolView view = tableSchema.getViewCache().getObject(monitor, tableSchema, tableName);
                            if (view == null)
                                throw new DBException("View '" + tableName + "' not found in schema '" + schemaName + "'");
                            return view;
                        } else if (type == "TABLE") {
                            ExasolTable table = tableSchema.getTableCache().getObject(monitor, tableSchema, tableName);
                            if (table == null)
                                throw new DBException("Table '" + tableName + "' not found in schema '" + schemaName + "'");
                            return table;
                        } else {
                            throw new DBException("Object type " + type + " unknown");
                        }
                    }
                });
            }
        }
    }
}
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)

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