// ***************************
// *** ForeignKey ***
// ***************************
// trying to find table column with specified name
TableColumn tableColumn = table.findColumnByName(fkColumnName);
String errMessage = null;
// warn if null
if (tableColumn == null) {
errMessage = String.format("[Database %s] Unable to find table column '%5$s' for the table %s (schema %s, catalog %s)",
database.getName(),
tableName,
schemaName,
catalogName,
fkColumnName);
traceLog.debug(errMessage);
}
// if fail is enabled
if (failOnError && (tableColumn == null)) {
throw new DatabaseMetaDataMethodException(errMessage, "populateForeignKeys");
}
// create FK column
ForeignKeyColumn fkColumn = factory.createForeignKeyColumn();
// check if we found the original table column
if (tableColumn != null) {
// mark original table column as part of FK
tableColumn.setForeignKeyColumn(Boolean.TRUE);
// clone properties from original table column to the fkcolumn
PropertyUtils.copyProperties(fkColumn, tableColumn);
} else { // recovery if table column is not found but we still want to create fk column
// set name at least
fkColumn.setName(fkColumnName);
}
// modify ordinal position that correspond to the position in FK
fkColumn.setOrdinalPosition(ordinalPosition);
// check for PK table and corresponding PK column
Table pkTable = database.findTableByName(pkTableCatalogName, pkTableSchemaName, pkTableName);
// sets the scope table of a foreign key.
fk.setSourceTable(pkTable);
// find PK
PrimaryKey pk = (pkTable == null) ? null : pkTable.getPrimaryKey();
// set
fk.setSourcePrimaryKey(pk);
// What happens to a foreign key when the primary key is updated
fk.setUpdateRule(getKeyModifyRuleType(updateRule));
// What happens to a foreign key when the primary key is deleted
fk.setDeleteRule(getKeyModifyRuleType(deleteRule));
// Can the evaluation of foreign key constraints be deferred until commit
fk.setDeferrability(getKeyDeferrabilityType(defferability));
// find PK table column
TableColumn pkColumn = (pkTable == null) ? null : pkTable.findColumnByName(pkColumnName);
// Sets mapped source column (in PK/source table) for this foreign key column
fkColumn.setSourceColumn(pkColumn);
// add FK column to the foreign key
fk.addColumn(fkColumn);