Search in sources :

Example 36 with SchemaDefinition

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

the class MySQLDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys(false)) {
        SchemaDefinition schema = getSchema(record.get(Statistics.TABLE_SCHEMA));
        String constraintName = record.get(Statistics.INDEX_NAME);
        String tableName = record.get(Statistics.TABLE_NAME);
        String columnName = record.get(Statistics.COLUMN_NAME);
        String key = getKeyName(tableName, constraintName);
        TableDefinition table = getTable(schema, tableName);
        if (table != null) {
            relations.addUniqueKey(key, table.getColumn(columnName));
        }
    }
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record)

Example 37 with SchemaDefinition

use of org.jooq.util.SchemaDefinition 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 38 with SchemaDefinition

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

the class CUBRIDDatabase method getSchemata0.

@Override
protected List<SchemaDefinition> getSchemata0() throws SQLException {
    List<SchemaDefinition> result = new ArrayList<SchemaDefinition>();
    result.add(new SchemaDefinition(this, "", ""));
    return result;
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) ArrayList(java.util.ArrayList)

Example 39 with SchemaDefinition

use of org.jooq.util.SchemaDefinition 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 40 with SchemaDefinition

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

Aggregations

SchemaDefinition (org.jooq.util.SchemaDefinition)51 Record (org.jooq.Record)36 ArrayList (java.util.ArrayList)29 TableDefinition (org.jooq.util.TableDefinition)28 DefaultDataTypeDefinition (org.jooq.util.DefaultDataTypeDefinition)14 DataTypeDefinition (org.jooq.util.DataTypeDefinition)13 ColumnDefinition (org.jooq.util.ColumnDefinition)12 DefaultSequenceDefinition (org.jooq.util.DefaultSequenceDefinition)7 SequenceDefinition (org.jooq.util.SequenceDefinition)7 DefaultColumnDefinition (org.jooq.util.DefaultColumnDefinition)4 PgNamespace (org.jooq.util.postgres.pg_catalog.tables.PgNamespace)4 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)3 RoutineDefinition (org.jooq.util.RoutineDefinition)3 KeyColumnUsage (org.jooq.util.xml.jaxb.KeyColumnUsage)3 Record6 (org.jooq.Record6)2 Schema (org.jooq.Schema)2 DefaultCheckConstraintDefinition (org.jooq.util.DefaultCheckConstraintDefinition)2 DefaultEnumDefinition (org.jooq.util.DefaultEnumDefinition)2 EnumDefinition (org.jooq.util.EnumDefinition)2 Columns (org.jooq.util.postgres.information_schema.tables.Columns)2