Search in sources :

Example 51 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class H2Database method loadForeignKeys2_0.

private void loadForeignKeys2_0(DefaultRelations relations) {
    KeyColumnUsage fkKcu = KEY_COLUMN_USAGE.as("fk_kcu");
    KeyColumnUsage pkKcu = KEY_COLUMN_USAGE.as("pk_kcu");
    for (Record record : create().select(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA, TABLE_CONSTRAINTS.TABLE_NAME, fkKcu.CONSTRAINT_NAME, fkKcu.TABLE_SCHEMA, fkKcu.TABLE_NAME, fkKcu.COLUMN_NAME, pkKcu.COLUMN_NAME).from(REFERENTIAL_CONSTRAINTS).join(fkKcu).on(fkKcu.CONSTRAINT_SCHEMA.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(fkKcu.CONSTRAINT_NAME.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME)).join(TABLE_CONSTRAINTS).on(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA.eq(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA)).and(TABLE_CONSTRAINTS.CONSTRAINT_NAME.eq(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME)).join(pkKcu).on(pkKcu.CONSTRAINT_SCHEMA.eq(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(pkKcu.CONSTRAINT_NAME.eq(TABLE_CONSTRAINTS.CONSTRAINT_NAME)).and(pkKcu.ORDINAL_POSITION.eq(fkKcu.POSITION_IN_UNIQUE_CONSTRAINT)).where(fkKcu.TABLE_SCHEMA.in(getInputSchemata())).orderBy(fkKcu.TABLE_SCHEMA.asc(), fkKcu.TABLE_NAME.asc(), fkKcu.CONSTRAINT_NAME.asc(), fkKcu.ORDINAL_POSITION.asc())) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(fkKcu.TABLE_SCHEMA));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA));
        String foreignKey = record.get(fkKcu.CONSTRAINT_NAME);
        String foreignKeyTableName = record.get(fkKcu.TABLE_NAME);
        String foreignKeyColumn = record.get(fkKcu.COLUMN_NAME);
        String uniqueKey = record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME);
        String uniqueKeyTableName = record.get(TABLE_CONSTRAINTS.TABLE_NAME);
        String uniqueKeyColumn = record.get(pkKcu.COLUMN_NAME);
        TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
        TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
        if (foreignKeyTable != null && uniqueKeyTable != null)
            relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), uniqueKey, uniqueKeyTable, uniqueKeyTable.getColumn(uniqueKeyColumn), true);
    }
}
Also used : KeyColumnUsage(org.jooq.meta.hsqldb.information_schema.tables.KeyColumnUsage) SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 52 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class H2Database method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    // Same implementation as in HSQLDBDatabase and MySQLDatabase
    Map<Record, Result<Record>> indexes = indexes().fetchGroups(new Field[] { INDEXES.TABLE_SCHEMA, INDEXES.TABLE_NAME, INDEXES.INDEX_NAME, INDEXES.NON_UNIQUE }, new Field[] { INDEXES.COLUMN_NAME, INDEXES.ORDINAL_POSITION });
    indexLoop: for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
        final Record index = entry.getKey();
        final Result<Record> columns = entry.getValue();
        final SchemaDefinition tableSchema = getSchema(index.get(INDEXES.TABLE_SCHEMA));
        if (tableSchema == null)
            continue indexLoop;
        final String indexName = index.get(INDEXES.INDEX_NAME);
        final String tableName = index.get(INDEXES.TABLE_NAME);
        final TableDefinition table = getTable(tableSchema, tableName);
        if (table == null)
            continue indexLoop;
        final boolean unique = !index.get(INDEXES.NON_UNIQUE, boolean.class);
        // [#6310] [#6620] Function-based indexes are not yet supported
        for (Record column : columns) if (table.getColumn(column.get(INDEXES.COLUMN_NAME)) == null)
            continue indexLoop;
        result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {

            List<IndexColumnDefinition> indexColumns = new ArrayList<>();

            {
                for (Record column : columns) {
                    indexColumns.add(new DefaultIndexColumnDefinition(this, table.getColumn(column.get(INDEXES.COLUMN_NAME)), SortOrder.ASC, column.get(INDEXES.ORDINAL_POSITION, int.class)));
                }
            }

            @Override
            protected List<IndexColumnDefinition> getIndexColumns0() {
                return indexColumns;
            }
        });
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) Result(org.jooq.Result) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Entry(java.util.Map.Entry) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 53 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class H2Database method loadForeignKeys1_4.

