@Test public void fkUtilities(final TestContext testContext, final Connection connection) throws Exception { final SchemaCrawlerOptionsBuilder schemaCrawlerOptionsBuilder = SchemaCrawlerOptionsBuilder .builder().withSchemaInfoLevel(SchemaInfoLevelBuilder.maximum()); final SchemaCrawlerOptions schemaCrawlerOptions = schemaCrawlerOptionsBuilder .toOptions(); final Catalog catalog = getCatalog(connection, schemaCrawlerOptions); final Schema schema = catalog.lookupSchema("PUBLIC.BOOKS").get(); assertThat("BOOKS Schema not found", schema, notNullValue()); final Table table = catalog.lookupTable(schema, "BOOKS").get(); assertThat("BOOKS Table not found", table, notNullValue()); final ForeignKey fk = table.getForeignKeys().toArray(new ForeignKey[0])[0]; assertThat("Foreign key not found", fk, notNullValue()); final ColumnReference columnReference = fk.getColumnReferences() .toArray(new ColumnReference[0])[0]; assertThat("Column reference not found", columnReference, notNullValue()); assertThat(MetaDataUtility .constructForeignKeyName(columnReference.getForeignKeyColumn(), columnReference.getPrimaryKeyColumn()), is("SC_AA4376_AFD2BA21")); assertThat(MetaDataUtility.findForeignKeyCardinality(fk), is(ForeignKeyCardinality.zero_many)); assertThat(MetaDataUtility.foreignKeyColumnNames(fk), containsInAnyOrder("PUBLIC.BOOKS.BOOKAUTHORS.BOOKID")); }
public static final List<String> foreignKeyColumnNames(final ForeignKey foreignKey) { if (foreignKey == null) { return Collections.emptyList(); } final List<String> columnNames = new ArrayList<>(); for (final ForeignKeyColumnReference columnReference: foreignKey .getColumnReferences()) { columnNames.add(columnReference.getForeignKeyColumn().getFullName()); } return columnNames; }
private Map<String, ForeignKeyColumnReference> mapForeignKeyColumns(final List<Table> tables) { final Map<String, ForeignKeyColumnReference> fkColumnsMap = new HashMap<>(); for (final Table table: tables) { for (final ForeignKey fk: table.getForeignKeys()) { for (final ForeignKeyColumnReference fkMap: fk.getColumnReferences()) { fkColumnsMap.put(fkMap.getForeignKeyColumn().getFullName(), fkMap); } } } return fkColumnsMap; }
/** * {@inheritDoc} * <p> * Note: Since foreign keys are not always explicitly named in * databases, the sorting routine orders the foreign keys by the names * of the columns in the foreign keys. * </p> */ @Override public int compareTo(final NamedObject obj) { if (obj == null) { return -1; } final ForeignKey other = (ForeignKey) obj; final List<ForeignKeyColumnReference> thisColumnReferences = getColumnReferences(); final List<ForeignKeyColumnReference> otherColumnReferences = other .getColumnReferences(); return CompareUtility.compareLists(thisColumnReferences, otherColumnReferences); }
@Override public ImmutableList<DaColumnReference> getColumnReferences() { return ListAdapter.adapt(fk.getColumnReferences()) .collect(new Function<ForeignKeyColumnReference, DaColumnReference>() { @Override public DaColumnReference valueOf(ForeignKeyColumnReference fk) { return new DaColumnReferenceImpl(fk, schemaStrategy); } }) .toImmutable(); }
boolean isImportedKey = false; for (final ForeignKeyColumnReference columnReference: mutableForeignKey .getColumnReferences())
private static Map<List<String>, String> extractForeignKeys(Table table) { Collection<ForeignKey> foreignKeys = table.getForeignKeys(); Map<List<String>, String> fks = fks = new LinkedHashMap<>(10); if (foreignKeys != null) { for (ForeignKey foreignKey : foreignKeys) { // todo handle composite keys List<ForeignKeyColumnReference> columnReferences = foreignKey.getColumnReferences(); if (columnReferences.isEmpty()) continue; ForeignKeyColumnReference firstReference = columnReferences.get(0); String otherTableName = firstReference.getPrimaryKeyColumn().getParent().getName(); List<String> keys = new ArrayList<>(3); for (ForeignKeyColumnReference reference : columnReferences) { // todo assert that all have the same parent Table otherTable = reference.getPrimaryKeyColumn().getParent(); Table thisTable = reference.getForeignKeyColumn().getParent(); if (otherTable.equals(table) && !thisTable.equals(table)) continue; if (!otherTable.getName().equals(otherTableName)) { throw new IllegalStateException("Foreign Key to different tables " + reference + " inconsistent with " + firstReference); } keys.add(reference.getForeignKeyColumn().getName()); } if (!keys.isEmpty()) fks.put(keys, otherTableName); } } return fks; }
private void printForeignKeys(final Table table) { for (final ForeignKey foreignKey: table.getForeignKeys()) { boolean isForeignKeyUnique = isForeignKeyUnique(foreignKey, table); for (final ColumnReference columnReference: foreignKey .getColumnReferences()) { if (table.equals(columnReference.getPrimaryKeyColumn().getParent())) { out.write(printColumnReference(foreignKey.getName(), columnReference, isForeignKeyUnique)); } } } }
printColumnReferences(tableName, foreignKey.getColumnReferences() .toArray(new ColumnReference[0]));
TablesGraph(final NamedObjectList<MutableTable> tables) { if (tables == null) { return; } for (final Table table: tables) { addVertex(table); for (final ForeignKey foreignKey: table.getForeignKeys()) { for (final ForeignKeyColumnReference columnReference: foreignKey .getColumnReferences()) { addDirectedEdge(columnReference.getPrimaryKeyColumn().getParent(), columnReference.getForeignKeyColumn().getParent()); } } } }
.getColumnReferences(); jsonFk.put("columnReferences", handleColumnReferences(columnReferences));
for (Table table: catalog.getTables(schema)) { for (ForeignKey fk: table.getForeignKeys()) { for (ForeignKeyColumnReference fkRef: fk.getColumnReferences()) { Column fkCol = fkRef.getForeignKeyColumn(); Column pkCol = fkRef.getPrimaryKeyColumn();