HHH-2403 - ImportSqlCommandExtractor set from outside SchemaExport

This commit is contained in:
Lukasz Antoniak 2011-11-09 19:05:05 +01:00 committed by Steve Ebersole
parent 72b2af3b0a
commit 92ae782653
3 changed files with 29 additions and 14 deletions

View File

@ -132,6 +132,7 @@ import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.hibernate.service.spi.SessionFactoryServiceRegistryFactory; import org.hibernate.service.spi.SessionFactoryServiceRegistryFactory;
import org.hibernate.stat.Statistics; import org.hibernate.stat.Statistics;
import org.hibernate.stat.spi.StatisticsImplementor; import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.tool.hbm2ddl.ImportSqlCommandExtractor;
import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.hbm2ddl.SchemaValidator; import org.hibernate.tool.hbm2ddl.SchemaValidator;
@ -441,7 +442,9 @@ public final class SessionFactoryImpl
LOG.debugf( "Instantiated session factory" ); LOG.debugf( "Instantiated session factory" );
if ( settings.isAutoCreateSchema() ) { if ( settings.isAutoCreateSchema() ) {
new SchemaExport( serviceRegistry, cfg ).create( false, true ); new SchemaExport( serviceRegistry, cfg )
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) )
.create( false, true );
} }
if ( settings.isAutoUpdateSchema() ) { if ( settings.isAutoUpdateSchema() ) {
new SchemaUpdate( serviceRegistry, cfg ).execute( false, true ); new SchemaUpdate( serviceRegistry, cfg ).execute( false, true );
@ -450,7 +453,8 @@ public final class SessionFactoryImpl
new SchemaValidator( serviceRegistry, cfg ).validate(); new SchemaValidator( serviceRegistry, cfg ).validate();
} }
if ( settings.isAutoDropSchema() ) { if ( settings.isAutoDropSchema() ) {
schemaExport = new SchemaExport( serviceRegistry, cfg ); schemaExport = new SchemaExport( serviceRegistry, cfg )
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) );
} }
currentSessionContext = buildCurrentSessionContext(); currentSessionContext = buildCurrentSessionContext();
@ -766,7 +770,9 @@ public final class SessionFactoryImpl
LOG.debugf("Instantiated session factory"); LOG.debugf("Instantiated session factory");
if ( settings.isAutoCreateSchema() ) { if ( settings.isAutoCreateSchema() ) {
new SchemaExport( metadata ).create( false, true ); new SchemaExport( metadata )
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) )
.create( false, true );
} }
/* /*
if ( settings.isAutoUpdateSchema() ) { if ( settings.isAutoUpdateSchema() ) {
@ -777,7 +783,8 @@ public final class SessionFactoryImpl
} }
*/ */
if ( settings.isAutoDropSchema() ) { if ( settings.isAutoDropSchema() ) {
schemaExport = new SchemaExport( metadata ); schemaExport = new SchemaExport( metadata )
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) );
} }
currentSessionContext = buildCurrentSessionContext(); currentSessionContext = buildCurrentSessionContext();

View File

