Search in sources :

Example 21 with TableDefinition

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

the class PostgresDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys("UNIQUE")) {
        SchemaDefinition schema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
        String key = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
        String tableName = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
        String columnName = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
        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 22 with TableDefinition

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

the class PostgresDatabase method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys("PRIMARY KEY")) {
        SchemaDefinition schema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
        String key = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
        String tableName = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
        String columnName = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
        TableDefinition table = getTable(schema, tableName);
        if (table != null) {
            relations.addPrimaryKey(key, table.getColumn(columnName));
        }
    }
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record)

Example 23 with TableDefinition

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

the class HSQLDBDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    Result<?> result = create().select(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA, KEY_COLUMN_USAGE.CONSTRAINT_NAME, KEY_COLUMN_USAGE.TABLE_SCHEMA, KEY_COLUMN_USAGE.TABLE_NAME, KEY_COLUMN_USAGE.COLUMN_NAME).from(REFERENTIAL_CONSTRAINTS).join(KEY_COLUMN_USAGE).on(KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(KEY_COLUMN_USAGE.CONSTRAINT_NAME.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME)).where(KEY_COLUMN_USAGE.TABLE_SCHEMA.in(getInputSchemata())).orderBy(KEY_COLUMN_USAGE.TABLE_SCHEMA.asc(), KEY_COLUMN_USAGE.TABLE_NAME.asc(), KEY_COLUMN_USAGE.CONSTRAINT_NAME.asc(), KEY_COLUMN_USAGE.ORDINAL_POSITION.asc()).fetch();
    for (Record record : result) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA));
        String foreignKey = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
        String foreignKeyTable = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
        String foreignKeyColumn = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
        String uniqueKey = record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME);
        TableDefinition referencingTable = getTable(foreignKeySchema, foreignKeyTable);
        if (referencingTable != null) {
            ColumnDefinition referencingColumn = referencingTable.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 24 with TableDefinition

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

the class HSQLDBDatabase method loadCheckConstraints.

@Override
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
    TableConstraints tc = TABLE_CONSTRAINTS.as("tc");
    CheckConstraints cc = CHECK_CONSTRAINTS.as("cc");
    // [#2808] [#3019] Workaround for bad handling of JOIN .. USING
    Field<String> constraintName = field(name(cc.CONSTRAINT_NAME.getName()), String.class);
    for (Record record : create().select(tc.TABLE_SCHEMA, tc.TABLE_NAME, constraintName, cc.CHECK_CLAUSE).from(tc).join(cc).using(tc.CONSTRAINT_CATALOG, tc.CONSTRAINT_SCHEMA, tc.CONSTRAINT_NAME).where(tc.TABLE_SCHEMA.in(getInputSchemata())).fetch()) {
        SchemaDefinition schema = getSchema(record.get(tc.TABLE_SCHEMA));
        TableDefinition table = getTable(schema, record.get(tc.TABLE_NAME));
        if (table != null) {
            relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, record.get(constraintName), record.get(cc.CHECK_CLAUSE)));
        }
    }
}
Also used : TableConstraints(org.jooq.util.hsqldb.information_schema.tables.TableConstraints) SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) DefaultCheckConstraintDefinition(org.jooq.util.DefaultCheckConstraintDefinition) CheckConstraints(org.jooq.util.hsqldb.information_schema.tables.CheckConstraints)

Example 25 with TableDefinition

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

the class HSQLDBDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<TableDefinition>();
    for (Record record : create().select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME).from(TABLES).where(TABLES.TABLE_SCHEMA.in(getInputSchemata())).orderBy(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME).fetch()) {
        SchemaDefinition schema = getSchema(record.get(TABLES.TABLE_SCHEMA));
        String name = record.get(TABLES.TABLE_NAME);
        String comment = "";
        result.add(new HSQLDBTableDefinition(schema, name, comment));
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) ArrayList(java.util.ArrayList) 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