org.hibernate.tool.schema.internal.exec.GenerationTarget Java Examples

The following examples show how to use org.hibernate.tool.schema.internal.exec.GenerationTarget. 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: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 );
		}
	}
}
 
Example #2
Source File: SchemaCreatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void createFromScript(
		ScriptSourceInput scriptSourceInput,
		ImportSqlCommandExtractor commandExtractor,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	scriptSourceInput.prepare();
	try {
		for ( String command : scriptSourceInput.read( commandExtractor ) ) {
			applySqlString( command, formatter, options, targets );
		}
	}
	finally {
		scriptSourceInput.release();
	}
}
 
Example #3
Source File: SchemaCreatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static void applySqlString(
		String sqlString,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	if ( StringHelper.isEmpty( sqlString ) ) {
		return;
	}

	try {
		String sqlStringFormatted = formatter.format( sqlString );
		for ( GenerationTarget target : targets ) {
			target.accept( sqlStringFormatted );
		}
	}
	catch (CommandAcceptanceException e) {
		options.getExceptionHandler().handleException( e );
	}
}
 
Example #4
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void dropFromScript(
		ScriptSourceInput scriptSourceInput,
		ImportSqlCommandExtractor commandExtractor,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	scriptSourceInput.prepare();
	try {
		for ( String command : scriptSourceInput.read( commandExtractor ) ) {
			applySqlString( command, formatter, options, targets );
		}
	}
	finally {
		scriptSourceInput.release();
	}
}
 
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: 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 #7
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static void applySqlString(
		boolean quiet,
		String sqlString,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	if ( !StringHelper.isEmpty( sqlString ) ) {
		String sqlStringFormatted = formatter.format( sqlString );
		for ( GenerationTarget target : targets ) {
			try {
				target.accept( sqlStringFormatted );
			}
			catch (CommandAcceptanceException e) {
				if ( !quiet ) {
					options.getExceptionHandler().handleException( e );
				}
				// otherwise ignore the exception
			}
		}
	}
}
 
Example #8
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void migrateTable(
		Table table,
		TableInformation tableInformation,
		Dialect dialect,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	final Database database = metadata.getDatabase();

	//noinspection unchecked
	applySqlStrings(
			false,
			table.sqlAlterStrings(
					dialect,
					metadata,
					tableInformation,
					getDefaultCatalogName( database, dialect ),
					getDefaultSchemaName( database, dialect )
			),
			formatter,
			options,
			targets
	);
}
 
Example #9
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 #10
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * For tests
 */
public void doDrop(Metadata metadata, boolean manageNamespaces, GenerationTarget... targets) {
	final ServiceRegistry serviceRegistry = ( (MetadataImplementor) metadata ).getMetadataBuildingOptions().getServiceRegistry();
	doDrop(
			metadata,
			serviceRegistry,
			serviceRegistry.getService( ConfigurationService.class ).getSettings(),
			manageNamespaces,
			targets
	);
}
 
Example #11
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 );
	}
}
 
Example #12
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void applyConstraintDropping(
		Namespace namespace,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	final Dialect dialect = metadata.getDatabase().getJdbcEnvironment().getDialect();

	if ( !dialect.dropConstraints() ) {
		return;
	}

	for ( Table table : namespace.getTables() ) {
		if ( !table.isPhysicalTable() ) {
			continue;
		}
		if ( !schemaFilter.includeTable( table ) ) {
			continue;
		}

		final Iterator fks = table.getForeignKeyIterator();
		while ( fks.hasNext() ) {
			final ForeignKey foreignKey = (ForeignKey) fks.next();
			applySqlStrings(
					dialect.getForeignKeyExporter().getSqlDropStrings( foreignKey, metadata ),
					formatter,
					options,
					targets
			);
		}
	}
}
 
Example #13
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static void applySqlStrings(
		boolean quiet,
		Iterator<String> sqlStrings,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	if ( sqlStrings != null ) {
		while ( sqlStrings.hasNext() ) {
			final String sqlString = sqlStrings.next();
			applySqlString( quiet, sqlString, formatter, options, targets );
		}
	}
}
 