@ -16,14 +16,14 @@ import org.hibernate.service.spi.ServiceRegistryImplementor;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/ */
public class ImportSqlCommandExtractorInitiator implements BasicServiceInitiator<ImportSqlCommandExtractor> { public class ImportSqlCommandExtractorInitiator implements BasicServiceInitiator<ImportSqlCommandExtractor> {
private static final String DEFAULT_EXTRACTOR = SingleLineSqlCommandExtractor.class.getName();
public static final ImportSqlCommandExtractorInitiator INSTANCE = new ImportSqlCommandExtractorInitiator(); public static final ImportSqlCommandExtractorInitiator INSTANCE = new ImportSqlCommandExtractorInitiator();
public static final ImportSqlCommandExtractor DEFAULT_EXTRACTOR = new SingleLineSqlCommandExtractor();
@Override @Override
public ImportSqlCommandExtractor initiateService(Map configurationValues, ServiceRegistryImplementor registry) { public ImportSqlCommandExtractor initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
String extractorClassName = (String) configurationValues.get( Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR ); String extractorClassName = (String) configurationValues.get( Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR );
if ( StringHelper.isEmpty( extractorClassName ) ) { if ( StringHelper.isEmpty( extractorClassName ) ) {
extractorClassName = DEFAULT_EXTRACTOR; return DEFAULT_EXTRACTOR;
} }
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class ); final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
return instantiateExplicitCommandExtractor( extractorClassName, classLoaderService ); return instantiateExplicitCommandExtractor( extractorClassName, classLoaderService );

View File

@ -91,7 +91,6 @@ public class SchemaExport {
} }
} }
private final ServiceRegistry serviceRegistry;
private final ConnectionHelper connectionHelper; private final ConnectionHelper connectionHelper;
private final SqlStatementLogger sqlStatementLogger; private final SqlStatementLogger sqlStatementLogger;
private final SqlExceptionHelper sqlExceptionHelper; private final SqlExceptionHelper sqlExceptionHelper;
@ -102,13 +101,13 @@ public class SchemaExport {
private final List<Exception> exceptions = new ArrayList<Exception>(); private final List<Exception> exceptions = new ArrayList<Exception>();
private Formatter formatter; private Formatter formatter;
private ImportSqlCommandExtractor importSqlCommandExtractor = ImportSqlCommandExtractorInitiator.DEFAULT_EXTRACTOR;
private String outputFile = null; private String outputFile = null;
private String delimiter; private String delimiter;
private boolean haltOnError = false; private boolean haltOnError = false;
public SchemaExport(ServiceRegistry serviceRegistry, Configuration configuration) { public SchemaExport(ServiceRegistry serviceRegistry, Configuration configuration) {
this.serviceRegistry = serviceRegistry;
this.connectionHelper = new SuppliedConnectionProviderConnectionHelper( this.connectionHelper = new SuppliedConnectionProviderConnectionHelper(
serviceRegistry.getService( ConnectionProvider.class ) serviceRegistry.getService( ConnectionProvider.class )
); );
@ -128,7 +127,7 @@ public class SchemaExport {
} }
public SchemaExport(MetadataImplementor metadata) { public SchemaExport(MetadataImplementor metadata) {
this.serviceRegistry = metadata.getServiceRegistry(); ServiceRegistry serviceRegistry = metadata.getServiceRegistry();
this.connectionHelper = new SuppliedConnectionProviderConnectionHelper( this.connectionHelper = new SuppliedConnectionProviderConnectionHelper(
serviceRegistry.getService( ConnectionProvider.class ) serviceRegistry.getService( ConnectionProvider.class )
); );
@ -189,7 +188,6 @@ public class SchemaExport {
this.dropSQL = configuration.generateDropSchemaScript( dialect ); this.dropSQL = configuration.generateDropSchemaScript( dialect );
this.createSQL = configuration.generateSchemaCreationScript( dialect ); this.createSQL = configuration.generateSchemaCreationScript( dialect );
this.serviceRegistry = createServiceRegistry( props );
} }
/** /**
@ -215,7 +213,6 @@ public class SchemaExport {
final Dialect dialect = Dialect.getDialect( configuration.getProperties() ); final Dialect dialect = Dialect.getDialect( configuration.getProperties() );
this.dropSQL = configuration.generateDropSchemaScript( dialect ); this.dropSQL = configuration.generateDropSchemaScript( dialect );
this.createSQL = configuration.generateSchemaCreationScript( dialect ); this.createSQL = configuration.generateSchemaCreationScript( dialect );
this.serviceRegistry = createServiceRegistry( configuration.getProperties() );
} }
public SchemaExport( public SchemaExport(
@ -229,7 +226,6 @@ public class SchemaExport {
this.sqlStatementLogger = new SqlStatementLogger( false, true ); this.sqlStatementLogger = new SqlStatementLogger( false, true );
this.sqlExceptionHelper = new SqlExceptionHelper(); this.sqlExceptionHelper = new SqlExceptionHelper();
this.formatter = FormatStyle.DDL.getFormatter(); this.formatter = FormatStyle.DDL.getFormatter();
this.serviceRegistry = createServiceRegistry( new Properties() );
} }
/** /**
@ -265,6 +261,17 @@ public class SchemaExport {
return this; return this;
} }
/**
* Set <i>import.sql</i> command extractor. By default {@link SingleLineSqlCommandExtractor} is used.
*
* @param importSqlCommandExtractor <i>import.sql</i> command extractor.
* @return this
*/
public SchemaExport setImportSqlCommandExtractor(ImportSqlCommandExtractor importSqlCommandExtractor) {
this.importSqlCommandExtractor = importSqlCommandExtractor;
return this;
}
/** /**
* Should we stop once an error occurs? * Should we stop once an error occurs?
* *
@ -425,7 +432,7 @@ public class SchemaExport {
private void importScript(NamedReader namedReader, List<Exporter> exporters) throws Exception { private void importScript(NamedReader namedReader, List<Exporter> exporters) throws Exception {
BufferedReader reader = new BufferedReader( namedReader.getReader() ); BufferedReader reader = new BufferedReader( namedReader.getReader() );
String[] statements = serviceRegistry.getService( ImportSqlCommandExtractor.class ).extractCommands( reader ); String[] statements = importSqlCommandExtractor.extractCommands( reader );
if (statements != null) { if (statements != null) {
for ( String statement : statements ) { for ( String statement : statements ) {
if ( statement != null ) { if ( statement != null ) {
@ -586,7 +593,8 @@ public class SchemaExport {
SchemaExport se = new SchemaExport( serviceRegistry, cfg ) SchemaExport se = new SchemaExport( serviceRegistry, cfg )
.setHaltOnError( halt ) .setHaltOnError( halt )
.setOutputFile( outFile ) .setOutputFile( outFile )
.setDelimiter( delim ); .setDelimiter( delim )
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) );
if ( format ) { if ( format ) {
se.setFormat( true ); se.setFormat( true );
} }