From e112eae4b89a60c1e430cd6bbb09493372ad8a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 27 Mar 2023 15:57:32 +0200 Subject: [PATCH] HHH-16255 De-duplicate a few methods from SchemaCreatorImpl/SchemaDropperImpl/SchemaTruncatorImpl --- .../tool/schema/internal/Helper.java | 69 ++++++++++++++++++ .../schema/internal/SchemaCreatorImpl.java | 72 ++----------------- .../schema/internal/SchemaDropperImpl.java | 66 ++--------------- .../schema/internal/SchemaTruncatorImpl.java | 59 ++------------- 4 files changed, 88 insertions(+), 178 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java index cc82da1840..014280a4cf 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java @@ -11,21 +11,29 @@ import java.io.Reader; import java.io.Writer; import java.net.URL; import java.sql.SQLException; +import java.util.List; import java.util.Map; import java.util.regex.Pattern; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.model.relational.Database; import org.hibernate.boot.model.relational.SqlStringGenerationContext; +import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; +import org.hibernate.engine.jdbc.internal.Formatter; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.resource.transaction.spi.DdlTransactionIsolator; import org.hibernate.service.ServiceRegistry; import org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl; import org.hibernate.tool.schema.extract.spi.DatabaseInformation; import org.hibernate.tool.schema.internal.exec.AbstractScriptSourceInput; +import org.hibernate.tool.schema.internal.exec.GenerationTarget; import org.hibernate.tool.schema.internal.exec.ScriptSourceInputAggregate; import org.hibernate.tool.schema.internal.exec.ScriptSourceInputFromFile; import org.hibernate.tool.schema.internal.exec.ScriptSourceInputFromReader; @@ -33,9 +41,12 @@ import org.hibernate.tool.schema.internal.exec.ScriptSourceInputFromUrl; import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile; import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToUrl; import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter; +import org.hibernate.tool.schema.spi.CommandAcceptanceException; +import org.hibernate.tool.schema.spi.ExecutionOptions; import org.hibernate.tool.schema.spi.SchemaManagementTool; import org.hibernate.tool.schema.spi.ScriptSourceInput; import org.hibernate.tool.schema.spi.ScriptTargetOutput; +import org.hibernate.tool.schema.spi.SqlScriptCommandExtractor; /** * Helper methods. @@ -185,4 +196,62 @@ public class Helper { throw jdbcEnvironment.getSqlExceptionHelper().convert( e, "Unable to build DatabaseInformation" ); } } + + public static SqlStringGenerationContext createSqlStringGenerationContext(ExecutionOptions options, Metadata metadata) { + final Database database = metadata.getDatabase(); + return SqlStringGenerationContextImpl.fromConfigurationMap( + database.getJdbcEnvironment(), + database, + options.getConfigurationValues() + ); + } + + public static void applySqlStrings( + String[] sqlStrings, + Formatter formatter, + ExecutionOptions options, + GenerationTarget... targets) { + if ( sqlStrings == null ) { + return; + } + + for ( String sqlString : sqlStrings ) { + applySqlString( sqlString, formatter, options, targets ); + } + } + + public static void applySqlString( + String sqlString, + Formatter formatter, + ExecutionOptions options, + GenerationTarget... targets) { + if ( StringHelper.isEmpty( sqlString ) ) { + return; + } + + String sqlStringFormatted = formatter.format( sqlString ); + for ( GenerationTarget target : targets ) { + try { + target.accept( sqlStringFormatted ); + } + catch (CommandAcceptanceException e) { + options.getExceptionHandler().handleException( e ); + } + } + } + + public static void applyScript( + ExecutionOptions options, + SqlScriptCommandExtractor commandExtractor, + Dialect dialect, + ScriptSourceInput scriptInput, + Formatter formatter, + GenerationTarget[] targets) { + final List commands = scriptInput.extract( + reader -> commandExtractor.extractCommands( reader, dialect ) + ); + for ( String command : commands ) { + applySqlString( command, formatter, options, targets ); + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java index be354c3b5c..b4aeb0edb5 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java @@ -18,13 +18,11 @@ import org.hibernate.Internal; 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.InitCommand; import org.hibernate.boot.model.relational.Namespace; import org.hibernate.boot.model.relational.Sequence; import org.hibernate.boot.model.relational.SqlStringGenerationContext; -import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.dialect.Dialect; @@ -46,7 +44,6 @@ import org.hibernate.tool.schema.internal.exec.GenerationTarget; import org.hibernate.tool.schema.internal.exec.JdbcContext; import org.hibernate.tool.schema.internal.exec.ScriptSourceInputFromUrl; import org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl; -import org.hibernate.tool.schema.spi.CommandAcceptanceException; import org.hibernate.tool.schema.spi.ContributableMatcher; import org.hibernate.tool.schema.spi.ExceptionHandler; import org.hibernate.tool.schema.spi.ExecutionOptions; @@ -63,9 +60,11 @@ import static org.hibernate.cfg.AvailableSettings.HBM2DDL_CHARSET_NAME; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_IMPORT_FILES; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_LOAD_SCRIPT_SOURCE; import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE; -import static org.hibernate.internal.util.StringHelper.isNotEmpty; import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize; import static org.hibernate.internal.util.config.ConfigurationHelper.getString; +import static org.hibernate.tool.schema.internal.Helper.applyScript; +import static org.hibernate.tool.schema.internal.Helper.applySqlStrings; +import static org.hibernate.tool.schema.internal.Helper.createSqlStringGenerationContext; import static org.hibernate.tool.schema.internal.Helper.interpretFormattingEnabled; import static org.hibernate.tool.schema.internal.Helper.interpretScriptSourceSetting; @@ -200,12 +199,7 @@ public class SchemaCreatorImpl implements SchemaCreator { Dialect dialect, ExecutionOptions options, GenerationTarget... targets) { - final List commands = scriptSourceInput.extract( - reader -> commandExtractor.extractCommands( reader, dialect ) - ); - for ( String command : commands ) { - applySqlString( command, formatter, options, targets ); - } + applyScript( options, commandExtractor, dialect, scriptSourceInput, formatter, targets ); } @Internal @@ -225,15 +219,6 @@ public class SchemaCreatorImpl implements SchemaCreator { ); } - private static SqlStringGenerationContext createSqlStringGenerationContext(ExecutionOptions options, Metadata metadata) { - final Database database = metadata.getDatabase(); - return SqlStringGenerationContextImpl.fromConfigurationMap( - database.getJdbcEnvironment(), - database, - options.getConfigurationValues() - ); - } - @Internal public void createFromMetadata( Metadata metadata, @@ -565,36 +550,6 @@ public class SchemaCreatorImpl implements SchemaCreator { exportIdentifiers.add( exportIdentifier ); } - private static void applySqlStrings( - String[] sqlStrings, - Formatter formatter, - ExecutionOptions options, - GenerationTarget... targets) { - if ( sqlStrings != null ) { - for ( String sqlString : sqlStrings ) { - applySqlString( sqlString, formatter, options, targets ); - } - } - } - - private static void applySqlString( - String sqlString, - Formatter formatter, - ExecutionOptions options, - GenerationTarget... targets) { - if ( isNotEmpty( sqlString ) ) { - try { - final String sqlStringFormatted = formatter.format( sqlString ); - for ( GenerationTarget target : targets ) { - target.accept( sqlStringFormatted ); - } - } - catch (CommandAcceptanceException e) { - options.getExceptionHandler().handleException( e ); - } - } - } - private void applyImportSources( ExecutionOptions options, SqlScriptCommandExtractor commandExtractor, @@ -653,7 +608,7 @@ public class SchemaCreatorImpl implements SchemaCreator { if ( importScriptSetting != null ) { final ScriptSourceInput importScriptInput = interpretScriptSourceSetting( importScriptSetting, getClassLoaderService(), getCharsetName( options ) ); - applyImportScript( + applyScript( options, commandExtractor, dialect, @@ -692,7 +647,7 @@ public class SchemaCreatorImpl implements SchemaCreator { for ( String currentFile : importFiles ) { final String resourceName = currentFile.trim(); if ( !resourceName.isEmpty() ) { //skip empty resource names - applyImportScript( + applyScript( options, commandExtractor, dialect, @@ -704,21 +659,6 @@ public class SchemaCreatorImpl implements SchemaCreator { } } - private static void applyImportScript( - ExecutionOptions options, - SqlScriptCommandExtractor commandExtractor, - Dialect dialect, - ScriptSourceInput importScriptInput, - Formatter formatter, - GenerationTarget[] targets) { - final List commands = importScriptInput.extract( - reader -> commandExtractor.extractCommands( reader, dialect ) - ); - for ( String command : commands ) { - applySqlString( command, formatter, options, targets ); - } - } - private ScriptSourceInput interpretLegacyImportScriptSetting( String resourceName, ClassLoaderService classLoaderService, diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java index 9737ff8f10..a3d416a990 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java @@ -19,12 +19,10 @@ import org.hibernate.Internal; 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; import org.hibernate.boot.model.relational.SqlStringGenerationContext; -import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl; import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.dialect.Dialect; import org.hibernate.engine.config.spi.ConfigurationService; @@ -63,8 +61,10 @@ import org.hibernate.tool.schema.spi.TargetDescriptor; import org.jboss.logging.Logger; -import static org.hibernate.internal.util.StringHelper.isNotEmpty; import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize; +import static org.hibernate.tool.schema.internal.Helper.applyScript; +import static org.hibernate.tool.schema.internal.Helper.applySqlStrings; +import static org.hibernate.tool.schema.internal.Helper.createSqlStringGenerationContext; import static org.hibernate.tool.schema.internal.Helper.interpretFormattingEnabled; /** @@ -179,17 +179,17 @@ public class SchemaDropperImpl implements SchemaDropper { switch ( sourceDescriptor.getSourceType() ) { case SCRIPT: - dropFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, dialect, options, targets ); + applyScript( options, commandExtractor, dialect, sourceDescriptor.getScriptSourceInput(), formatter, targets ); break; case METADATA: dropFromMetadata( metadata, options, inclusionFilter, dialect, formatter, targets ); break; case METADATA_THEN_SCRIPT: dropFromMetadata( metadata, options, inclusionFilter, dialect, formatter, targets ); - dropFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, dialect, options, targets ); + applyScript( options, commandExtractor, dialect, sourceDescriptor.getScriptSourceInput(), formatter, targets ); break; case SCRIPT_THEN_METADATA: - dropFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, dialect, options, targets ); + applyScript( options, commandExtractor, dialect, sourceDescriptor.getScriptSourceInput(), formatter, targets ); dropFromMetadata( metadata, options, inclusionFilter, dialect, formatter, targets ); break; } @@ -199,30 +199,6 @@ public class SchemaDropperImpl implements SchemaDropper { return tool.getServiceRegistry().getService(SqlScriptCommandExtractor.class); } - private void dropFromScript( - ScriptSourceInput scriptSourceInput, - SqlScriptCommandExtractor commandExtractor, - Formatter formatter, - Dialect dialect, - ExecutionOptions options, - GenerationTarget... targets) { - final List commands = scriptSourceInput.extract( - reader -> commandExtractor.extractCommands( reader, dialect ) - ); - for ( String command : commands ) { - applySqlString( command, formatter, options, targets ); - } - } - - private static SqlStringGenerationContext createSqlStringGenerationContext(ExecutionOptions options, Metadata metadata) { - final Database database = metadata.getDatabase(); - return SqlStringGenerationContextImpl.fromConfigurationMap( - database.getJdbcEnvironment(), - database, - options.getConfigurationValues() - ); - } - private void dropFromMetadata( Metadata metadata, ExecutionOptions options, @@ -496,36 +472,6 @@ public class SchemaDropperImpl implements SchemaDropper { exportIdentifiers.add( exportIdentifier ); } - private static void applySqlStrings( - String[] sqlStrings, - Formatter formatter, - ExecutionOptions options, - GenerationTarget... targets) { - if ( sqlStrings != null ) { - for ( String sqlString : sqlStrings ) { - applySqlString( sqlString, formatter, options, targets ); - } - } - } - - private static void applySqlString( - String sqlString, - Formatter formatter, - ExecutionOptions options, - GenerationTarget... targets) { - if ( isNotEmpty( sqlString ) ) { - final String sqlStringFormatted = formatter.format( sqlString ); - for ( GenerationTarget target : targets ) { - try { - target.accept( sqlStringFormatted ); - } - catch (CommandAcceptanceException e) { - options.getExceptionHandler().handleException( e ); - } - } - } - } - @Override public DelayedDropAction buildDelayedAction( Metadata metadata, diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaTruncatorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaTruncatorImpl.java index 10e07e28c0..f528ca89f3 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaTruncatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaTruncatorImpl.java @@ -11,13 +11,11 @@ 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.SqlStringGenerationContext; -import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; 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.internal.util.config.ConfigurationHelper; import org.hibernate.mapping.ForeignKey; @@ -27,7 +25,6 @@ import org.hibernate.tool.schema.internal.exec.GenerationTarget; import org.hibernate.tool.schema.internal.exec.JdbcContext; import org.hibernate.tool.schema.internal.exec.ScriptSourceInputFromUrl; import org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl; -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.SchemaFilter; @@ -46,6 +43,10 @@ import java.util.Set; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_CHARSET_NAME; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_LOAD_SCRIPT_SOURCE; import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE; +import static org.hibernate.tool.schema.internal.Helper.applyScript; +import static org.hibernate.tool.schema.internal.Helper.applySqlString; +import static org.hibernate.tool.schema.internal.Helper.applySqlStrings; +import static org.hibernate.tool.schema.internal.Helper.createSqlStringGenerationContext; import static org.hibernate.tool.schema.internal.Helper.interpretScriptSourceSetting; /** @@ -121,9 +122,7 @@ public class SchemaTruncatorImpl implements SchemaTruncator { Formatter formatter, GenerationTarget... targets) { final Database database = metadata.getDatabase(); - SqlStringGenerationContext context = SqlStringGenerationContextImpl.fromConfigurationMap( - metadata.getDatabase().getJdbcEnvironment(), database, options.getConfigurationValues() ); - + SqlStringGenerationContext context = createSqlStringGenerationContext( options, metadata ); final Set exportIdentifiers = CollectionHelper.setOfSize( 50 ); @@ -281,40 +280,6 @@ public class SchemaTruncatorImpl implements SchemaTruncator { exportIdentifiers.add( exportIdentifier ); } - private static void applySqlStrings( - String[] sqlStrings, - Formatter formatter, - ExecutionOptions options, - GenerationTarget... targets) { - if ( sqlStrings == null ) { - return; - } - - for ( String sqlString : sqlStrings ) { - applySqlString( sqlString, formatter, options, targets ); - } - } - - private static void applySqlString( - String sqlString, - Formatter formatter, - ExecutionOptions options, - GenerationTarget... targets) { - if ( StringHelper.isEmpty( sqlString ) ) { - return; - } - - String sqlStringFormatted = formatter.format( sqlString ); - for ( GenerationTarget target : targets ) { - try { - target.accept( sqlStringFormatted ); - } - catch (CommandAcceptanceException e) { - options.getExceptionHandler().handleException( e ); - } - } - } - //Woooooo, massive copy/paste from SchemaCreatorImpl! private void applyImportSources( @@ -339,12 +304,7 @@ public class SchemaTruncatorImpl implements SchemaTruncator { if ( importScriptSetting != null ) { final ScriptSourceInput importScriptInput = interpretScriptSourceSetting( importScriptSetting, classLoaderService, charsetName ); - final List commands = importScriptInput.extract( - reader -> commandExtractor.extractCommands( reader, dialect ) - ); - for ( int i = 0; i < commands.size(); i++ ) { - applySqlString( commands.get( i ), formatter, options, targets ); - } + applyScript( options, commandExtractor, dialect, importScriptInput, formatter, targets ); } final String importFiles = ConfigurationHelper.getString( @@ -360,12 +320,7 @@ public class SchemaTruncatorImpl implements SchemaTruncator { continue; } final ScriptSourceInput importScriptInput = interpretLegacyImportScriptSetting( resourceName, classLoaderService, charsetName ); - final List commands = importScriptInput.extract( - reader -> commandExtractor.extractCommands( reader, dialect ) - ); - for ( int i = 0; i < commands.size(); i++ ) { - applySqlString( commands.get( i ), formatter, options, targets ); - } + applyScript( options, commandExtractor, dialect, importScriptInput, formatter, targets ); } }