use of org.hibernate.tool.schema.extract.spi.ForeignKeyInformation in project hibernate-orm by hibernate.
the class InformationExtractorJdbcDatabaseMetaDataImpl method getForeignKeys.
@Override
public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInformation) {
final Map<Identifier, ForeignKeyBuilder> fkBuilders = new HashMap<>();
final QualifiedTableName tableName = tableInformation.getName();
final Identifier catalog = tableName.getCatalogName();
final Identifier schema = tableName.getSchemaName();
final String catalogFilter;
final String schemaFilter;
if (catalog == null) {
catalogFilter = "";
} else {
catalogFilter = catalog.getText();
}
if (schema == null) {
schemaFilter = "";
} else {
schemaFilter = schema.getText();
}
try {
ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getImportedKeys(catalogFilter, schemaFilter, tableInformation.getName().getTableName().getText());
try {
while (resultSet.next()) {
// IMPL NOTE : The builder is mainly used to collect the column reference mappings
final Identifier fkIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("FK_NAME"));
ForeignKeyBuilder fkBuilder = fkBuilders.get(fkIdentifier);
if (fkBuilder == null) {
fkBuilder = generateForeignKeyBuilder(fkIdentifier);
fkBuilders.put(fkIdentifier, fkBuilder);
}
final QualifiedTableName incomingPkTableName = extractKeyTableName(resultSet, "PK");
final TableInformation pkTableInformation = extractionContext.getDatabaseObjectAccess().locateTableInformation(incomingPkTableName);
if (pkTableInformation == null) {
// should match.
continue;
}
final Identifier fkColumnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("FKCOLUMN_NAME"));
final Identifier pkColumnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("PKCOLUMN_NAME"));
fkBuilder.addColumnMapping(tableInformation.getColumn(fkColumnIdentifier), pkTableInformation.getColumn(pkColumnIdentifier));
}
} finally {
resultSet.close();
}
} catch (SQLException e) {
throw convertSQLException(e, "Error accessing column metadata: " + tableInformation.getName().toString());
}
final List<ForeignKeyInformation> fks = new ArrayList<ForeignKeyInformation>();
for (ForeignKeyBuilder fkBuilder : fkBuilders.values()) {
ForeignKeyInformation fk = fkBuilder.build();
fks.add(fk);
}
return fks;
}
use of org.hibernate.tool.schema.extract.spi.ForeignKeyInformation in project hibernate-orm by hibernate.
the class AbstractSchemaMigrator method applyForeignKeys.
protected void applyForeignKeys(Table table, TableInformation tableInformation, Dialect dialect, Metadata metadata, Formatter formatter, ExecutionOptions options, GenerationTarget... targets) {
if (dialect.hasAlterTable()) {
final Exporter<ForeignKey> exporter = dialect.getForeignKeyExporter();
@SuppressWarnings("unchecked") final Iterator<ForeignKey> fkItr = table.getForeignKeyIterator();
while (fkItr.hasNext()) {
final ForeignKey foreignKey = fkItr.next();
if (foreignKey.isPhysicalConstraint() && foreignKey.isCreationEnabled()) {
ForeignKeyInformation existingForeignKey = null;
if (tableInformation != null) {
existingForeignKey = findMatchingForeignKey(foreignKey, tableInformation);
}
if (existingForeignKey == null) {
// todo : shouldn't we just drop+recreate if FK exists?
// this follows the existing code from legacy SchemaUpdate which just skipped
// in old SchemaUpdate code, this was the trigger to "create"
applySqlStrings(false, exporter.getSqlCreateStrings(foreignKey, metadata), formatter, options, targets);
}
}
}
}
}
use of org.hibernate.tool.schema.extract.spi.ForeignKeyInformation in project hibernate-orm by hibernate.
the class TableInformationImpl method foreignKeys.
protected Map<Identifier, ForeignKeyInformation> foreignKeys() {
if (foreignKeys == null) {
final Map<Identifier, ForeignKeyInformation> fkMap = new HashMap<>();
final Iterable<ForeignKeyInformation> fks = extractor.getForeignKeys(this);
for (ForeignKeyInformation fk : fks) {
fkMap.put(fk.getForeignKeyIdentifier(), fk);
}
this.foreignKeys = fkMap;
}
return foreignKeys;
}
Aggregations