org.hibernate.tool.schema.spi.SourceDescriptor Java Examples

The following examples show how to use org.hibernate.tool.schema.spi.SourceDescriptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpannerSchemaCreator.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 6 votes vote down vote up
@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);
  }
}
 
Example #2
Source File: SpannerSchemaDropper.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 6 votes vote down vote up
@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);
  }
}
 
Example #3
Source File: SchemaCreatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 );
}
 
Example #4
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 );
}
 
Example #5
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 );
	}
}
 
Example #6
Source File: SpannerSchemaDropper.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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);
  }
}
 
Example #7
Source File: RedirectionSchemaInitializer.java    From teiid-spring-boot with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: SchemaCreatorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 );
}
 
Example #9
Source File: SchemaCreatorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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
	);
}
 
Example #10
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
}
 
Example #11
Source File: SchemaExport.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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() );
		}
	}
}
 
Example #12
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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
	);
}