private static Element createForeignKeyColumnElement( final Document dom, final ForeignKeyColumnReference fkcm) { final Element eForeignKey = dom.createElement("column"); final Column referencingColumn = fkcm.getForeignKeyColumn(); final Column referencedColumn = fkcm.getPrimaryKeyColumn(); eForeignKey.setAttribute("name", referencingColumn.getName()); eForeignKey.setAttribute("referencedtable", referencedColumn.getParent().getName()); eForeignKey.setAttribute("referencedcolumn", referencedColumn.getName()); return eForeignKey; }
private static boolean isOutgoingForeignKey(final Table table, final ForeignKey foreignKey) { final List<ForeignKeyColumnReference> fkcms = foreignKey.getColumnReferences(); ThreadContext.assertFault( fkcms.size() > 0, "Table [%s] reports a foreign key [%s] with no columns", table.getName(), foreignKey.getName()); final ForeignKeyColumnReference fkcm = fkcms.get(0); final Column referencedColumn = fkcm.getPrimaryKeyColumn(); final String referencedtableName = referencedColumn.getParent().getName(); return !referencedtableName.equals(table.getName()); }
private void createForeignKeys( final MetadataResultSet results, final NamedObjectList<MutableForeignKey> foreignKeys) throws SQLException { try { while (results.next()) { String foreignKeyName = quotedName(results.getString("FK_NAME")); 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")); 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 Column pkColumn = lookupOrCreateColumn(pkTableCatalogName, pkTableSchemaName, pkTableName, pkColumnName); final Column fkColumn = lookupOrCreateColumn(fkTableCatalogName, fkTableSchemaName, fkTableName, fkColumnName); final boolean isPkColumnPartial = pkColumn instanceof ColumnPartial; final boolean isFkColumnPartial = fkColumn instanceof ColumnPartial; if (pkColumn == null || fkColumn == null || (isFkColumnPartial && isPkColumnPartial)) { continue; } if (Utility.isBlank(foreignKeyName)) { foreignKeyName = MetaDataUtility.constructForeignKeyName(pkColumn, fkColumn); } final Optional<MutableForeignKey> foreignKeyOptional = foreignKeys.lookup(foreignKeyName); final MutableForeignKey foreignKey; if (foreignKeyOptional.isPresent()) { foreignKey = foreignKeyOptional.get(); } else { foreignKey = new MutableForeignKey(foreignKeyName); foreignKeys.add(foreignKey); } foreignKey.addColumnReference(keySequence, pkColumn, fkColumn); foreignKey.setUpdateRule(ForeignKeyUpdateRule.valueOf(updateRule)); foreignKey.setDeleteRule(ForeignKeyUpdateRule.valueOf(deleteRule)); foreignKey.setDeferrability(ForeignKeyDeferrability.valueOf(deferrability)); foreignKey.addAttributes(results.getAttributes()); if (fkColumn instanceof MutableColumn) { ((MutableColumn) fkColumn).setReferencedColumn(pkColumn); ((MutableTable) fkColumn.getParent()).addForeignKey(foreignKey); } else if (isFkColumnPartial) { ((ColumnPartial) fkColumn).setReferencedColumn(pkColumn); ((TablePartial) fkColumn.getParent()).addForeignKey(foreignKey); } if (pkColumn instanceof MutableColumn) { ((MutableTable) pkColumn.getParent()).addForeignKey(foreignKey); } else if (isPkColumnPartial) { ((TablePartial) pkColumn.getParent()).addForeignKey(foreignKey); } } } finally { results.close(); } }