Search in sources :

Example 26 with TableDefinition

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

the class JDBCDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Schema schema : getSchemasFromMeta()) {
        SchemaDefinition s = getSchema(schema.getName());
        if (s != null) {
            for (Table<?> table : schema.getTables()) {
                TableDefinition t = getTable(s, table.getName());
                if (t != null) {
                    UniqueKey<?> key = table.getPrimaryKey();
                    if (key != null) {
                        for (Field<?> field : key.getFields()) {
                            ColumnDefinition c = t.getColumn(field.getName());
                            relations.addPrimaryKey("PK_" + key.getTable().getName(), c);
                        }
                    }
                }
            }
        }
    }
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) Schema(org.jooq.Schema) TableDefinition(org.jooq.util.TableDefinition) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 27 with TableDefinition

use of org.jooq.util.TableDefinition 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 28 with TableDefinition

use of org.jooq.util.TableDefinition 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 29 with TableDefinition

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

the class CUBRIDDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<TableDefinition>();
    for (Record record : create().select(DB_CLASS.CLASS_NAME).from(DB_CLASS).orderBy(DB_CLASS.CLASS_NAME.asc()).fetch()) {
        String name = record.get(DB_CLASS.CLASS_NAME);
        CUBRIDTableDefinition table = new CUBRIDTableDefinition(getSchemata().get(0), name, null);
        result.add(table);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record)

Example 30 with TableDefinition

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

the class CUBRIDDatabase method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys(DB_INDEX.IS_UNIQUE.isTrue().and(DB_INDEX.IS_PRIMARY_KEY.isFalse()))) {
        String key = record.get("constraint_name", String.class);
        String tableName = record.get(DB_CLASS.CLASS_NAME);
        String columnName = record.get(DB_INDEX_KEY.KEY_ATTR_NAME);
        TableDefinition table = getTable(getSchemata().get(0), tableName);
        if (table != null) {
            relations.addUniqueKey(key, table.getColumn(columnName));
        }
    }
}
Also used : 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