use of org.jooq.util.TableDefinition 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.TableDefinition 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);
}
}
}
}
use of org.jooq.util.TableDefinition 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);
}
}
}
use of org.jooq.util.TableDefinition 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);
}
}
}
}
use of org.jooq.util.TableDefinition in project jOOQ by jOOQ.
the class H2Database method loadPrimaryKeys.
@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
for (Record record : fetchKeys("PRIMARY KEY")) {
SchemaDefinition schema = getSchema(record.get(Constraints.TABLE_SCHEMA));
if (schema != null) {
String tableName = record.get(Constraints.TABLE_NAME);
String primaryKey = record.get(Constraints.CONSTRAINT_NAME);
String columnName = record.get(Indexes.COLUMN_NAME);
TableDefinition table = getTable(schema, tableName);
if (table != null) {
relations.addPrimaryKey(primaryKey, table.getColumn(columnName));
}
}
}
}
Aggregations