下面列出了org.hibernate.tool.schema.internal.SchemaCreatorImpl#org.hibernate.tool.schema.spi.SourceDescriptor 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void doCreation(
Metadata metadata,
ExecutionOptions options,
SourceDescriptor sourceDescriptor,
TargetDescriptor targetDescriptor) {
// Add auxiliary database objects to batch DDL statements
metadata.getDatabase().addAuxiliaryDatabaseObject(new StartBatchDdl(Action.CREATE));
metadata.getDatabase().addAuxiliaryDatabaseObject(new RunBatchDdl(Action.CREATE));
try (Connection connection = tool.getDatabaseMetadataConnection(options)) {
SpannerDatabaseInfo spannerDatabaseInfo = new SpannerDatabaseInfo(connection.getMetaData());
tool.getSpannerTableExporter(options).init(metadata, spannerDatabaseInfo, Action.CREATE);
tool.getForeignKeyExporter(options).init(spannerDatabaseInfo);
schemaCreator.doCreation(metadata, options, sourceDescriptor, targetDescriptor);
} catch (SQLException e) {
throw new RuntimeException("Failed to update Spanner table schema.", e);
}
}
@Override
public void doDrop(
Metadata metadata,
ExecutionOptions options,
SourceDescriptor sourceDescriptor,
TargetDescriptor targetDescriptor) {
// Initialize auxiliary database objects to enable DDL statement batching.
metadata.getDatabase().addAuxiliaryDatabaseObject(new StartBatchDdl(Action.DROP));
metadata.getDatabase().addAuxiliaryDatabaseObject(new RunBatchDdl(Action.DROP));
try (Connection connection = tool.getDatabaseMetadataConnection(options)) {
// Initialize exporters with drop table dependencies so tables are dropped in the right order.
SpannerDatabaseInfo spannerDatabaseInfo = new SpannerDatabaseInfo(connection.getMetaData());
tool.getSpannerTableExporter(options).init(metadata, spannerDatabaseInfo, Action.DROP);
tool.getForeignKeyExporter(options).init(spannerDatabaseInfo);
schemaDropper.doDrop(metadata, options, sourceDescriptor, targetDescriptor);
} catch (SQLException e) {
throw new RuntimeException("Failed to update Spanner table schema.", e);
}
}
@Override
public void doCreation(
Metadata metadata,
ExecutionOptions options,
SourceDescriptor sourceDescriptor,
TargetDescriptor targetDescriptor) {
if ( targetDescriptor.getTargetTypes().isEmpty() ) {
return;
}
final JdbcContext jdbcContext = tool.resolveJdbcContext( options.getConfigurationValues() );
final GenerationTarget[] targets = tool.buildGenerationTargets(
targetDescriptor,
jdbcContext,
options.getConfigurationValues(),
true
);
doCreation( metadata, jdbcContext.getDialect(), options, sourceDescriptor, targets );
}
@Override
public void doDrop(
Metadata metadata,
ExecutionOptions options,
SourceDescriptor sourceDescriptor,
TargetDescriptor targetDescriptor) {
if ( targetDescriptor.getTargetTypes().isEmpty() ) {
return;
}
final JdbcContext jdbcContext = tool.resolveJdbcContext( options.getConfigurationValues() );
final GenerationTarget[] targets = tool.buildGenerationTargets( targetDescriptor, jdbcContext, options.getConfigurationValues(), true );
doDrop( metadata, options, jdbcContext.getDialect(), sourceDescriptor, targets );
}
private void performDrop(
Metadata metadata,
ExecutionOptions options,
Dialect dialect,
SourceDescriptor sourceDescriptor,
GenerationTarget... targets) {
final ImportSqlCommandExtractor commandExtractor = tool.getServiceRegistry().getService( ImportSqlCommandExtractor.class );
final boolean format = Helper.interpretFormattingEnabled( options.getConfigurationValues() );
final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
if ( sourceDescriptor.getSourceType() == SourceType.SCRIPT ) {
dropFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets );
}
else if ( sourceDescriptor.getSourceType() == SourceType.METADATA ) {
dropFromMetadata( metadata, options, dialect, formatter, targets );
}
else if ( sourceDescriptor.getSourceType() == SourceType.METADATA_THEN_SCRIPT ) {
dropFromMetadata( metadata, options, dialect, formatter, targets );
dropFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets );
}
else {
dropFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets );
dropFromMetadata( metadata, options, dialect, formatter, targets );
}
}
@Override
public DelayedDropAction buildDelayedAction(
Metadata metadata, ExecutionOptions options, SourceDescriptor sourceDescriptor) {
try (Connection connection = tool.getDatabaseMetadataConnection(options)) {
// Initialize exporters with drop table dependencies so tables are dropped in the right order.
SpannerDatabaseInfo spannerDatabaseInfo = new SpannerDatabaseInfo(connection.getMetaData());
tool.getSpannerTableExporter(options).init(
metadata, spannerDatabaseInfo, Action.DROP);
tool.getForeignKeyExporter(options).init(spannerDatabaseInfo);
return schemaDropper.buildDelayedAction(metadata, options, sourceDescriptor);
} catch (SQLException e) {
throw new RuntimeException("Failed to update Spanner table schema.", e);
}
}
List<String> createScript(Metadata metadata, Dialect d, boolean includeDrops) {
final JournalingGenerationTarget target = new JournalingGenerationTarget();
final ExecutionOptions options = new ExecutionOptions() {
@Override
public boolean shouldManageNamespaces() {
return false;
}
@Override
public Map getConfigurationValues() {
return Collections.emptyMap();
}
@Override
public ExceptionHandler getExceptionHandler() {
return ExceptionHandlerHaltImpl.INSTANCE;
}
};
HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
tool.injectServices((ServiceRegistryImplementor) this.registry);
SourceDescriptor sd = new SourceDescriptor() {
@Override
public SourceType getSourceType() {
return SourceType.METADATA;
}
@Override
public ScriptSourceInput getScriptSourceInput() {
return null;
}
};
if (includeDrops) {
new SchemaDropperImpl(tool).doDrop(metadata, options, d, sd, target);
}
new SchemaCreatorImpl(tool).doCreation(metadata, d, options, sd, target);
return target.commands;
}
private void performCreation(
Metadata metadata,
Dialect dialect,
ExecutionOptions options,
SourceDescriptor sourceDescriptor,
GenerationTarget... targets) {
final ImportSqlCommandExtractor commandExtractor = tool.getServiceRegistry().getService( ImportSqlCommandExtractor.class );
final boolean format = Helper.interpretFormattingEnabled( options.getConfigurationValues() );
final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
switch ( sourceDescriptor.getSourceType() ) {
case SCRIPT: {
createFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets );
break;
}
case METADATA: {
createFromMetadata( metadata, options, dialect, formatter, targets );
break;
}
case METADATA_THEN_SCRIPT: {
createFromMetadata( metadata, options, dialect, formatter, targets );
createFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets );
break;
}
case SCRIPT_THEN_METADATA: {
createFromScript( sourceDescriptor.getScriptSourceInput(), commandExtractor, formatter, options, targets );
createFromMetadata( metadata, options, dialect, formatter, targets );
}
}
applyImportSources( options, commandExtractor, format, targets );
}
public void doCreation(
Metadata metadata,
final ServiceRegistry serviceRegistry,
final Map settings,
final boolean manageNamespaces,
GenerationTarget... targets) {
doCreation(
metadata,
serviceRegistry.getService( JdbcEnvironment.class ).getDialect(),
new ExecutionOptions() {
@Override
public boolean shouldManageNamespaces() {
return manageNamespaces;
}
@Override
public Map getConfigurationValues() {
return settings;
}
@Override
public ExceptionHandler getExceptionHandler() {
return ExceptionHandlerLoggedImpl.INSTANCE;
}
},
new SourceDescriptor() {
@Override
public SourceType getSourceType() {
return SourceType.METADATA;
}
@Override
public ScriptSourceInput getScriptSourceInput() {
return null;
}
},
targets
);
}
@Override
public DelayedDropAction buildDelayedAction(
Metadata metadata,
ExecutionOptions options,
SourceDescriptor sourceDescriptor) {
final JournalingGenerationTarget target = new JournalingGenerationTarget();
doDrop( metadata, options, tool.getServiceRegistry().getService( JdbcEnvironment.class ).getDialect(), sourceDescriptor, target );
return new DelayedDropActionImpl( target.commands );
}
public void doExecution(
Action action,
boolean needsJdbc,
Metadata metadata,
ServiceRegistry serviceRegistry,
TargetDescriptor targetDescriptor) {
Map config = new HashMap();
config.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() );
config.put( AvailableSettings.HBM2DDL_DELIMITER, delimiter );
config.put( AvailableSettings.FORMAT_SQL, format );
config.put( AvailableSettings.HBM2DDL_IMPORT_FILES, importFiles );
final SchemaManagementTool tool = serviceRegistry.getService( SchemaManagementTool.class );
final ExceptionHandler exceptionHandler = haltOnError
? ExceptionHandlerHaltImpl.INSTANCE
: new ExceptionHandlerCollectingImpl();
final ExecutionOptions executionOptions = SchemaManagementToolCoordinator.buildExecutionOptions(
config,
exceptionHandler
);
final SourceDescriptor sourceDescriptor = new SourceDescriptor() {
@Override
public SourceType getSourceType() {
return SourceType.METADATA;
}
@Override
public ScriptSourceInput getScriptSourceInput() {
return null;
}
};
try {
if ( action.doDrop() ) {
tool.getSchemaDropper( config ).doDrop(
metadata,
executionOptions,
sourceDescriptor,
targetDescriptor
);
}
if ( action.doCreate() ) {
tool.getSchemaCreator( config ).doCreation(
metadata,
executionOptions,
sourceDescriptor,
targetDescriptor
);
}
}
finally {
if ( exceptionHandler instanceof ExceptionHandlerCollectingImpl ) {
exceptions.addAll( ( (ExceptionHandlerCollectingImpl) exceptionHandler ).getExceptions() );
}
}
}
/**
* For tests
*/
public void doDrop(
Metadata metadata,
final ServiceRegistry serviceRegistry,
final Map settings,
final boolean manageNamespaces,
GenerationTarget... targets) {
if ( targets == null || targets.length == 0 ) {
final JdbcContext jdbcContext = tool.resolveJdbcContext( settings );
targets = new GenerationTarget[] {
new GenerationTargetToDatabase(
serviceRegistry.getService( TransactionCoordinatorBuilder.class ).buildDdlTransactionIsolator( jdbcContext ),
true
)
};
}
doDrop(
metadata,
new ExecutionOptions() {
@Override
public boolean shouldManageNamespaces() {
return manageNamespaces;
}
@Override
public Map getConfigurationValues() {
return settings;
}
@Override
public ExceptionHandler getExceptionHandler() {
return ExceptionHandlerLoggedImpl.INSTANCE;
}
},
serviceRegistry.getService( JdbcEnvironment.class ).getDialect(),
new SourceDescriptor() {
@Override
public SourceType getSourceType() {
return SourceType.METADATA;
}
@Override
public ScriptSourceInput getScriptSourceInput() {
return null;
}
},
targets
);
}