use of org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation in project hibernate-orm by hibernate.
the class InformationExtractorJdbcDatabaseMetaDataImpl method getTables.
public NameSpaceTablesInformation getTables(Identifier catalog, Identifier schema) {
String catalogFilter = null;
String schemaFilter = null;
if (extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsCatalogs()) {
if (catalog == null) {
if (extractionContext.getJdbcEnvironment().getCurrentCatalog() != null) {
// 1) look in current namespace
catalogFilter = toMetaDataObjectName(extractionContext.getJdbcEnvironment().getCurrentCatalog());
} else if (extractionContext.getDefaultCatalog() != null) {
// 2) look in default namespace
catalogFilter = toMetaDataObjectName(extractionContext.getDefaultCatalog());
} else {
catalogFilter = "";
}
} else {
catalogFilter = toMetaDataObjectName(catalog);
}
}
if (extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsSchemas()) {
if (schema == null) {
if (extractionContext.getJdbcEnvironment().getCurrentSchema() != null) {
// 1) look in current namespace
schemaFilter = toMetaDataObjectName(extractionContext.getJdbcEnvironment().getCurrentSchema());
} else if (extractionContext.getDefaultSchema() != null) {
// 2) look in default namespace
schemaFilter = toMetaDataObjectName(extractionContext.getDefaultSchema());
} else {
schemaFilter = "";
}
} else {
schemaFilter = toMetaDataObjectName(schema);
}
}
try {
ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getTables(catalogFilter, schemaFilter, "%", tableTypes);
final NameSpaceTablesInformation tablesInformation = processTableResults(resultSet);
populateTablesWithColumns(catalogFilter, schemaFilter, tablesInformation);
return tablesInformation;
} catch (SQLException sqlException) {
throw convertSQLException(sqlException, "Error accessing table metadata");
}
}
use of org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation 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);
}
}
}
use of org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation in project hibernate-orm by hibernate.
the class GroupedSchemaMigratorImpl method performTablesMigration.
@Override
protected NameSpaceTablesInformation performTablesMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
final NameSpaceTablesInformation tablesInformation = new NameSpaceTablesInformation(metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());
if (schemaFilter.includeNamespace(namespace)) {
createSchemaAndCatalog(existingDatabase, options, dialect, formatter, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
final NameSpaceTablesInformation tables = existingDatabase.getTablesInformation(namespace);
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
checkExportIdentifier(table, exportIdentifiers);
final TableInformation tableInformation = tables.getTableInformation(table);
if (tableInformation == null) {
createTable(table, dialect, metadata, formatter, options, targets);
} else if (tableInformation != null && tableInformation.isPhysicalTable()) {
tablesInformation.addTableInformation(tableInformation);
migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
final TableInformation tableInformation = tablesInformation.getTableInformation(table);
if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
applyUniqueKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
}
return tablesInformation;
}
use of org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation in project hibernate-orm by hibernate.
the class InformationExtractorJdbcDatabaseMetaDataImpl method processTableResults.
private NameSpaceTablesInformation processTableResults(ResultSet resultSet) throws SQLException {
try {
NameSpaceTablesInformation tables = new NameSpaceTablesInformation(identifierHelper());
while (resultSet.next()) {
final TableInformation tableInformation = extractTableInformation(resultSet);
tables.addTableInformation(tableInformation);
}
return tables;
} finally {
try {
resultSet.close();
} catch (SQLException ignore) {
}
}
}
use of org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation in project hibernate-orm by hibernate.
the class IndividuallySchemaMigratorImpl method performTablesMigration.
@Override
protected NameSpaceTablesInformation performTablesMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
final NameSpaceTablesInformation tablesInformation = new NameSpaceTablesInformation(metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());
if (schemaFilter.includeNamespace(namespace)) {
createSchemaAndCatalog(existingDatabase, options, dialect, formatter, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
checkExportIdentifier(table, exportIdentifiers);
final TableInformation tableInformation = existingDatabase.getTableInformation(table.getQualifiedTableName());
if (tableInformation == null) {
createTable(table, dialect, metadata, formatter, options, targets);
} else if (tableInformation != null && tableInformation.isPhysicalTable()) {
tablesInformation.addTableInformation(tableInformation);
migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
final TableInformation tableInformation = tablesInformation.getTableInformation(table);
if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
applyUniqueKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
}
return tablesInformation;
}
Aggregations