org.hibernate.engine.jdbc.internal.Formatter Java Examples

The following examples show how to use org.hibernate.engine.jdbc.internal.Formatter. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #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: SqlStatementLogger.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Log a SQL statement string using the specified formatter
 *
 * @param statement The SQL statement.
 * @param formatter The formatter to use.
 */
@AllowSysOut
public void logStatement(String statement, Formatter formatter) {
	if ( format ) {
		if ( logToStdout || LOG.isDebugEnabled() ) {
			statement = formatter.format( statement );
		}
	}
	LOG.debug( statement );
	if ( logToStdout ) {
		System.out.println( "Hibernate: " + statement );
	}
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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
				);
			}
		}
	}
}