Search in sources :

Example 31 with TableDefinition

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

the class DerbyDatabase method loadUniqueKeys.

/**
     * {@inheritDoc}
     */
@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys("U")) {
        SchemaDefinition schema = getSchema(record.get(Sysschemas.SCHEMANAME));
        String key = record.get(Sysconstraints.CONSTRAINTNAME);
        String tableName = record.get(Systables.TABLENAME);
        String descriptor = record.get(Sysconglomerates.DESCRIPTOR, String.class);
        TableDefinition table = getTable(schema, tableName);
        if (table != null) {
            for (int index : decode(descriptor)) {
                relations.addUniqueKey(key, table.getColumn(index));
            }
        }
    }
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record)

Example 32 with TableDefinition

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

the class DerbyDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    Field<String> fkName = field("fc.constraintname", String.class);
    Field<String> fkTable = field("ft.tablename", String.class);
    Field<String> fkSchema = field("fs.schemaname", String.class);
    Field<?> fkDescriptor = field("fg.descriptor");
    Field<String> ukName = field("pc.constraintname", String.class);
    Field<String> ukSchema = field("ps.schemaname", String.class);
    for (Record record : create().select(fkName, fkTable, fkSchema, fkDescriptor, ukName, ukSchema).from("sys.sysconstraints   fc").join("sys.sysforeignkeys   f ").on("f.constraintid = fc.constraintid").join("sys.sysconglomerates fg").on("fg.conglomerateid = f.conglomerateid").join("sys.systables        ft").on("ft.tableid = fg.tableid").join("sys.sysschemas       fs").on("ft.schemaid = fs.schemaid").join("sys.sysconstraints   pc").on("pc.constraintid = f.keyconstraintid").join("sys.sysschemas       ps").on("pc.schemaid = ps.schemaid").where("fc.type = 'F'").fetch()) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(fkSchema));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(ukSchema));
        String foreignKeyName = record.get(fkName);
        String foreignKeyTableName = record.get(fkTable);
        List<Integer> foreignKeyIndexes = decode(record.get(fkDescriptor, String.class));
        String uniqueKeyName = record.get(ukName);
        TableDefinition referencingTable = getTable(foreignKeySchema, foreignKeyTableName);
        if (referencingTable != null) {
            for (int i = 0; i < foreignKeyIndexes.size(); i++) {
                ColumnDefinition column = referencingTable.getColumn(foreignKeyIndexes.get(i));
                relations.addForeignKey(foreignKeyName, uniqueKeyName, column, uniqueKeySchema);
            }
        }
    }
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 33 with TableDefinition

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

the class FirebirdDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations r) throws SQLException {
    for (Record record : fetchKeys("UNIQUE")) {
        String tableName = record.get(RDB$RELATION_CONSTRAINTS.RDB$RELATION_NAME.trim());
        String fieldName = record.get(RDB$INDEX_SEGMENTS.RDB$FIELD_NAME.trim());
        String key = record.get(RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_NAME.trim());
        TableDefinition td = getTable(this.getSchemata().get(0), tableName);
        if (td != null) {
            ColumnDefinition cd = td.getColumn(fieldName);
            r.addUniqueKey(key, cd);
        }
    }
}
Also used : TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 34 with TableDefinition

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

the class H2Database method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    for (Record record : create().select(CrossReferences.FK_NAME, CrossReferences.FKTABLE_NAME, CrossReferences.FKTABLE_SCHEMA, CrossReferences.FKCOLUMN_NAME, Constraints.CONSTRAINT_NAME, Constraints.CONSTRAINT_SCHEMA).from(CROSS_REFERENCES).join(CONSTRAINTS).on(CrossReferences.PK_NAME.equal(Constraints.UNIQUE_INDEX_NAME)).and(CrossReferences.PKTABLE_NAME.equal(Constraints.TABLE_NAME)).and(CrossReferences.PKTABLE_SCHEMA.equal(Constraints.TABLE_SCHEMA)).where(CrossReferences.FKTABLE_SCHEMA.in(getInputSchemata())).and(Constraints.CONSTRAINT_TYPE.in("PRIMARY KEY", "UNIQUE")).orderBy(CrossReferences.FKTABLE_SCHEMA.asc(), CrossReferences.FK_NAME.asc(), CrossReferences.ORDINAL_POSITION.asc()).fetch()) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(CrossReferences.FKTABLE_SCHEMA));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(Constraints.CONSTRAINT_SCHEMA));
        if (foreignKeySchema != null && uniqueKeySchema != null) {
            String foreignKeyTableName = record.get(CrossReferences.FKTABLE_NAME);
            String foreignKeyColumn = record.get(CrossReferences.FKCOLUMN_NAME);
            String foreignKey = record.get(CrossReferences.FK_NAME);
            String uniqueKey = record.get(Constraints.CONSTRAINT_NAME);
            TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
            if (foreignKeyTable != null) {
                ColumnDefinition referencingColumn = foreignKeyTable.getColumn(foreignKeyColumn);
                relations.addForeignKey(foreignKey, uniqueKey, referencingColumn, uniqueKeySchema);
            }
        }
    }
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 35 with TableDefinition

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

the class H2Database method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys("PRIMARY KEY")) {
        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.getColumn(columnName));
            }
        }
    }
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record)

Aggregations

TableDefinition (org.jooq.util.TableDefinition)37 Record (org.jooq.Record)32 SchemaDefinition (org.jooq.util.SchemaDefinition)28 ColumnDefinition (org.jooq.util.ColumnDefinition)14 ArrayList (java.util.ArrayList)9 KeyColumnUsage (org.jooq.util.xml.jaxb.KeyColumnUsage)3 HashMap (java.util.HashMap)2 DefaultCheckConstraintDefinition (org.jooq.util.DefaultCheckConstraintDefinition)2 DefaultEnumDefinition (org.jooq.util.DefaultEnumDefinition)2 EnumDefinition (org.jooq.util.EnumDefinition)2 StringReader (java.io.StringReader)1 Name (org.jooq.Name)1 Record5 (org.jooq.Record5)1 Record6 (org.jooq.Record6)1 Schema (org.jooq.Schema)1 CSVReader (org.jooq.tools.csv.CSVReader)1 Rdb$indexSegments (org.jooq.util.firebird.rdb.tables.Rdb$indexSegments)1 Rdb$refConstraints (org.jooq.util.firebird.rdb.tables.Rdb$refConstraints)1 Rdb$relationConstraints (org.jooq.util.firebird.rdb.tables.Rdb$relationConstraints)1 CheckConstraints (org.jooq.util.hsqldb.information_schema.tables.CheckConstraints)1