void addColumnPair(final int keySequence, final Column pkColumn, final Column fkColumn) { final String fkColumnMapName = getName() + "." + keySequence; final MutableForeignKeyColumnMap fkColumnPair = new MutableForeignKeyColumnMap(this, fkColumnMapName); fkColumnPair.setKeySequence(keySequence); fkColumnPair.setPrimaryKeyColumn(pkColumn); fkColumnPair.setForeignKeyColumn(fkColumn); addColumnPair(fkColumnPair); }
private void createForeignKeys( final MetadataResultSet results, final NamedObjectList<MutableForeignKey> foreignKeys) throws SQLException { try { while (results.next()) { String foreignKeyName = quotedName(results.getString("FK_NAME")); if (Utility.isBlank(foreignKeyName)) { foreignKeyName = UNKNOWN; } LOGGER.log(Level.FINER, "Retrieving foreign key: " + foreignKeyName); final String pkTableCatalogName = quotedName(results.getString("PKTABLE_CAT")); final String pkTableSchemaName = quotedName(results.getString("PKTABLE_SCHEM")); final String pkTableName = quotedName(results.getString("PKTABLE_NAME")); final String pkColumnName = quotedName(results.getString("PKCOLUMN_NAME")); final String fkTableCatalogName = quotedName(results.getString("FKTABLE_CAT")); final String fkTableSchemaName = quotedName(results.getString("FKTABLE_SCHEM")); final String fkTableName = quotedName(results.getString("FKTABLE_NAME")); final String fkColumnName = quotedName(results.getString("FKCOLUMN_NAME")); MutableForeignKey foreignKey = foreignKeys.lookup(foreignKeyName); if (foreignKey == null) { foreignKey = new MutableForeignKey(foreignKeyName); foreignKeys.add(foreignKey); } final int keySequence = results.getInt("KEY_SEQ", 0); final int updateRule = results.getInt("UPDATE_RULE", ForeignKeyUpdateRule.unknown.getId()); final int deleteRule = results.getInt("DELETE_RULE", ForeignKeyUpdateRule.unknown.getId()); final int deferrability = results.getInt("DEFERRABILITY", ForeignKeyDeferrability.unknown.getId()); final MutableColumn pkColumn = lookupOrCreateColumn(pkTableCatalogName, pkTableSchemaName, pkTableName, pkColumnName); final MutableColumn fkColumn = lookupOrCreateColumn(fkTableCatalogName, fkTableSchemaName, fkTableName, fkColumnName); // Make a direct connection between the two columns if (pkColumn != null && fkColumn != null) { foreignKey.addColumnPair(keySequence, pkColumn, fkColumn); foreignKey.setUpdateRule(ForeignKeyUpdateRule.valueOf(updateRule)); foreignKey.setDeleteRule(ForeignKeyUpdateRule.valueOf(deleteRule)); foreignKey.setDeferrability(ForeignKeyDeferrability.valueOf(deferrability)); foreignKey.addAttributes(results.getAttributes()); fkColumn.setReferencedColumn(pkColumn); ((MutableTable) pkColumn.getParent()).addForeignKey(foreignKey); ((MutableTable) fkColumn.getParent()).addForeignKey(foreignKey); } } } finally { results.close(); } }