Example #14
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void createSchemaAndCatalog(
		DatabaseInformation existingDatabase,
		ExecutionOptions options,
		Dialect dialect,
		Formatter formatter,
		boolean tryToCreateCatalogs,
		boolean tryToCreateSchemas,
		Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
	if ( tryToCreateCatalogs || tryToCreateSchemas ) {
		if ( tryToCreateCatalogs ) {
			final Identifier catalogLogicalName = namespace.getName().getCatalog();
			final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();

			if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName )
					&& !existingDatabase.catalogExists( catalogLogicalName ) ) {
				applySqlStrings(
						false,
						dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ),
						formatter,
						options,
						targets
				);
				exportedCatalogs.add( catalogLogicalName );
			}
		}

		if ( tryToCreateSchemas
				&& namespace.getPhysicalName().getSchema() != null
				&& !existingDatabase.schemaExists( namespace.getName() ) ) {
			applySqlStrings(
					false,
					dialect.getCreateSchemaCommand( namespace.getPhysicalName().getSchema().render( dialect ) ),
					formatter,
					options,
					targets
			);
		}
	}
}
 
Example #15
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected static void applySqlStrings(
		boolean quiet,
		String[] sqlStrings,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	if ( sqlStrings != null ) {
		for ( String sqlString : sqlStrings ) {
			applySqlString( quiet, sqlString, formatter, options, targets );
		}
	}
}
 
Example #16
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void applyForeignKeys(
		Table table,
		TableInformation tableInformation,
		Dialect dialect,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	if ( dialect.hasAlterTable() ) {
		final Exporter<ForeignKey> exporter = dialect.getForeignKeyExporter();

		@SuppressWarnings("unchecked")
		final Iterator<ForeignKey> fkItr = table.getForeignKeyIterator();
		while ( fkItr.hasNext() ) {
			final ForeignKey foreignKey = fkItr.next();
			if ( foreignKey.isPhysicalConstraint() && foreignKey.isCreationEnabled() ) {
				boolean existingForeignKeyFound = false;
				if ( tableInformation != null ) {
					existingForeignKeyFound = checkForExistingForeignKey(
							foreignKey,
							tableInformation
					);
				}
				if ( !existingForeignKeyFound ) {
					// todo : shouldn't we just drop+recreate if FK exists?
					//		this follows the existing code from legacy SchemaUpdate which just skipped

					// in old SchemaUpdate code, this was the trigger to "create"
					applySqlStrings(
							false,
							exporter.getSqlCreateStrings( foreignKey, metadata ),
							formatter,
							options,
							targets
					);
				}
			}
		}
	}
}
 
Example #17
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void applyIndexes(
		Table table,
		TableInformation tableInformation,
		Dialect dialect,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	final Exporter<Index> exporter = dialect.getIndexExporter();

	final Iterator<Index> indexItr = table.getIndexIterator();
	while ( indexItr.hasNext() ) {
		final Index index = indexItr.next();
		if ( !StringHelper.isEmpty( index.getName() ) ) {
			IndexInformation existingIndex = null;
			if ( tableInformation != null ) {
				existingIndex = findMatchingIndex( index, tableInformation );
			}
			if ( existingIndex == null ) {
				applySqlStrings(
						false,
						exporter.getSqlCreateStrings( index, metadata ),
						formatter,
						options,
						targets
				);
			}
		}
	}
}
 
Example #18
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void createTable(
		Table table,
		Dialect dialect,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	applySqlStrings(
			false,
			dialect.getTableExporter().getSqlCreateStrings( table, metadata ),
			formatter,
			options,
			targets
	);
}
 
Example #19
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected abstract NameSpaceTablesInformation performTablesMigration(
Metadata metadata,
DatabaseInformation existingDatabase,
ExecutionOptions options,
Dialect dialect,
Formatter formatter,
Set<String> exportIdentifiers,
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace, GenerationTarget[] targets);
 