private void loadForeignKeys1_4(DefaultRelations relations) {
    for (Record record : create().select(CROSS_REFERENCES.FK_NAME, CROSS_REFERENCES.FKTABLE_NAME, CROSS_REFERENCES.FKTABLE_SCHEMA, CROSS_REFERENCES.FKCOLUMN_NAME, CROSS_REFERENCES.PKCOLUMN_NAME, CROSS_REFERENCES.referencedConstraint().CONSTRAINT_NAME, CROSS_REFERENCES.referencedConstraint().TABLE_NAME, CROSS_REFERENCES.referencedConstraint().CONSTRAINT_SCHEMA).from(CROSS_REFERENCES).where(CROSS_REFERENCES.FKTABLE_SCHEMA.in(getInputSchemata())).and(CROSS_REFERENCES.referencedConstraint().CONSTRAINT_TYPE.in("PRIMARY KEY", "PRIMARY_KEY", "UNIQUE")).orderBy(CROSS_REFERENCES.FKTABLE_SCHEMA.asc(), CROSS_REFERENCES.FK_NAME.asc(), CROSS_REFERENCES.ORDINAL_POSITION.asc()).fetch()) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(CROSS_REFERENCES.FKTABLE_SCHEMA));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(CROSS_REFERENCES.referencedConstraint().CONSTRAINT_SCHEMA));
        if (foreignKeySchema != null && uniqueKeySchema != null) {
            String foreignKey = record.get(CROSS_REFERENCES.FK_NAME);
            String foreignKeyTableName = record.get(CROSS_REFERENCES.FKTABLE_NAME);
            String foreignKeyColumn = record.get(CROSS_REFERENCES.FKCOLUMN_NAME);
            String uniqueKey = record.get(CROSS_REFERENCES.referencedConstraint().CONSTRAINT_NAME);
            String uniqueKeyTableName = record.get(CROSS_REFERENCES.referencedConstraint().TABLE_NAME);
            String uniqueKeyColumn = record.get(CROSS_REFERENCES.PKCOLUMN_NAME);
            TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
            TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
            if (foreignKeyTable != null && uniqueKeyTable != null)
                relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), uniqueKey, uniqueKeyTable, uniqueKeyTable.getColumn(uniqueKeyColumn), true);
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 54 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class H2Database method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<>();
    for (TableRecord r : create().select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME, when(TABLES.TABLE_TYPE.eq(inline("VIEW")), inline(TableType.VIEW.name())).when(TABLES.STORAGE_TYPE.like(inline("%TEMPORARY%")), inline(TableType.TEMPORARY.name())).else_(inline(TableType.TABLE.name())).convertFrom(TableType::valueOf).as("table_type"), TABLES.REMARKS, VIEWS.VIEW_DEFINITION).from(TABLES).leftJoin(VIEWS).on(TABLES.TABLE_SCHEMA.eq(VIEWS.TABLE_SCHEMA)).and(TABLES.TABLE_NAME.eq(VIEWS.TABLE_NAME)).where(TABLES.TABLE_SCHEMA.in(getInputSchemata())).orderBy(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME).fetch(mapping(TableRecord::new))) {
        SchemaDefinition schema = getSchema(r.schema);
        String source = r.source;
        if (source != null && !source.toLowerCase().startsWith("create"))
            source = "create view \"" + r.table + "\" as " + source;
        if (schema != null)
            result.add(new H2TableDefinition(schema, r.table, r.comment, r.type, source));
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition)

Example 55 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class H2Database method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (Record record : primaryKeys(getInputSchemata())) {
        SchemaDefinition schema = getSchema(record.get(CONSTRAINTS.TABLE_SCHEMA));
        if (schema != null) {
            String tableName = record.get(CONSTRAINTS.TABLE_NAME);
            String primaryKey = record.get(CONSTRAINTS.CONSTRAINT_NAME);
            String columnName = record.get(INDEXES.COLUMN_NAME);
            TableDefinition table = getTable(schema, tableName);
            if (table != null)
                relations.addPrimaryKey(primaryKey, table, table.getColumn(columnName));
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Aggregations

TableDefinition (org.jooq.meta.TableDefinition)67 SchemaDefinition (org.jooq.meta.SchemaDefinition)47 Record (org.jooq.Record)44 ArrayList (java.util.ArrayList)23 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)16 IndexDefinition (org.jooq.meta.IndexDefinition)14 List (java.util.List)12 ColumnDefinition (org.jooq.meta.ColumnDefinition)12 IOException (java.io.IOException)10 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)10 Result (org.jooq.Result)9 CatalogDefinition (org.jooq.meta.CatalogDefinition)9 EnumDefinition (org.jooq.meta.EnumDefinition)9 RoutineDefinition (org.jooq.meta.RoutineDefinition)9 Arrays.asList (java.util.Arrays.asList)8 TableType (org.jooq.TableOptions.TableType)8 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)8 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)8 PackageDefinition (org.jooq.meta.PackageDefinition)8 SequenceDefinition (org.jooq.meta.SequenceDefinition)8