Search in sources :

Example 11 with AbstractObjectReference

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

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

the class HANAStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(JDBCSession session, GenericSchema parentSchema, String objectNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> result) throws SQLException, DBException {
    String stmt = "SELECT SCHEMA_NAME, TABLE_NAME, COLUMN_NAME, COMMENTS FROM SYS.TABLE_COLUMNS WHERE";
    stmt += caseSensitive ? " COLUMN_NAME LIKE ?" : " UPPER(COLUMN_NAME) LIKE ?";
    if (parentSchema != null)
        stmt += " AND SCHEMA_NAME = ?";
    stmt += " ORDER BY SCHEMA_NAME, TABLE_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 13 with AbstractObjectReference

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

the class HANAStructureAssistant method findTablesByMask.

private void findTablesByMask(JDBCSession session, GenericSchema parentSchema, String objectNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> result) throws SQLException, DBException {
    String stmt = "SELECT SCHEMA_NAME, TABLE_NAME, COMMENTS FROM SYS.TABLES WHERE";
    stmt += caseSensitive ? " TABLE_NAME LIKE ?" : " UPPER(TABLE_NAME) LIKE ?";
    if (parentSchema != null)
        stmt += " AND SCHEMA_NAME = ?";
    stmt += " ORDER BY SCHEMA_NAME, TABLE_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_TABLE) {

                    @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 14 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, PostgreDatabase database, @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 = database.getSchema(session.getProgressMonitor(), schemaId);
                if (tableSchema == null) {
                    log.debug("Can't resolve table '" + tableName + "' - owner schema " + schemaId + " not found");
                    continue;
                }
                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 15 with AbstractObjectReference

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

the class PostgreStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(JDBCSession session, PostgreDatabase database, @Nullable final List<PostgreSchema> schema, String columnNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load constraints
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.attname,x.attrelid,x.atttypid,c.relnamespace " + "FROM pg_catalog.pg_attribute x, pg_catalog.pg_class c\n" + "WHERE c.oid=x.attrelid AND x.attname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND c.relnamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.attname LIMIT " + maxResults)) {
        dbStat.setString(1, columnNameMask);
        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, "attrelid");
                final String attributeName = JDBCUtils.safeGetString(dbResult, "attname");
                final PostgreSchema constrSchema = database.getSchema(session.getProgressMonitor(), schemaId);
                if (constrSchema == null) {
                    log.debug("Attribute's schema '" + schemaId + "' not found");
                    continue;
                }
                objects.add(new AbstractObjectReference(attributeName, constrSchema, null, PostgreTableBase.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        final PostgreTableBase table = PostgreUtils.getObjectById(monitor, constrSchema.getTableCache(), constrSchema, tableId);
                        if (table == null) {
                            throw new DBException("Table '" + tableId + "' not found in schema '" + constrSchema.getName() + "'");
                        }
                        return table.getAttribute(monitor, attributeName);
                    }
                });
            }
        }
    }
}
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