Example #20
Source File: HibernateSchemaManagementTool.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
GenerationTarget[] buildGenerationTargets(
		TargetDescriptor targetDescriptor,
		DdlTransactionIsolator ddlTransactionIsolator,
		Map options) {
	final String scriptDelimiter = ConfigurationHelper.getString( HBM2DDL_DELIMITER, options );

	final GenerationTarget[] targets = new GenerationTarget[ targetDescriptor.getTargetTypes().size() ];

	int index = 0;

	if ( targetDescriptor.getTargetTypes().contains( TargetType.STDOUT ) ) {
		targets[index] = new GenerationTargetToStdout( scriptDelimiter );
		index++;
	}

	if ( targetDescriptor.getTargetTypes().contains( TargetType.SCRIPT ) ) {
		if ( targetDescriptor.getScriptTargetOutput() == null ) {
			throw new SchemaManagementException( "Writing to script was requested, but no script file was specified" );
		}
		targets[index] = new GenerationTargetToScript( targetDescriptor.getScriptTargetOutput(), scriptDelimiter );
		index++;
	}

	if ( targetDescriptor.getTargetTypes().contains( TargetType.DATABASE ) ) {
		targets[index] = new GenerationTargetToDatabase( ddlTransactionIsolator, false );
	}

	return targets;
}
 
Example #21
Source File: HibernateSchemaManagementTool.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
GenerationTarget[] buildGenerationTargets(
		TargetDescriptor targetDescriptor,
		JdbcContext jdbcContext,
		Map options,
		boolean needsAutoCommit) {
	final String scriptDelimiter = ConfigurationHelper.getString( HBM2DDL_DELIMITER, options );

	final GenerationTarget[] targets = new GenerationTarget[ targetDescriptor.getTargetTypes().size() ];

	int index = 0;

	if ( targetDescriptor.getTargetTypes().contains( TargetType.STDOUT ) ) {
		targets[index] = new GenerationTargetToStdout( scriptDelimiter );
		index++;
	}

	if ( targetDescriptor.getTargetTypes().contains( TargetType.SCRIPT ) ) {
		if ( targetDescriptor.getScriptTargetOutput() == null ) {
			throw new SchemaManagementException( "Writing to script was requested, but no script file was specified" );
		}
		targets[index] = new GenerationTargetToScript( targetDescriptor.getScriptTargetOutput(), scriptDelimiter );
		index++;
	}

	if ( targetDescriptor.getTargetTypes().contains( TargetType.DATABASE ) ) {
		targets[index] = new GenerationTargetToDatabase( getDdlTransactionIsolator( jdbcContext ), true );
	}

	return targets;
}
 
Example #22
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 #23
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 boolean manageNamespaces,
		GenerationTarget... targets) {
	final ServiceRegistry serviceRegistry = ( (MetadataImplementor) metadata ).getMetadataBuildingOptions().getServiceRegistry();
	doCreation(
			metadata,
			serviceRegistry,
			serviceRegistry.getService( ConfigurationService.class ).getSettings(),
			manageNamespaces,
			targets
	);
}
 
Example #24
Source File: SchemaCreatorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 );
	}
}
 
Example #25
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 #26
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
	);
}
 
Example #27
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void applyUniqueKeys(
		Table table,
		TableInformation tableInfo,
		Dialect dialect,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	if ( uniqueConstraintStrategy == null ) {
		uniqueConstraintStrategy = determineUniqueConstraintSchemaUpdateStrategy( metadata );
	}

	if ( uniqueConstraintStrategy != UniqueConstraintSchemaUpdateStrategy.SKIP ) {
		final Exporter<Constraint> exporter = dialect.getUniqueKeyExporter();

		final Iterator ukItr = table.getUniqueKeyIterator();
		while ( ukItr.hasNext() ) {
			final UniqueKey uniqueKey = (UniqueKey) ukItr.next();
			// 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() ) );
			}
			if ( indexInfo == null ) {
				if ( uniqueConstraintStrategy == UniqueConstraintSchemaUpdateStrategy.DROP_RECREATE_QUIETLY ) {
					applySqlStrings(
							true,
							exporter.getSqlDropStrings( uniqueKey, metadata ),
							formatter,
							options,
							targets
					);
				}

				applySqlStrings(
						true,
						exporter.getSqlCreateStrings( uniqueKey, metadata ),
						formatter,
						options,
						targets
				);
			}
		}
	}
}