Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ else if ( i < paramIndexes.length ) {
sqlAppender.appendSql( chunks[i] );
}
if ( arg != null ) {
if ( caseWrapper &&
!( arg instanceof Distinct ) &&
!( arg instanceof Star ) ) {
if ( caseWrapper
&& !( arg instanceof Distinct )
&& !( arg instanceof Star ) ) {
translator.getCurrentClauseStack().push( Clause.WHERE );
sqlAppender.appendSql( "case when " );
filter.accept( translator );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
final Identifier currentSchema = getCurrentSchema();
final Identifier currentCatalog = getCurrentCatalog();
if ( currentCatalog != null || currentSchema != null ) {
final TableInformation tableInfo =
locateTableInNamespace( currentCatalog, currentSchema, tableName );
final var tableInfo = locateTableInNamespace( currentCatalog, currentSchema, tableName );
if ( tableInfo != null ) {
return tableInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.Sequence;
Expand All @@ -24,26 +22,18 @@
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.hibernate.engine.jdbc.internal.Formatter;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.Index;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
import org.hibernate.tool.schema.UniqueConstraintSchemaUpdateStrategy;
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
import org.hibernate.tool.schema.extract.spi.IndexInformation;
import org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation;
import org.hibernate.tool.schema.extract.spi.SequenceInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation;
import org.hibernate.tool.schema.spi.GenerationTarget;
import org.hibernate.tool.schema.internal.exec.JdbcContext;
import org.hibernate.tool.schema.spi.CommandAcceptanceException;
import org.hibernate.tool.schema.spi.ContributableMatcher;
import org.hibernate.tool.schema.spi.ExecutionOptions;
import org.hibernate.tool.schema.spi.Exporter;
import org.hibernate.tool.schema.spi.SchemaFilter;
import org.hibernate.tool.schema.spi.SchemaManagementException;
import org.hibernate.tool.schema.spi.SchemaMigrator;
Expand All @@ -54,8 +44,12 @@
import static org.hibernate.cfg.SchemaToolingSettings.UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY;
import static org.hibernate.engine.config.spi.StandardConverters.STRING;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize;
import static org.hibernate.tool.schema.UniqueConstraintSchemaUpdateStrategy.DROP_RECREATE_QUIETLY;
import static org.hibernate.tool.schema.UniqueConstraintSchemaUpdateStrategy.SKIP;
import static org.hibernate.tool.schema.internal.Helper.buildDatabaseInformation;
import static org.hibernate.tool.schema.internal.Helper.interpretFormattingEnabled;
import static org.hibernate.tool.schema.internal.SchemaCreatorImpl.createUserDefinedTypes;
import static org.hibernate.tool.schema.internal.SchemaDropperImpl.dropUserDefinedTypes;

Expand All @@ -82,25 +76,20 @@ public void doMigration(
ExecutionOptions options,
ContributableMatcher contributableInclusionFilter,
TargetDescriptor targetDescriptor) {
final SqlStringGenerationContext sqlGenerationContext = sqlGenerationContext( metadata, options );
final var sqlGenerationContext = sqlGenerationContext( metadata, options );
if ( !targetDescriptor.getTargetTypes().isEmpty() ) {
final JdbcContext jdbcContext = tool.resolveJdbcContext( options.getConfigurationValues() );
try ( DdlTransactionIsolator ddlTransactionIsolator = tool.getDdlTransactionIsolator( jdbcContext ) ) {
final DatabaseInformation databaseInformation = Helper.buildDatabaseInformation(
tool.getServiceRegistry(),
ddlTransactionIsolator,
sqlGenerationContext,
tool
);

final GenerationTarget[] targets = tool.buildGenerationTargets(
final var jdbcContext = tool.resolveJdbcContext( options.getConfigurationValues() );
try ( var isolator = tool.getDdlTransactionIsolator( jdbcContext ) ) {
final var databaseInformation =
buildDatabaseInformation( isolator, sqlGenerationContext, tool );
final var targets = tool.buildGenerationTargets(
targetDescriptor,
ddlTransactionIsolator,
isolator,
options.getConfigurationValues()
);

try {
for ( GenerationTarget target : targets ) {
for ( var target : targets ) {
target.prepare();
}

Expand All @@ -116,7 +105,7 @@ public void doMigration(
);
}
finally {
for ( GenerationTarget target : targets ) {
for ( var target : targets ) {
try {
target.release();
}
Expand Down Expand Up @@ -169,16 +158,16 @@ private void performMigration(
Dialect dialect,
SqlStringGenerationContext sqlGenerationContext,
GenerationTarget... targets) {
final boolean format = Helper.interpretFormattingEnabled( options.getConfigurationValues() );
final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
final boolean format = interpretFormattingEnabled( options.getConfigurationValues() );
final var formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();

final Set<String> exportIdentifiers = CollectionHelper.setOfSize( 50 );
final Set<String> exportIdentifiers = setOfSize( 50 );

final Database database = metadata.getDatabase();
final Exporter<AuxiliaryDatabaseObject> auxiliaryExporter = dialect.getAuxiliaryDatabaseObjectExporter();
final var database = metadata.getDatabase();
final var auxiliaryExporter = dialect.getAuxiliaryDatabaseObjectExporter();

// Drop all AuxiliaryDatabaseObjects
for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
for ( var auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
if ( auxiliaryDatabaseObject.appliesToDialect( dialect ) ) {
applySqlStrings(
true,
Expand All @@ -194,7 +183,7 @@ private void performMigration(
dropUserDefinedTypes( metadata, options, schemaFilter, dialect, formatter, sqlGenerationContext, targets );

// Create before-table AuxiliaryDatabaseObjects
for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
for ( var auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
if ( auxiliaryDatabaseObject.beforeTablesOnCreation()
&& auxiliaryDatabaseObject.appliesToDialect( dialect ) ) {
applySqlStrings(
Expand Down Expand Up @@ -222,8 +211,8 @@ private void performMigration(
}
final Map<Namespace, NameSpaceTablesInformation> tablesInformation = new HashMap<>();
final Set<Identifier> exportedCatalogs = new HashSet<>();
for ( Namespace namespace : database.getNamespaces() ) {
final NameSpaceTablesInformation nameSpaceTablesInformation = performTablesMigration(
for ( var namespace : database.getNamespaces() ) {
final var nameSpaceTablesInformation = performTablesMigration(
metadata,
existingDatabase,
options,
Expand All @@ -239,10 +228,10 @@ private void performMigration(
);
tablesInformation.put( namespace, nameSpaceTablesInformation );
if ( schemaFilter.includeNamespace( namespace ) ) {
for ( Sequence sequence : namespace.getSequences() ) {
for ( var sequence : namespace.getSequences() ) {
if ( contributableInclusionFilter.matches( sequence ) ) {
checkExportIdentifier( sequence, exportIdentifiers);
final SequenceInformation sequenceInformation = existingDatabase.getSequenceInformation( sequence.getName() );
final var sequenceInformation = existingDatabase.getSequenceInformation( sequence.getName() );
if ( sequenceInformation == null ) {
applySequence( sequence, dialect, metadata, formatter, options, sqlGenerationContext, targets );
}
Expand All @@ -252,12 +241,12 @@ private void performMigration(
}

//NOTE: Foreign keys must be created *after* all tables of all namespaces for cross-namespace fks. see HHH-10420
for ( Namespace namespace : database.getNamespaces() ) {
for ( var namespace : database.getNamespaces() ) {
if ( schemaFilter.includeNamespace( namespace ) ) {
final NameSpaceTablesInformation nameSpaceTablesInformation = tablesInformation.get( namespace );
for ( Table table : namespace.getTables() ) {
final var nameSpaceTablesInformation = tablesInformation.get( namespace );
for ( var table : namespace.getTables() ) {
if ( schemaFilter.includeTable( table ) && contributableInclusionFilter.matches( table ) ) {
final TableInformation tableInformation = nameSpaceTablesInformation.getTableInformation( table );
final var tableInformation = nameSpaceTablesInformation.getTableInformation( table );
if ( tableInformation == null || tableInformation.isPhysicalTable() ) {
applyForeignKeys( table, tableInformation, dialect, metadata, formatter, options,
sqlGenerationContext, targets );
Expand All @@ -268,8 +257,9 @@ private void performMigration(
}

// Create after-table AuxiliaryDatabaseObjects
for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
if ( !auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect( dialect ) ) {
for ( var auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
if ( !auxiliaryDatabaseObject.beforeTablesOnCreation()
&& auxiliaryDatabaseObject.appliesToDialect( dialect ) ) {
applySqlStrings(
true,
auxiliaryExporter.getSqlCreateStrings( auxiliaryDatabaseObject, metadata, sqlGenerationContext ),
Expand All @@ -291,7 +281,8 @@ private static void applySequence(
GenerationTarget... targets) {
applySqlStrings(
false,
dialect.getSequenceExporter().getSqlCreateStrings( sequence, metadata, sqlGenerationContext ),
dialect.getSequenceExporter()
.getSqlCreateStrings( sequence, metadata, sqlGenerationContext ),
formatter,
options,
targets
Expand All @@ -308,7 +299,8 @@ protected void createTable(
GenerationTarget... targets) {
applySqlStrings(
false,
dialect.getTableExporter().getSqlCreateStrings( table, metadata, sqlGenerationContext ),
dialect.getTableExporter()
.getSqlCreateStrings( table, metadata, sqlGenerationContext ),
formatter,
options,
targets
Expand Down Expand Up @@ -343,13 +335,13 @@ protected void applyIndexes(
ExecutionOptions options,
SqlStringGenerationContext sqlGenerationContext,
GenerationTarget... targets) {
final Exporter<Index> exporter = dialect.getIndexExporter();
for ( Index index : table.getIndexes().values() ) {
final var exporter = dialect.getIndexExporter();
for ( var index : table.getIndexes().values() ) {
if ( !isEmpty( index.getName() ) ) {
IndexInformation existingIndex = null;
if ( tableInformation != null ) {
existingIndex = findMatchingIndex( index, tableInformation );
}
final var existingIndex =
tableInformation != null
? findMatchingIndex( index, tableInformation )
: null;
if ( existingIndex == null ) {
applySqlStrings(
false,
Expand Down Expand Up @@ -381,15 +373,15 @@ protected void applyUniqueKeys(
}

if ( uniqueConstraintStrategy != SKIP ) {
final Exporter<UniqueKey> exporter = dialect.getUniqueKeyExporter();
for ( UniqueKey uniqueKey : table.getUniqueKeys().values() ) {
final var exporter = dialect.getUniqueKeyExporter();
for ( var uniqueKey : table.getUniqueKeys().values() ) {
// Skip if index already exists. Most of the time, this
// won't work since most Dialects use Constraints. However,
// keep it for the few that do use Indexes.
IndexInformation indexInfo = null;
if ( tableInfo != null && StringHelper.isNotEmpty( uniqueKey.getName() ) ) {
indexInfo = tableInfo.getIndex( Identifier.toIdentifier( uniqueKey.getName() ) );
}
final var indexInfo =
tableInfo != null && isNotEmpty( uniqueKey.getName() )
? tableInfo.getIndex( Identifier.toIdentifier( uniqueKey.getName() ) )
: null;
if ( indexInfo == null ) {
if ( uniqueConstraintStrategy == DROP_RECREATE_QUIETLY ) {
applySqlStrings(
Expand Down Expand Up @@ -430,8 +422,8 @@ protected void applyForeignKeys(
SqlStringGenerationContext sqlGenerationContext,
GenerationTarget... targets) {
if ( dialect.hasAlterTable() ) {
final Exporter<ForeignKey> exporter = dialect.getForeignKeyExporter();
for ( ForeignKey foreignKey : table.getForeignKeyCollection() ) {
final var exporter = dialect.getForeignKeyExporter();
for ( var foreignKey : table.getForeignKeyCollection() ) {
if ( foreignKey.isPhysicalConstraint()
&& foreignKey.isCreationEnabled()
&& ( tableInformation == null || !checkForExistingForeignKey( foreignKey, tableInformation ) ) ) {
Expand Down Expand Up @@ -478,8 +470,8 @@ boolean equivalentForeignKeyExistsInDatabase(TableInformation tableInformation,
return StreamSupport.stream( tableInformation.getForeignKeys().spliterator(), false )
.flatMap( foreignKeyInformation -> StreamSupport.stream( foreignKeyInformation.getColumnReferenceMappings().spliterator(), false ) )
.anyMatch( columnReferenceMapping -> {
final ColumnInformation referencingColumnMetadata = columnReferenceMapping.getReferencingColumnMetadata();
final ColumnInformation referencedColumnMetadata = columnReferenceMapping.getReferencedColumnMetadata();
final var referencingColumnMetadata = columnReferenceMapping.getReferencingColumnMetadata();
final var referencedColumnMetadata = columnReferenceMapping.getReferencedColumnMetadata();
final String existingReferencingColumn = referencingColumnMetadata.getColumnIdentifier().getText();
final String existingReferencedTable =
referencedColumnMetadata.getContainingTableInformation().getName().getTableName().getCanonicalName();
Expand Down Expand Up @@ -564,7 +556,7 @@ private static void applySqlString(
GenerationTarget... targets) {
if ( !isEmpty( sql ) ) {
final String formattedSql = formatter.format( sql );
for ( GenerationTarget target : targets ) {
for ( var target : targets ) {
try {
target.accept( formattedSql );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hibernate.tool.schema.spi.SqlScriptCommandExtractor;

import java.net.URL;
import java.util.Map;

import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_CHARSET_NAME;
import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_IMPORT_FILES;
Expand All @@ -45,9 +44,7 @@ void applyImportSources(
boolean format,
Dialect dialect,
GenerationTarget... targets) {

final Formatter formatter = getImportScriptFormatter(format);

final var formatter = getImportScriptFormatter(format);
boolean hasDefaultImportFileScriptBeenExecuted = applyImportScript(
options,
commandExtractor,
Expand Down Expand Up @@ -103,7 +100,7 @@ private boolean applyImportScript(
GenerationTarget[] targets) {
final Object importScriptSetting = getImportScriptSetting( options );
if ( importScriptSetting != null ) {
final ScriptSourceInput importScriptInput =
final var importScriptInput =
interpretScriptSourceSetting( importScriptSetting, getClassLoaderService(), getCharsetName( options ) );
applyScript(
options,
Expand All @@ -124,8 +121,10 @@ private boolean containsDefaultImportFile(ScriptSourceInput importScriptInput,Ex
if ( skipDefaultFileImport( options ) ) {
return false;
}
final URL defaultImportFileUrl = getClassLoaderService().locateResource( DEFAULT_IMPORT_FILE );
return defaultImportFileUrl != null && importScriptInput.containsScript( defaultImportFileUrl );
else {
final URL defaultImportFileUrl = getClassLoaderService().locateResource( DEFAULT_IMPORT_FILE );
return defaultImportFileUrl != null && importScriptInput.containsScript( defaultImportFileUrl );
}
}

/**
Expand All @@ -143,15 +142,14 @@ private void applyImportFiles(
StringHelper.split( ",",
getString( HBM2DDL_IMPORT_FILES, options.getConfigurationValues(), defaultImportFile ) );
final String charsetName = getCharsetName( options );
final ClassLoaderService classLoaderService = getClassLoaderService();
final var classLoaderService = getClassLoaderService();
for ( String currentFile : importFiles ) {
final String resourceName = currentFile.trim();
if ( !resourceName.isEmpty() ) { //skip empty resource names
if ( !currentFile.isBlank() ) { //skip empty resource names
applyScript(
options,
commandExtractor,
dialect,
interpretLegacyImportScriptSetting( resourceName, classLoaderService, charsetName ),
interpretLegacyImportScriptSetting( currentFile.trim(), classLoaderService, charsetName ),
formatter,
targets
);
Expand Down Expand Up @@ -187,7 +185,7 @@ private static String getCharsetName(ExecutionOptions options) {
* @return a {@link java.io.Reader} or a string URL
*/
private static Object getImportScriptSetting(ExecutionOptions options) {
final Map<String, Object> configuration = options.getConfigurationValues();
final var configuration = options.getConfigurationValues();
final Object importScriptSetting = configuration.get( HBM2DDL_LOAD_SCRIPT_SOURCE );
return importScriptSetting == null
? configuration.get( JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE )
Expand Down
Loading