use of org.hibernate.tool.schema.extract.spi.TableInformation in project hibernate-orm by hibernate.
the class InformationExtractorJdbcDatabaseMetaDataImpl method populateTablesWithColumns.
private void populateTablesWithColumns(String catalogFilter, String schemaFilter, NameSpaceTablesInformation tables) {
try {
ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getColumns(catalogFilter, schemaFilter, null, "%");
try {
String currentTableName = "";
TableInformation currentTable = null;
while (resultSet.next()) {
if (!currentTableName.equals(resultSet.getString("TABLE_NAME"))) {
currentTableName = resultSet.getString("TABLE_NAME");
currentTable = tables.getTableInformation(currentTableName);
}
if (currentTable != null) {
final ColumnInformationImpl columnInformation = new ColumnInformationImpl(currentTable, DatabaseIdentifier.toIdentifier(resultSet.getString("COLUMN_NAME")), resultSet.getInt("DATA_TYPE"), new StringTokenizer(resultSet.getString("TYPE_NAME"), "() ").nextToken(), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS"), interpretTruthValue(resultSet.getString("IS_NULLABLE")));
currentTable.addColumn(columnInformation);
}
}
} finally {
resultSet.close();
}
} catch (SQLException e) {
throw convertSQLException(e, "Error accessing tables metadata");
}
}
use of org.hibernate.tool.schema.extract.spi.TableInformation in project hibernate-orm by hibernate.
the class InformationExtractorJdbcDatabaseMetaDataImpl method processTableResults.
private TableInformation processTableResults(Identifier catalog, Identifier schema, Identifier tableName, ResultSet resultSet) throws SQLException {
try {
boolean found = false;
TableInformation tableInformation = null;
while (resultSet.next()) {
if (tableName.equals(Identifier.toIdentifier(resultSet.getString("TABLE_NAME"), tableName.isQuoted()))) {
if (found) {
log.multipleTablesFound(tableName.render());
final String catalogName = catalog == null ? "" : catalog.render();
final String schemaName = schema == null ? "" : schema.render();
throw new SchemaExtractionException(String.format(Locale.ENGLISH, "More than one table found in namespace (%s, %s) : %s", catalogName, schemaName, tableName.render()));
} else {
found = true;
tableInformation = extractTableInformation(resultSet);
addColumns(tableInformation);
}
}
}
if (!found) {
log.tableNotFound(tableName.render());
}
return tableInformation;
} finally {
try {
resultSet.close();
} catch (SQLException ignore) {
}
}
}
use of org.hibernate.tool.schema.extract.spi.TableInformation 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.TableInformation in project hibernate-orm by hibernate.
the class IndividuallySchemaValidatorImpl method validateTables.
@Override
protected void validateTables(Metadata metadata, DatabaseInformation databaseInformation, ExecutionOptions options, Dialect dialect, Namespace namespace) {
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
final TableInformation tableInformation = databaseInformation.getTableInformation(table.getQualifiedTableName());
validateTable(table, tableInformation, metadata, options, dialect);
}
}
}
use of org.hibernate.tool.schema.extract.spi.TableInformation in project hibernate-orm by hibernate.
the class AbstractSchemaMigrator method performMigration.
private void performMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, GenerationTarget... targets) {
final boolean format = Helper.interpretFormattingEnabled(options.getConfigurationValues());
final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
final Set<String> exportIdentifiers = new HashSet<String>(50);
final Database database = metadata.getDatabase();
// Drop all AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
}
// Create beforeQuery-table AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (!auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
}
}
boolean tryToCreateCatalogs = false;
boolean tryToCreateSchemas = false;
if (options.shouldManageNamespaces()) {
if (dialect.canCreateSchema()) {
tryToCreateSchemas = true;
}
if (dialect.canCreateCatalog()) {
tryToCreateCatalogs = true;
}
}
final Map<Namespace, NameSpaceTablesInformation> tablesInformation = new HashMap<>();
Set<Identifier> exportedCatalogs = new HashSet<>();
for (Namespace namespace : database.getNamespaces()) {
final NameSpaceTablesInformation nameSpaceTablesInformation = performTablesMigration(metadata, existingDatabase, options, dialect, formatter, exportIdentifiers, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
tablesInformation.put(namespace, nameSpaceTablesInformation);
if (schemaFilter.includeNamespace(namespace)) {
for (Sequence sequence : namespace.getSequences()) {
checkExportIdentifier(sequence, exportIdentifiers);
final SequenceInformation sequenceInformation = existingDatabase.getSequenceInformation(sequence.getName());
if (sequenceInformation == null) {
applySqlStrings(false, dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata), formatter, options, targets);
}
}
}
}
//NOTE : Foreign keys must be created *afterQuery* all tables of all namespaces for cross namespace fks. see HHH-10420
for (Namespace namespace : database.getNamespaces()) {
if (schemaFilter.includeNamespace(namespace)) {
final NameSpaceTablesInformation nameSpaceTablesInformation = tablesInformation.get(namespace);
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table)) {
final TableInformation tableInformation = nameSpaceTablesInformation.getTableInformation(table);
if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
applyForeignKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
}
}
// Create afterQuery-table AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
}
}
}
Aggregations