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));
}
}
}
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);
}
}
}
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;
}
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));
}
}
}
}
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);
}
}
}
}
Aggregations