@Override protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException { for (Record record : create() .select( concat(SYSTABLE.TABLE_NAME, val("__"), SYSIDX.INDEX_NAME).as("indexName"), SYSTABLE.TABLE_NAME, SYSTABCOL.COLUMN_NAME) .from(SYSIDX) .join(SYSIDXCOL) .on(SYSIDX.TABLE_ID.equal(SYSIDXCOL.TABLE_ID)) .and(SYSIDX.INDEX_ID.equal(SYSIDXCOL.INDEX_ID)) .join(SYSTABLE) .on(SYSIDXCOL.TABLE_ID.equal(SYSTABLE.TABLE_ID)) .join(SYSTABCOL) .on(SYSIDXCOL.TABLE_ID.equal(SYSTABCOL.TABLE_ID)) .and(SYSIDXCOL.COLUMN_ID.equal(SYSTABCOL.COLUMN_ID)) .where(SYSIDX.INDEX_CATEGORY.equal((byte) 1)) .orderBy(SYSIDXCOL.SEQUENCE) .fetch()) { String key = record.getValue("indexName", String.class); String tableName = record.getValue(SYSTABLE.TABLE_NAME); String columnName = record.getValue(SYSTABCOL.COLUMN_NAME); TableDefinition table = getTable(getSchema(), tableName); if (table != null) { relations.addPrimaryKey(key, table.getColumn(columnName)); } } }
@Override protected void loadForeignKeys(DefaultRelations relations) throws SQLException { Sysidx fkIndex = SYSIDX.as("fkIndex"); Sysidx ukIndex = SYSIDX.as("ukIndex"); Systable fkTable = SYSTABLE.as("fkTable"); Systable ukTable = SYSTABLE.as("ukTable"); for (Record record : create() .select( concat(fkTable.TABLE_NAME, val("__"), fkIndex.INDEX_NAME).as("fkIndexName"), fkTable.TABLE_NAME, SYSTABCOL.COLUMN_NAME, concat(ukTable.TABLE_NAME, val("__"), ukIndex.INDEX_NAME).as("ukIndexName")) .from(SYSFKEY) .join(fkIndex) .on(SYSFKEY.FOREIGN_INDEX_ID.equal(fkIndex.INDEX_ID)) .and(SYSFKEY.FOREIGN_TABLE_ID.equal(fkIndex.TABLE_ID)) .join(SYSIDXCOL) .on(fkIndex.INDEX_ID.equal(SYSIDXCOL.INDEX_ID)) .and(fkIndex.TABLE_ID.equal(SYSIDXCOL.TABLE_ID)) .join(fkTable) .on(SYSFKEY.FOREIGN_TABLE_ID.equal(fkTable.TABLE_ID)) .join(SYSTABCOL) .on(SYSIDXCOL.TABLE_ID.equal(SYSTABCOL.TABLE_ID)) .and(SYSIDXCOL.COLUMN_ID.equal(SYSTABCOL.COLUMN_ID)) .join(ukIndex) .on(SYSFKEY.PRIMARY_INDEX_ID.equal(ukIndex.INDEX_ID)) .and(SYSFKEY.PRIMARY_TABLE_ID.equal(ukIndex.TABLE_ID)) .join(ukTable) .on(SYSFKEY.PRIMARY_TABLE_ID.equal(ukTable.TABLE_ID)) .orderBy(fkTable.TABLE_NAME.asc(), fkIndex.INDEX_NAME.asc(), SYSIDXCOL.SEQUENCE.asc()) .fetch()) { String foreignKey = record.getValue("fkIndexName", String.class); String foreignKeyTableName = record.getValue(SYSTABLE.TABLE_NAME); String foreignKeyColumn = record.getValue(SYSTABCOL.COLUMN_NAME); String referencedKey = record.getValue("ukIndexName", String.class); TableDefinition foreignKeyTable = getTable(getSchema(), foreignKeyTableName); if (foreignKeyTable != null) { ColumnDefinition referencingColumn = foreignKeyTable.getColumn(foreignKeyColumn); relations.addForeignKey(foreignKey, referencedKey, referencingColumn, getSchema()); } } }