use of org.jooq.util.firebird.rdb.tables.Rdb$indexSegments in project jOOQ by jOOQ.
the class FirebirdDatabase method loadForeignKeys.
@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
Rdb$relationConstraints pk = RDB$RELATION_CONSTRAINTS.as("pk");
Rdb$relationConstraints fk = RDB$RELATION_CONSTRAINTS.as("fk");
Rdb$refConstraints rc = RDB$REF_CONSTRAINTS.as("rc");
Rdb$indexSegments isp = RDB$INDEX_SEGMENTS.as("isp");
Rdb$indexSegments isf = RDB$INDEX_SEGMENTS.as("isf");
for (Record record : create().selectDistinct(fk.RDB$CONSTRAINT_NAME.trim().as("fk"), fk.RDB$RELATION_NAME.trim().as("fkTable"), isf.RDB$FIELD_NAME.trim().as("fkField"), pk.RDB$CONSTRAINT_NAME.trim().as("pk"), pk.RDB$RELATION_NAME.trim().as("pkTable")).from(fk).join(rc).on(fk.RDB$CONSTRAINT_NAME.eq(rc.RDB$CONSTRAINT_NAME)).join(pk).on(pk.RDB$CONSTRAINT_NAME.eq(rc.RDB$CONST_NAME_UQ)).join(isp).on(isp.RDB$INDEX_NAME.eq(pk.RDB$INDEX_NAME)).join(isf).on(isf.RDB$INDEX_NAME.eq(fk.RDB$INDEX_NAME)).where(isp.RDB$FIELD_POSITION.eq(isf.RDB$FIELD_POSITION)).orderBy(fk.RDB$CONSTRAINT_NAME.asc(), isf.RDB$FIELD_POSITION.asc()).fetch()) {
String pkName = record.get("pk", String.class);
String pkTable = record.get("pkTable", String.class);
String fkName = record.get("fk", String.class);
String fkTable = record.get("fkTable", String.class);
String fkField = record.get("fkField", String.class);
TableDefinition foreignKeyTable = getTable(getSchemata().get(0), fkTable, true);
TableDefinition primaryKeyTable = getTable(getSchemata().get(0), pkTable, true);
if (primaryKeyTable != null && foreignKeyTable != null)
relations.addForeignKey(fkName, foreignKeyTable, foreignKeyTable.getColumn(fkField), pkName, primaryKeyTable);
}
}
use of org.jooq.util.firebird.rdb.tables.Rdb$indexSegments in project jOOQ by jOOQ.
the class FirebirdDatabase method getIndexes0.
@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
final List<IndexDefinition> result = new ArrayList<>();
final Rdb$relationConstraints c = RDB$RELATION_CONSTRAINTS.as("c");
final Rdb$indices i = RDB$INDICES.as("i");
final Rdb$indexSegments s = RDB$INDEX_SEGMENTS.as("s");
Map<Record, Result<Record>> indexes = create().select(s.rdb$indices().RDB$RELATION_NAME.trim().as(i.RDB$RELATION_NAME), s.rdb$indices().RDB$INDEX_NAME.trim().as(i.RDB$INDEX_NAME), s.rdb$indices().RDB$UNIQUE_FLAG, s.RDB$FIELD_NAME.trim().as(s.RDB$FIELD_NAME), s.RDB$FIELD_POSITION).from(s).where(s.rdb$indices().RDB$INDEX_NAME.notIn(select(c.RDB$CONSTRAINT_NAME).from(c))).orderBy(s.rdb$indices().RDB$RELATION_NAME, s.rdb$indices().RDB$INDEX_NAME, s.RDB$FIELD_POSITION).fetchGroups(new Field[] { i.RDB$RELATION_NAME, i.RDB$INDEX_NAME, i.RDB$UNIQUE_FLAG }, new Field[] { s.RDB$FIELD_NAME, s.RDB$FIELD_POSITION });
indexLoop: for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
final Record index = entry.getKey();
final Result<Record> columns = entry.getValue();
final SchemaDefinition schema = getSchemata().get(0);
final String indexName = index.get(i.RDB$INDEX_NAME);
final String tableName = index.get(i.RDB$RELATION_NAME);
final TableDefinition table = getTable(schema, tableName);
if (table == null)
continue indexLoop;
final boolean unique = index.get(i.RDB$UNIQUE_FLAG, boolean.class);
// [#6310] [#6620] Function-based indexes are not yet supported
for (Record column : columns) if (table.getColumn(column.get(s.RDB$FIELD_NAME)) == null)
continue indexLoop;
result.add(new AbstractIndexDefinition(schema, indexName, table, unique) {
List<IndexColumnDefinition> indexColumns = new ArrayList<>();
{
for (Record column : columns) {
indexColumns.add(new DefaultIndexColumnDefinition(this, table.getColumn(column.get(s.RDB$FIELD_NAME)), SortOrder.ASC, column.get(s.RDB$FIELD_POSITION, int.class)));
}
}
@Override
protected List<IndexColumnDefinition> getIndexColumns0() {
return indexColumns;
}
});
}
return result;
}
Aggregations