Search in sources :

Example 21 with ColumnDefinition

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

the class PostgresDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    // [#3520] PostgreSQL INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS contains incomplete information about foreign keys
    // The (CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME) tuple is non-unique, in case two tables share the
    // same CONSTRAINT_NAME.
    // The JDBC driver implements this correctly through the pg_catalog, although the sorting and column name casing is wrong, too.
    Result<Record> result = create().fetch(getConnection().getMetaData().getExportedKeys(null, null, null)).sortAsc("key_seq").sortAsc("fk_name").sortAsc("fktable_name").sortAsc("fktable_schem");
    for (Record record : result) {
        SchemaDefinition foreignKeySchema = getSchema(record.get("fktable_schem", String.class));
        SchemaDefinition uniqueKeySchema = getSchema(record.get("pktable_schem", String.class));
        String foreignKey = record.get("fk_name", String.class);
        String foreignKeyTable = record.get("fktable_name", String.class);
        String foreignKeyColumn = record.get("fkcolumn_name", String.class);
        String uniqueKey = record.get("pk_name", String.class);
        TableDefinition referencingTable = getTable(foreignKeySchema, foreignKeyTable);
        if (referencingTable != null) {
            // [#986] Add the table name as a namespace prefix to the key
            // name. In Postgres, foreign key names are only unique per table
            ColumnDefinition referencingColumn = referencingTable.getColumn(foreignKeyColumn);
            relations.addForeignKey(foreignKeyTable + "__" + 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 22 with ColumnDefinition

use of org.jooq.util.ColumnDefinition 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 23 with ColumnDefinition

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

the class DerbyTableDefinition method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
    for (Record record : create().select(Syscolumns.COLUMNNAME, Syscolumns.COLUMNNUMBER, Syscolumns.COLUMNDATATYPE, Syscolumns.COLUMNDEFAULT, Syscolumns.AUTOINCREMENTINC).from(SYSCOLUMNS).where(Syscolumns.REFERENCEID.equal(inline(tableid))).orderBy(Syscolumns.COLUMNNUMBER).fetch()) {
        String typeName = record.get(Syscolumns.COLUMNDATATYPE, String.class);
        Number precision = parsePrecision(typeName);
        Number scale = parseScale(typeName);
        DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), parseTypeName(typeName), precision, precision, scale, !parseNotNull(typeName), record.get(Syscolumns.COLUMNDEFAULT));
        ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(Syscolumns.COLUMNNAME), record.get(Syscolumns.COLUMNNUMBER), type, null != record.get(Syscolumns.AUTOINCREMENTINC), null);
        result.add(column);
    }
    return result;
}
Also used : DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DataTypeDefinition(org.jooq.util.DataTypeDefinition) DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) ColumnDefinition(org.jooq.util.ColumnDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition)

Example 24 with ColumnDefinition

use of org.jooq.util.ColumnDefinition 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 25 with ColumnDefinition

use of org.jooq.util.ColumnDefinition 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)

Aggregations

ColumnDefinition (org.jooq.util.ColumnDefinition)27 Record (org.jooq.Record)23 ArrayList (java.util.ArrayList)15 TableDefinition (org.jooq.util.TableDefinition)14 DefaultColumnDefinition (org.jooq.util.DefaultColumnDefinition)13 DefaultDataTypeDefinition (org.jooq.util.DefaultDataTypeDefinition)13 SchemaDefinition (org.jooq.util.SchemaDefinition)12 DataTypeDefinition (org.jooq.util.DataTypeDefinition)10 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)3 DefaultEnumDefinition (org.jooq.util.DefaultEnumDefinition)2 EnumDefinition (org.jooq.util.EnumDefinition)2 Rdb$fields (org.jooq.util.firebird.rdb.tables.Rdb$fields)2 Columns (org.jooq.util.postgres.information_schema.tables.Columns)2 PgNamespace (org.jooq.util.postgres.pg_catalog.tables.PgNamespace)2 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Record5 (org.jooq.Record5)1 Schema (org.jooq.Schema)1 CSVReader (org.jooq.tools.csv.CSVReader)1 Rdb$indexSegments (org.jooq.util.firebird.rdb.tables.Rdb$indexSegments)1