Search in sources :

Example 1 with KeyColumnUsage

use of org.jooq.meta.hsqldb.information_schema.tables.KeyColumnUsage 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 2 with KeyColumnUsage

use of org.jooq.meta.hsqldb.information_schema.tables.KeyColumnUsage in project jOOQ by jOOQ.

the class HSQLDBDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    KeyColumnUsage fkKcu = KEY_COLUMN_USAGE.as("fk_kcu");
    KeyColumnUsage pkKcu = KEY_COLUMN_USAGE.as("pk_kcu");
    Result<?> result = 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()).fetch();
    for (Record record : result) {
        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)

Aggregations

Record (org.jooq.Record)2 SchemaDefinition (org.jooq.meta.SchemaDefinition)2 TableDefinition (org.jooq.meta.TableDefinition)2 KeyColumnUsage (org.jooq.meta.hsqldb.information_schema.tables.KeyColumnUsage)2