HHH-14921 Always use SqlStringGenerationContext for generation of SQL strings involving table/sequence names

This commit is contained in:
Yoann Rodière 2021-12-06 16:35:25 +01:00
parent b6f833441a
commit 495bd51caa
58 changed files with 501 additions and 426 deletions

View File

@ -16,6 +16,7 @@ import jakarta.persistence.TemporalType;
import org.hibernate.ScrollMode;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.community.dialect.identity.SQLiteIdentityColumnSupport;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.NationalizationSupport;
@ -116,7 +117,7 @@ public class SQLiteDialect extends Dialect {
super( dialect );
}
@Override
public String getColumnDefinitionUniquenessFragment(Column column) {
public String getColumnDefinitionUniquenessFragment(Column column, SqlStringGenerationContext context) {
return " unique";
}
}

View File

@ -11,6 +11,7 @@ import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.QualifiedNameImpl;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.CommonFunctionFactory;
@ -19,7 +20,6 @@ import org.hibernate.community.dialect.identity.Teradata14IdentityColumnSupport;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.TopLimitHandler;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
@ -543,23 +543,18 @@ public class TeradataDialect extends Dialect {
}
@Override
public String[] getSqlCreateStrings(Index index, Metadata metadata) {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
public String[] getSqlCreateStrings(Index index, Metadata metadata, SqlStringGenerationContext context) {
QualifiedTableName qualifiedTableName = index.getTable().getQualifiedTableName();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
qualifiedTableName,
jdbcEnvironment.getDialect()
);
final String tableName = context.format( qualifiedTableName );
final String indexNameForCreation;
if ( getDialect().qualifyIndexName() ) {
indexNameForCreation = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
indexNameForCreation = context.format(
new QualifiedNameImpl(
qualifiedTableName.getCatalogName(),
qualifiedTableName.getSchemaName(),
Identifier.toIdentifier( index.getName() )
),
jdbcEnvironment.getDialect()
)
);
}
else {

View File

@ -7,6 +7,7 @@
package org.hibernate.community.dialect.unique;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.unique.DefaultUniqueDelegate;
import org.hibernate.mapping.UniqueKey;
@ -25,13 +26,11 @@ public class InformixUniqueDelegate extends DefaultUniqueDelegate {
// legacy model ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
// Do this here, rather than allowing UniqueKey/Constraint to do it.
// We need full, simplified control over whether or not it happens.
final String tableName = metadata.getDatabase().getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
uniqueKey.getTable().getQualifiedTableName(),
metadata.getDatabase().getJdbcEnvironment().getDialect()
);
final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() );
final String constraintName = dialect.quote( uniqueKey.getName() );
return dialect.getAlterTableString( tableName )
+ " add constraint " + uniqueConstraintSql( uniqueKey ) + " constraint " + constraintName;

View File

@ -31,8 +31,8 @@ public interface QualifiedName {
* Returns a String-form of the qualified name.
* <p/>
* Depending on intention, may not be appropriate. May want
* {@link org.hibernate.engine.jdbc.env.spi.QualifiedObjectNameFormatter#format}
* instead. See {@link org.hibernate.engine.jdbc.env.spi.JdbcEnvironment#getQualifiedObjectNameFormatter}
* {@link SqlStringGenerationContext#format}
* instead.
*
* @return The string form
*/

View File

@ -7,6 +7,7 @@
package org.hibernate.boot.model.relational;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
/**
* A context provided to methods responsible for generating SQL strings on startup.
@ -14,10 +15,18 @@ import org.hibernate.dialect.Dialect;
public interface SqlStringGenerationContext {
/**
* @return The database dialect, to generate SQL fragments that are specific to each vendor.
* @return The database dialect of the current JDBC environment,
* to generate SQL fragments that are specific to each vendor.
*/
Dialect getDialect();
/**
* @return The helper for dealing with identifiers in the current JDBC environment.
* <p>
* Note that the Identifiers returned from this helper already account for auto-quoting.
*/
IdentifierHelper getIdentifierHelper();
/**
* Render a formatted a table name
*
@ -45,4 +54,13 @@ public interface SqlStringGenerationContext {
*/
String format(QualifiedName qualifiedName);
/**
* Render a formatted sequence name, without the catalog (even the default one).
*
* @param qualifiedName The sequence name
*
* @return The formatted name
*/
String formatWithoutCatalog(QualifiedSequenceName qualifiedName);
}

View File

@ -11,6 +11,7 @@ import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.env.spi.QualifiedObjectNameFormatter;
@ -22,10 +23,13 @@ public class SqlStringGenerationContextImpl
}
private final Dialect dialect;
private final IdentifierHelper identifierHelper;
private final QualifiedObjectNameFormatter qualifiedObjectNameFormatter;
@SuppressWarnings("deprecation")
public SqlStringGenerationContextImpl(JdbcEnvironment jdbcEnvironment) {
this.dialect = jdbcEnvironment.getDialect();
this.identifierHelper = jdbcEnvironment.getIdentifierHelper();
this.qualifiedObjectNameFormatter = jdbcEnvironment.getQualifiedObjectNameFormatter();
}
@ -34,6 +38,11 @@ public class SqlStringGenerationContextImpl
return dialect;
}
@Override
public IdentifierHelper getIdentifierHelper() {
return identifierHelper;
}
@Override
public String format(QualifiedTableName qualifiedName) {
return qualifiedObjectNameFormatter.format( qualifiedName, dialect );
@ -49,4 +58,16 @@ public class SqlStringGenerationContextImpl
return qualifiedObjectNameFormatter.format( qualifiedName, dialect );
}
@Override
public String formatWithoutCatalog(QualifiedSequenceName qualifiedName) {
QualifiedSequenceName nameToFormat;
if ( qualifiedName.getCatalogName() != null ) {
nameToFormat = new QualifiedSequenceName( null,
qualifiedName.getSchemaName(), qualifiedName.getSequenceName() );
}
else {
nameToFormat = qualifiedName;
}
return qualifiedObjectNameFormatter.format( nameToFormat, dialect );
}
}

View File

@ -9,8 +9,10 @@ package org.hibernate.dialect;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.ScrollMode;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.function.CommonFunctionFactory;
import org.hibernate.dialect.identity.HANAIdentityColumnSupport;
@ -697,14 +699,16 @@ public abstract class AbstractHANADialect extends Dialect {
private final StandardTableExporter hanaTableExporter = new StandardTableExporter( this ) {
@Override
public String[] getSqlCreateStrings(Table table, org.hibernate.boot.Metadata metadata) {
String[] sqlCreateStrings = super.getSqlCreateStrings( table, metadata );
public String[] getSqlCreateStrings(Table table, Metadata metadata,
SqlStringGenerationContext context) {
String[] sqlCreateStrings = super.getSqlCreateStrings( table, metadata, context );
return quoteTypeIfNecessary( table, sqlCreateStrings, getCreateTableString() );
}
@Override
public String[] getSqlDropStrings(Table table, org.hibernate.boot.Metadata metadata) {
String[] sqlDropStrings = super.getSqlDropStrings( table, metadata );
public String[] getSqlDropStrings(Table table, Metadata metadata,
SqlStringGenerationContext context) {
String[] sqlDropStrings = super.getSqlDropStrings( table, metadata, context );
return quoteTypeIfNecessary( table, sqlDropStrings, "drop table" );
}

View File

@ -11,6 +11,7 @@ import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.function.CommonFunctionFactory;
import org.hibernate.dialect.function.SQLServerFormatEmulation;
import org.hibernate.dialect.identity.IdentityColumnSupport;
@ -847,14 +848,12 @@ public class SQLServerDialect extends AbstractTransactSQLDialect {
}
@Override
protected String getFormattedSequenceName(QualifiedSequenceName name, Metadata metadata) {
if ( name.getCatalogName() != null ) {
protected String getFormattedSequenceName(QualifiedSequenceName name, Metadata metadata,
SqlStringGenerationContext context) {
// SQL Server does not allow the catalog in the sequence name.
// See https://docs.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver15&viewFallbackFrom=sql-server-ver12
// Keeping the catalog in the name does not break on ORM, but it fails using Vert.X for Reactive.
name = new QualifiedSequenceName( null, name.getSchemaName(), name.getObjectName() );
}
return super.getFormattedSequenceName( name, metadata );
return context.formatWithoutCatalog( name );
}
}

View File

@ -12,6 +12,7 @@ import org.hibernate.StaleObjectStateException;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.function.CommonFunctionFactory;
import org.hibernate.dialect.lock.LockingStrategy;
import org.hibernate.dialect.lock.LockingStrategyException;
@ -783,12 +784,12 @@ public class SpannerDialect extends Dialect {
static class EmptyExporter<T extends Exportable> implements Exporter<T> {
@Override
public String[] getSqlCreateStrings(T exportable, Metadata metadata) {
public String[] getSqlCreateStrings(T exportable, Metadata metadata, SqlStringGenerationContext context) {
return ArrayHelper.EMPTY_STRING_ARRAY;
}
@Override
public String[] getSqlDropStrings(T exportable, Metadata metadata) {
public String[] getSqlDropStrings(T exportable, Metadata metadata, SqlStringGenerationContext context) {
return ArrayHelper.EMPTY_STRING_ARRAY;
}
}
@ -818,22 +819,22 @@ public class SpannerDialect extends Dialect {
static class DoNothingUniqueDelegate implements UniqueDelegate {
@Override
public String getColumnDefinitionUniquenessFragment(Column column) {
public String getColumnDefinitionUniquenessFragment(Column column, SqlStringGenerationContext context) {
return "";
}
@Override
public String getTableCreationUniqueConstraintsFragment(Table table) {
public String getTableCreationUniqueConstraintsFragment(Table table, SqlStringGenerationContext context) {
return "";
}
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, SqlStringGenerationContext context) {
return "";
}
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, SqlStringGenerationContext context) {
return "";
}
}

View File

@ -7,6 +7,7 @@
package org.hibernate.dialect;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Index;
@ -46,7 +47,7 @@ class SpannerDialectTableExporter implements Exporter<Table> {
}
@Override
public String[] getSqlCreateStrings(Table table, Metadata metadata) {
public String[] getSqlCreateStrings(Table table, Metadata metadata, SqlStringGenerationContext context) {
Collection<Column> keyColumns;
@ -66,10 +67,10 @@ class SpannerDialectTableExporter implements Exporter<Table> {
keyColumns = Collections.emptyList();
}
return getTableString( table, keyColumns );
return getTableString( table, keyColumns, context );
}
private String[] getTableString(Table table, Iterable<Column> keyColumns) {
private String[] getTableString(Table table, Iterable<Column> keyColumns, SqlStringGenerationContext context) {
String primaryKeyColNames = StreamSupport.stream( keyColumns.spliterator(), false )
.map( Column::getName )
.collect( Collectors.joining( "," ) );
@ -90,7 +91,7 @@ class SpannerDialectTableExporter implements Exporter<Table> {
statements.add(
MessageFormat.format(
this.createTableTemplate,
table.getQualifiedTableName().render(),
context.format( table.getQualifiedTableName() ),
colsAndTypes.toString(),
primaryKeyColNames
) );
@ -105,7 +106,7 @@ class SpannerDialectTableExporter implements Exporter<Table> {
}
@Override
public String[] getSqlDropStrings(Table table, Metadata metadata) {
public String[] getSqlDropStrings(Table table, Metadata metadata, SqlStringGenerationContext context) {
/* Cloud Spanner requires examining the metadata to find all indexes and interleaved tables.
* These must be dropped before the given table can be dropped.
@ -118,7 +119,7 @@ class SpannerDialectTableExporter implements Exporter<Table> {
dropStrings.add( "drop index " + index.next().getName() );
}
dropStrings.add( this.spannerDialect.getDropTableString( table.getQualifiedTableName().render() ) );
dropStrings.add( this.spannerDialect.getDropTableString( context.format( table.getQualifiedTableName() ) ) );
return dropStrings.toArray( new String[0] );
}

View File

@ -9,6 +9,7 @@ package org.hibernate.dialect.unique;
import java.util.Iterator;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.mapping.UniqueKey;
@ -29,10 +30,11 @@ public class DB2UniqueDelegate extends DefaultUniqueDelegate {
}
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
if ( hasNullable( uniqueKey ) ) {
return org.hibernate.mapping.Index.buildSqlCreateIndexString(
dialect,
context,
uniqueKey.getName(),
uniqueKey.getTable(),
uniqueKey.getColumnIterator(),
@ -42,23 +44,21 @@ public class DB2UniqueDelegate extends DefaultUniqueDelegate {
);
}
else {
return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata );
return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata, context );
}
}
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
if ( hasNullable( uniqueKey ) ) {
return org.hibernate.mapping.Index.buildSqlDropIndexString(
uniqueKey.getName(),
metadata.getDatabase().getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
uniqueKey.getTable().getQualifiedTableName(),
metadata.getDatabase().getJdbcEnvironment().getDialect()
)
context.format( uniqueKey.getTable().getQualifiedTableName() )
);
}
else {
return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata );
return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata, context );
}
}

View File

@ -9,8 +9,10 @@ package org.hibernate.dialect.unique;
import java.util.Iterator;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
/**
@ -34,23 +36,21 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
// legacy model ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public String getColumnDefinitionUniquenessFragment(org.hibernate.mapping.Column column) {
public String getColumnDefinitionUniquenessFragment(Column column,
SqlStringGenerationContext context) {
return "";
}
@Override
public String getTableCreationUniqueConstraintsFragment(org.hibernate.mapping.Table table) {
public String getTableCreationUniqueConstraintsFragment(Table table,
SqlStringGenerationContext context) {
return "";
}
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
uniqueKey.getTable().getQualifiedTableName(),
dialect
);
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() );
final String constraintName = dialect.quote( uniqueKey.getName() );
return dialect.getAlterTableString( tableName )
@ -76,13 +76,9 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
}
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
uniqueKey.getTable().getQualifiedTableName(),
dialect
);
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() );
final StringBuilder buf = new StringBuilder( dialect.getAlterTableString(tableName) );
buf.append( getDropUnique() );

View File

@ -7,6 +7,9 @@
package org.hibernate.dialect.unique;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
/**
@ -38,11 +41,11 @@ public interface UniqueDelegate {
* This is intended for dialects which do not support unique constraints
*
* @param column The column to which to apply the unique
*
* @param context A context for SQL string generation
* @return The fragment (usually "unique"), empty string indicates the uniqueness will be indicated using a
* different approach
*/
public String getColumnDefinitionUniquenessFragment(org.hibernate.mapping.Column column);
public String getColumnDefinitionUniquenessFragment(Column column, SqlStringGenerationContext context);
/**
* Get the fragment that can be used to apply unique constraints as part of table creation. The implementation
@ -53,30 +56,32 @@ public interface UniqueDelegate {
* Intended for Dialects which support unique constraint definitions, but just not in separate ALTER statements.
*
* @param table The table for which to generate the unique constraints fragment
*
* @param context A context for SQL string generation
* @return The fragment, typically in the form {@code ", unique(col1, col2), unique( col20)"}. NOTE: The leading
* comma is important!
*/
public String getTableCreationUniqueConstraintsFragment(org.hibernate.mapping.Table table);
public String getTableCreationUniqueConstraintsFragment(Table table, SqlStringGenerationContext context);
/**
* Get the SQL ALTER TABLE command to be used to create the given UniqueKey.
*
* @param uniqueKey The UniqueKey instance. Contains all information about the columns
* @param metadata Access to the bootstrap mapping information
*
* @param context A context for SQL string generation
* @return The ALTER TABLE command
*/
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata);
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context);
/**
* Get the SQL ALTER TABLE command to be used to drop the given UniqueKey.
*
* @param uniqueKey The UniqueKey instance. Contains all information about the columns
* @param metadata Access to the bootstrap mapping information
*
* @param context A context for SQL string generation
* @return The ALTER TABLE command
*/
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata);
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context);
}

View File

@ -58,7 +58,9 @@ public interface JdbcEnvironment extends Service {
* Obtain support for formatting qualified object names.
*
* @return Qualified name support.
* @deprecated Use a provided {@link org.hibernate.boot.model.relational.SqlStringGenerationContext} instead.
*/
@Deprecated
QualifiedObjectNameFormatter getQualifiedObjectNameFormatter();
/**

View File

@ -59,7 +59,7 @@ public class FilterConfiguration {
}
else if ( persistentClass != null ) {
String table = persistentClass.getTable().getQualifiedName(
factory.getDialect(),
factory.getSqlStringGenerationContext(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
);

View File

@ -18,6 +18,7 @@ import java.util.Locale;
import org.hibernate.HibernateException;
import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.StringHelper;
@ -168,9 +169,12 @@ public abstract class Constraint implements RelationalModel, Exportable, Seriali
return true;
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
@Override
public String sqlDropString(SqlStringGenerationContext context,
String defaultCatalog, String defaultSchema) {
Dialect dialect = context.getDialect();
if ( isGenerated( dialect ) ) {
final String tableName = getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema );
final String tableName = getTable().getQualifiedName( context, defaultCatalog, defaultSchema );
return String.format(
Locale.ROOT,
"%s evictData constraint %s",
@ -183,14 +187,17 @@ public abstract class Constraint implements RelationalModel, Exportable, Seriali
}
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
@Override
public String sqlCreateString(Mapping p, SqlStringGenerationContext context, String defaultCatalog,
String defaultSchema) {
Dialect dialect = context.getDialect();
if ( isGenerated( dialect ) ) {
// Certain dialects (ex: HANA) don't support FKs as expected, but other constraints can still be created.
// If that's the case, hasAlterTable() will be true, but getAddForeignKeyConstraintString will return
// empty string. Prevent blank "alter table" statements.
String constraintString = sqlConstraintString( dialect, getName(), defaultCatalog, defaultSchema );
String constraintString = sqlConstraintString( context, getName(), defaultCatalog, defaultSchema );
if ( !StringHelper.isEmpty( constraintString ) ) {
final String tableName = getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema );
final String tableName = getTable().getQualifiedName( context, defaultCatalog, defaultSchema );
return dialect.getAlterTableString( tableName ) + " " + constraintString;
}
}
@ -202,7 +209,7 @@ public abstract class Constraint implements RelationalModel, Exportable, Seriali
}
public abstract String sqlConstraintString(
Dialect d,
SqlStringGenerationContext context,
String constraintName,
String defaultCatalog,
String defaultSchema);

View File

@ -11,6 +11,7 @@ import java.util.Iterator;
import java.util.List;
import org.hibernate.MappingException;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
@ -54,11 +55,13 @@ public class ForeignKey extends Constraint {
}
}
@Override
public String sqlConstraintString(
Dialect dialect,
SqlStringGenerationContext context,
String constraintName,
String defaultCatalog,
String defaultSchema) {
Dialect dialect = context.getDialect();
String[] columnNames = new String[getColumnSpan()];
String[] referencedColumnNames = new String[getColumnSpan()];
@ -87,7 +90,7 @@ public class ForeignKey extends Constraint {
constraintName,
columnNames,
referencedTable.getQualifiedName(
dialect,
context,
defaultCatalog,
defaultSchema
),
@ -172,8 +175,11 @@ public class ForeignKey extends Constraint {
this.keyDefinition = keyDefinition;
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
String tableName = getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema );
@Override
public String sqlDropString(SqlStringGenerationContext context,
String defaultCatalog, String defaultSchema) {
Dialect dialect = context.getDialect();
String tableName = getTable().getQualifiedName( context, defaultCatalog, defaultSchema );
final StringBuilder buf = new StringBuilder( dialect.getAlterTableString( tableName ) );
buf.append( dialect.getDropForeignKeyString() );
if ( dialect.supportsIfExistsBeforeConstraintName() ) {

View File

@ -16,8 +16,8 @@ import org.hibernate.HibernateException;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.StringHelper;
@ -32,10 +32,13 @@ public class Index implements RelationalModel, Exportable, Serializable {
private java.util.Map<Column, String> columnOrderMap = new HashMap<>( );
private Identifier name;
public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema)
@Override
public String sqlCreateString(Mapping mapping, SqlStringGenerationContext context, String defaultCatalog,
String defaultSchema)
throws HibernateException {
Dialect dialect = context.getDialect();
return buildSqlCreateIndexString(
dialect,
context,
getQuotedName( dialect ),
getTable(),
getColumnIterator(),
@ -47,12 +50,12 @@ public class Index implements RelationalModel, Exportable, Serializable {
}
public static String buildSqlDropIndexString(
Dialect dialect,
SqlStringGenerationContext context,
Table table,
String name,
String defaultCatalog,
String defaultSchema) {
return buildSqlDropIndexString( name, table.getQualifiedName( dialect, defaultCatalog, defaultSchema ) );
return buildSqlDropIndexString( name, table.getQualifiedName( context, defaultCatalog, defaultSchema ) );
}
public static String buildSqlDropIndexString(
@ -62,7 +65,7 @@ public class Index implements RelationalModel, Exportable, Serializable {
}
public static String buildSqlCreateIndexString(
Dialect dialect,
SqlStringGenerationContext context,
String name,
Table table,
Iterator<Column> columns,
@ -71,9 +74,9 @@ public class Index implements RelationalModel, Exportable, Serializable {
String defaultCatalog,
String defaultSchema) {
return buildSqlCreateIndexString(
dialect,
context.getDialect(),
name,
table.getQualifiedName( dialect, defaultCatalog, defaultSchema ),
table.getQualifiedName( context, defaultCatalog, defaultSchema ),
columns,
columnOrderMap,
unique
@ -109,42 +112,17 @@ public class Index implements RelationalModel, Exportable, Serializable {
}
public static String buildSqlCreateIndexString(
Dialect dialect,
String name,
Table table,
Iterator<Column> columns,
boolean unique,
String defaultCatalog,
String defaultSchema) {
return buildSqlCreateIndexString(
dialect,
name,
table,
columns,
Collections.EMPTY_MAP,
unique,
defaultCatalog,
defaultSchema
);
}
public static String buildSqlCreateIndexString(
Dialect dialect,
SqlStringGenerationContext context,
String name,
Table table,
Iterator<Column> columns,
java.util.Map<Column, String> columnOrderMap,
boolean unique,
Metadata metadata) {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
dialect
);
final String tableName = context.format( table.getQualifiedTableName() );
return buildSqlCreateIndexString(
dialect,
context.getDialect(),
name,
tableName,
columns,
@ -168,10 +146,12 @@ public class Index implements RelationalModel, Exportable, Serializable {
}
@Override
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
public String sqlDropString(SqlStringGenerationContext context,
String defaultCatalog, String defaultSchema) {
Dialect dialect = context.getDialect();
return "drop index " +
StringHelper.qualify(
table.getQualifiedName( dialect, defaultCatalog, defaultSchema ),
table.getQualifiedName( context, defaultCatalog, defaultSchema ),
getQuotedName( dialect )
);
}

View File

@ -7,6 +7,7 @@
package org.hibernate.mapping;
import java.util.Iterator;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
@ -71,7 +72,9 @@ public class PrimaryKey extends Constraint {
return buf.append(')').toString();
}
public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) {
@Override
public String sqlConstraintString(SqlStringGenerationContext context, String constraintName, String defaultCatalog, String defaultSchema) {
Dialect dialect = context.getDialect();
StringBuilder buf = new StringBuilder(
dialect.getAddPrimaryKeyConstraintString(constraintName)
).append('(');

View File

@ -6,7 +6,7 @@
*/
package org.hibernate.mapping;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.engine.spi.Mapping;
/**
@ -17,6 +17,6 @@ import org.hibernate.engine.spi.Mapping;
*/
@Deprecated
public interface RelationalModel {
String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) throws HibernateException;
String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema);
String sqlCreateString(Mapping p, SqlStringGenerationContext context, String defaultCatalog, String defaultSchema) throws HibernateException;
String sqlDropString(SqlStringGenerationContext context, String defaultCatalog, String defaultSchema);
}

View File

@ -27,8 +27,7 @@ import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.env.spi.QualifiedObjectNameFormatter;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation;
@ -122,28 +121,23 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
return contributor;
}
/**
* @deprecated Should use {@link QualifiedObjectNameFormatter#format} on QualifiedObjectNameFormatter
* obtained from {@link JdbcEnvironment}
*/
@Deprecated
public String getQualifiedName(Dialect dialect, String defaultCatalog, String defaultSchema) {
public String getQualifiedName(SqlStringGenerationContext context, String defaultCatalog, String defaultSchema) {
if ( subselect != null ) {
return "( " + subselect + " )";
}
String quotedName = getQuotedName( dialect );
String usedSchema = schema == null ?
defaultSchema :
getQuotedSchema( dialect );
String usedCatalog = catalog == null ?
defaultCatalog :
getQuotedCatalog( dialect );
return qualify( usedCatalog, usedSchema, quotedName );
IdentifierHelper identifierHelper = context.getIdentifierHelper();
Identifier usedSchema = schema == null ?
identifierHelper.toIdentifier( defaultSchema ) :
schema;
Identifier usedCatalog = catalog == null ?
identifierHelper.toIdentifier( defaultCatalog ) :
catalog;
return context.format( new QualifiedTableName( usedCatalog, usedSchema, name ) );
}
/**
* @deprecated Should use {@link QualifiedObjectNameFormatter#format} on QualifiedObjectNameFormatter
* obtained from {@link JdbcEnvironment}
* @deprecated Should build a {@link QualifiedTableName}
* then use {@link SqlStringGenerationContext#format(QualifiedTableName)}.
*/
@Deprecated
public static String qualify(String catalog, String schema, String table) {
@ -429,17 +423,14 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
Metadata metadata,
TableInformation tableInfo,
Identifier defaultCatalog,
Identifier defaultSchema) throws HibernateException {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
Identifier defaultSchema,
SqlStringGenerationContext sqlStringGenerationContext) throws HibernateException {
final String tableName = sqlStringGenerationContext.format(
new QualifiedTableName(
catalog != null ? catalog : defaultCatalog,
schema != null ? schema : defaultSchema,
name
),
dialect
)
);
StringBuilder root = new StringBuilder( dialect.getAlterTableString( tableName ) )
@ -479,7 +470,7 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
UniqueKey uk = getOrCreateUniqueKey( keyName );
uk.addColumn( column );
alter.append( dialect.getUniqueDelegate()
.getColumnDefinitionUniquenessFragment( column ) );
.getColumnDefinitionUniquenessFragment( column, sqlStringGenerationContext ) );
}
String checkConstraint = column.checkConstraint();
@ -510,10 +501,13 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
return getPrimaryKey() != null;
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
@Override
public String sqlCreateString(Mapping p, SqlStringGenerationContext context, String defaultCatalog,
String defaultSchema) {
Dialect dialect = context.getDialect();
StringBuilder buf = new StringBuilder( hasPrimaryKey() ? dialect.getCreateTableString() : dialect.getCreateMultisetTableString() )
.append( ' ' )
.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( getQualifiedName( context, defaultCatalog, defaultSchema ) )
.append( " (" );
boolean identityColumn = idValue != null && idValue.isIdentityColumn( p.getIdentifierGeneratorFactory(), dialect );
@ -562,7 +556,7 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
UniqueKey uk = getOrCreateUniqueKey( keyName );
uk.addColumn( col );
buf.append( dialect.getUniqueDelegate()
.getColumnDefinitionUniquenessFragment( col ) );
.getColumnDefinitionUniquenessFragment( col, context ) );
}
String checkConstraint = col.checkConstraint();
@ -585,7 +579,7 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
.append( getPrimaryKey().sqlConstraintString( dialect ) );
}
buf.append( dialect.getUniqueDelegate().getTableCreationUniqueConstraintsFragment( this ) );
buf.append( dialect.getUniqueDelegate().getTableCreationUniqueConstraintsFragment( this, context ) );
if ( dialect.supportsTableCheck() ) {
for ( String checkConstraint : checkConstraints ) {
@ -604,8 +598,10 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
return buf.append( dialect.getTableTypeString() ).toString();
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
return dialect.getDropTableString( getQualifiedName( dialect, defaultCatalog, defaultSchema ) );
@Override
public String sqlDropString(SqlStringGenerationContext context, String defaultCatalog, String defaultSchema) {
Dialect dialect = context.getDialect();
return dialect.getDropTableString( getQualifiedName( context, defaultCatalog, defaultSchema ) );
}
public PrimaryKey getPrimaryKey() {
@ -811,25 +807,6 @@ public class Table implements RelationalModel, Serializable, ContributableDataba
return checkConstraints.iterator();
}
public Iterator<String> sqlCommentStrings(Dialect dialect, String defaultCatalog, String defaultSchema) {
List<String> comments = new ArrayList<>();
if ( dialect.supportsCommentOn() ) {
String tableName = getQualifiedName( dialect, defaultCatalog, defaultSchema );
if ( comment != null ) {
comments.add( "comment on table " + tableName + " is '" + comment + "'" );
}
Iterator<Column> iter = getColumnIterator();
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
String columnComment = column.getComment();
if ( columnComment != null ) {
comments.add( "comment on column " + tableName + '.' + column.getQuotedName( dialect ) + " is '" + columnComment + "'" );
}
}
}
return comments.iterator();
}
@Override
public String getExportIdentifier() {
return Table.qualify(

View File

@ -9,6 +9,7 @@ package org.hibernate.mapping;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.StringHelper;
@ -23,7 +24,7 @@ public class UniqueKey extends Constraint {
@Override
public String sqlConstraintString(
Dialect dialect,
SqlStringGenerationContext context,
String constraintName,
String defaultCatalog,
String defaultSchema) {
@ -34,9 +35,8 @@ public class UniqueKey extends Constraint {
@Override
public String sqlCreateString(
Dialect dialect,
Mapping p,
String defaultCatalog,
SqlStringGenerationContext context, String defaultCatalog,
String defaultSchema) {
return null;
// return dialect.getUniqueDelegate().getAlterTableToAddUniqueKeyCommand(
@ -46,8 +46,7 @@ public class UniqueKey extends Constraint {
@Override
public String sqlDropString(
Dialect dialect,
String defaultCatalog,
SqlStringGenerationContext context, String defaultCatalog,
String defaultSchema) {
return null;
// return dialect.getUniqueDelegate().getAlterTableToDropUniqueKeyCommand(

View File

@ -9,6 +9,22 @@ package org.hibernate.metamodel.mapping;
import java.util.List;
import java.util.function.BiConsumer;
import org.hibernate.MappingException;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.SharedSessionContract;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.mapping.Any;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.IndexedConsumer;
import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;

View File

@ -15,9 +15,10 @@ import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.mapping.Any;
@ -65,11 +66,9 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
MappingModelCreationProcess creationProcess) {
final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
final JdbcEnvironment jdbcEnvironment = sessionFactory.getJdbcServices().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
bootValueMapping.getTable().getQualifiedTableName(),
jdbcEnvironment.getDialect()
);
final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext();
final Dialect dialect = sqlStringGenerationContext.getDialect();
final String tableName = sqlStringGenerationContext.format( bootValueMapping.getTable().getQualifiedTableName() );
assert bootValueMapping.getColumnSpan() == 2;
final Iterator<Selectable> columnIterator = bootValueMapping.getColumnIterator();
@ -86,7 +85,7 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
containerRole.append( AnyDiscriminatorPart.ROLE_NAME),
declaringModelPart,
tableName,
metaColumn.getText( jdbcEnvironment.getDialect() ),
metaColumn.getText( dialect ),
bootValueMapping.isNullable(),
(MetaType) anyType.getDiscriminatorType()
);
@ -97,7 +96,7 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
containerRole.append( AnyKeyPart.ROLE_NAME),
declaringModelPart,
tableName,
keyColumn.getText( jdbcEnvironment.getDialect() ),
keyColumn.getText( dialect ),
bootValueMapping.isNullable(),
keyType
);

View File

@ -17,6 +17,7 @@ import java.util.function.Function;
import org.hibernate.MappingException;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.SharedSessionContract;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.FetchTiming;
@ -339,7 +340,7 @@ public class EmbeddableMappingTypeImpl extends AbstractEmbeddableMapping impleme
if ( selectable instanceof Column ) {
containingTableExpression = getTableIdentifierExpression(
( (Column) selectable ).getValue().getTable(),
jdbcEnvironment
creationProcess
);
}
else {
@ -556,12 +557,11 @@ public class EmbeddableMappingTypeImpl extends AbstractEmbeddableMapping impleme
return true;
}
private static String getTableIdentifierExpression(Table table, JdbcEnvironment jdbcEnvironment) {
return jdbcEnvironment
.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
jdbcEnvironment.getDialect()
);
private static String getTableIdentifierExpression(Table table, MappingModelCreationProcess creationProcess) {
final SqlStringGenerationContext sqlStringGenerationContext = creationProcess.getCreationContext()
.getSessionFactory()
.getSqlStringGenerationContext();
return sqlStringGenerationContext.format( table.getQualifiedTableName() );
}
private boolean initColumnMappings() {

View File

@ -10,9 +10,9 @@ import java.util.List;
import java.util.function.Consumer;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -476,7 +476,7 @@ public class IdClassEmbeddable extends AbstractEmbeddableMapping implements Iden
throw new IllegalAttributeType( "An IdClass cannot define <any/> attributes : " + attributeName );
}
},
(column, jdbcEnvironment) -> getTableIdentifierExpression( column.getValue().getTable(), jdbcEnvironment ),
(column, jdbcEnvironment) -> getTableIdentifierExpression( column.getValue().getTable(), creationProcess ),
this::addAttribute,
() -> {
// We need the attribute mapping types to finish initialization first before we can build the column mappings
@ -489,12 +489,11 @@ public class IdClassEmbeddable extends AbstractEmbeddableMapping implements Iden
);
}
private static String getTableIdentifierExpression(Table table, JdbcEnvironment jdbcEnvironment) {
return jdbcEnvironment
.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
jdbcEnvironment.getDialect()
);
private static String getTableIdentifierExpression(Table table, MappingModelCreationProcess creationProcess) {
final SqlStringGenerationContext sqlStringGenerationContext = creationProcess.getCreationContext()
.getSessionFactory()
.getSqlStringGenerationContext();
return sqlStringGenerationContext.format( table.getQualifiedTableName() );
}
private boolean initColumnMappings() {

View File

@ -16,6 +16,7 @@ import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.SharedSessionContract;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.collection.internal.StandardArraySemantics;
import org.hibernate.collection.internal.StandardBagSemantics;
import org.hibernate.collection.internal.StandardIdentifierBagSemantics;
@ -24,7 +25,6 @@ import org.hibernate.collection.spi.CollectionSemantics;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.StringHelper;
@ -524,8 +524,8 @@ public class MappingModelCreationHelper {
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
final JdbcEnvironment jdbcEnvironment = sessionFactory.getJdbcServices().getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext();
final Dialect dialect = sqlStringGenerationContext.getDialect();
final MappingMetamodel domainModel = creationContext.getDomainModel();
final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor( bootValueMapping.getRole() );
@ -644,9 +644,8 @@ public class MappingModelCreationHelper {
);
final String mapKeyTableExpression;
if ( bootValueMapping instanceof Map && ( (Map) bootValueMapping ).getMapKeyPropertyName() != null ) {
mapKeyTableExpression = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
( (Map) bootValueMapping ).getIndex().getTable().getQualifiedTableName(),
dialect
mapKeyTableExpression = sqlStringGenerationContext.format(
( (Map) bootValueMapping ).getIndex().getTable().getQualifiedTableName()
);
}
else {
@ -1281,14 +1280,10 @@ public class MappingModelCreationHelper {
}
private static String getTableIdentifierExpression(Table table, MappingModelCreationProcess creationProcess) {
final JdbcEnvironment jdbcEnvironment = creationProcess.getCreationContext()
.getMetadata()
.getDatabase()
.getJdbcEnvironment();
return jdbcEnvironment.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
jdbcEnvironment.getDialect()
);
final SqlStringGenerationContext sqlStringGenerationContext = creationProcess.getCreationContext()
.getSessionFactory()
.getSqlStringGenerationContext();
return sqlStringGenerationContext.format( table.getQualifiedTableName() );
}
private static CollectionPart interpretMapKey(

View File

@ -15,9 +15,9 @@ import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.hibernate.LockMode;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.StringHelper;
@ -270,13 +270,8 @@ public class ToOneAttributeMapping
isKeyTableNullable = true;
}
else {
final JdbcServices jdbcServices = declaringEntityPersister.getFactory().getJdbcServices();
final String targetTableName = jdbcServices.getJdbcEnvironment()
.getQualifiedObjectNameFormatter()
.format(
manyToOne.getTable().getQualifiedTableName(),
jdbcServices.getDialect()
);
final SqlStringGenerationContext sqlStringGenerationContext = declaringEntityPersister.getFactory().getSqlStringGenerationContext();
final String targetTableName = sqlStringGenerationContext.format( manyToOne.getTable().getQualifiedTableName() );
if ( CollectionPart.Nature.fromNameExact( navigableRole.getParent().getLocalName() ) != null ) {
final PluralAttributeMapping pluralAttribute = (PluralAttributeMapping) declaringEntityPersister.resolveSubPart(
navigableRole.getParent().getParent()

View File

@ -10,7 +10,7 @@ import java.util.List;
import java.util.function.Consumer;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.IndexedConsumer;
@ -371,7 +371,7 @@ public class VirtualIdEmbeddable extends AbstractEmbeddableMapping implements Id
throw new IllegalAttributeType( "A \"virtual id\" cannot define <any/> attributes : " + attributeName );
}
},
(column, jdbcEnvironment) -> getTableIdentifierExpression( column.getValue().getTable(), jdbcEnvironment ),
(column, jdbcEnvironment) -> getTableIdentifierExpression( column.getValue().getTable(), creationProcess ),
this::addAttribute,
() -> {
// We need the attribute mapping types to finish initialization first before we can build the column mappings
@ -384,14 +384,11 @@ public class VirtualIdEmbeddable extends AbstractEmbeddableMapping implements Id
);
}
private static String getTableIdentifierExpression(Table table, JdbcEnvironment jdbcEnvironment) {
return jdbcEnvironment
.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
jdbcEnvironment.getDialect()
);
private static String getTableIdentifierExpression(Table table, MappingModelCreationProcess creationProcess) {
final SqlStringGenerationContext sqlStringGenerationContext = creationProcess.getCreationContext()
.getSessionFactory()
.getSqlStringGenerationContext();
return sqlStringGenerationContext.format( table.getQualifiedTableName() );
}
private boolean initColumnMappings() {

View File

@ -289,7 +289,6 @@ public abstract class AbstractCollectionPersister
: null;
final Database database = creationContext.getMetadata().getDatabase();
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
this.factory = creationContext.getSessionFactory();
this.cacheAccessStrategy = cacheAccessStrategy;
@ -321,7 +320,7 @@ public abstract class AbstractCollectionPersister
isArray = collectionBootDescriptor.isArray();
subselectLoadable = collectionBootDescriptor.isSubselectLoadable();
qualifiedTableName = determineTableName( table, jdbcEnvironment );
qualifiedTableName = determineTableName( table );
int spacesSize = 1 + collectionBootDescriptor.getSynchronizedTables().size();
spaces = new String[spacesSize];
@ -691,15 +690,12 @@ public abstract class AbstractCollectionPersister
return comparator;
}
protected String determineTableName(Table table, JdbcEnvironment jdbcEnvironment) {
protected String determineTableName(Table table) {
if ( table.getSubselect() != null ) {
return "( " + table.getSubselect() + " )";
}
return jdbcEnvironment.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
jdbcEnvironment.getDialect()
);
return factory.getSqlStringGenerationContext().format( table.getQualifiedTableName() );
}
// private class ColumnMapperImpl implements ColumnMapper {

View File

@ -5327,15 +5327,12 @@ public abstract class AbstractEntityPersister
return 0;
}
protected String determineTableName(Table table, JdbcEnvironment jdbcEnvironment) {
protected String determineTableName(Table table) {
if ( table.getSubselect() != null ) {
return "( " + table.getSubselect() + " )";
}
return jdbcEnvironment.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
jdbcEnvironment.getDialect()
);
return factory.getSqlStringGenerationContext().format( table.getQualifiedTableName() );
}
@Override

View File

@ -168,7 +168,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
final SessionFactoryImplementor factory = creationContext.getSessionFactory();
final Database database = creationContext.getMetadata().getDatabase();
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
// DISCRIMINATOR
@ -256,7 +255,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
while ( tItr.hasNext() ) {
final Table table = tItr.next();
final KeyValue key = kItr.next();
final String tableName = determineTableName( table, jdbcEnvironment );
final String tableName = determineTableName( table );
tableNames.add( tableName );
String[] keyCols = new String[idColumnSpan];
String[] keyColReaders = new String[idColumnSpan];
@ -289,7 +288,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
isInverseTable[tableIndex] = join.isInverse();
Table table = join.getTable();
final String tableName = determineTableName( table, jdbcEnvironment );
final String tableName = determineTableName( table );
tableNames.add( tableName );
KeyValue key = join.getKey();
@ -334,7 +333,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
isLazies.add( Boolean.FALSE );
isInverses.add( Boolean.FALSE );
isNullables.add( Boolean.FALSE );
final String tableName = determineTableName( tab, jdbcEnvironment );
final String tableName = determineTableName( tab );
subclassTableNames.add( tableName );
String[] key = new String[idColumnSpan];
Iterator<Column> cItr = tab.getPrimaryKey().getColumnIterator();
@ -356,7 +355,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
isNullables.add( join.isOptional() );
isLazies.add( join.isLazy() );
String joinTableName = determineTableName( joinTable, jdbcEnvironment );
String joinTableName = determineTableName( joinTable );
subclassTableNames.add( joinTableName );
String[] key = new String[idColumnSpan];
Iterator<Column> citer = joinTable.getPrimaryKey().getColumnIterator();
@ -480,7 +479,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
while ( iter.hasNext() ) {
Property prop = iter.next();
String tabname = prop.getValue().getTable().getQualifiedName(
factory.getJdbcServices().getDialect(),
factory.getSqlStringGenerationContext(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
);
@ -502,7 +501,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
Property prop = iter.next();
Table tab = prop.getValue().getTable();
String tabname = tab.getQualifiedName(
factory.getJdbcServices().getDialect(),
factory.getSqlStringGenerationContext(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
);
@ -544,13 +543,13 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
Table table = persistentClass.getTable();
final Object convertedDiscriminatorValue = discriminatorType.getJavaTypeDescriptor().fromString( discriminatorSQLString );
discriminatorValues = new String[subclassSpan];
initDiscriminatorProperties( factory, jdbcEnvironment, subclassSpanMinusOne, table, convertedDiscriminatorValue
initDiscriminatorProperties( factory, subclassSpanMinusOne, table, convertedDiscriminatorValue
);
notNullColumnTableNumbers = new int[subclassSpan];
final int id = getTableId(
table.getQualifiedName(
factory.getJdbcServices().getDialect(),
factory.getSqlStringGenerationContext(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
),
@ -615,11 +614,11 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
// "foo.class = Bar" works in HQL
discriminatorValue = sc.getSubclassId();
}
initDiscriminatorProperties( factory, jdbcEnvironment, k, table, discriminatorValue );
initDiscriminatorProperties( factory, k, table, discriminatorValue );
subclassesByDiscriminatorValue.put( discriminatorValue, sc.getEntityName() );
int id = getTableId(
table.getQualifiedName(
factory.getJdbcServices().getDialect(),
factory.getSqlStringGenerationContext(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
),
@ -645,11 +644,10 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
private void initDiscriminatorProperties(
SessionFactoryImplementor factory,
JdbcEnvironment jdbcEnvironment,
int k,
Table table,
Object discriminatorValue) {
final String tableName = determineTableName( table, jdbcEnvironment );
final String tableName = determineTableName( table );
final String columnName = table.getPrimaryKey().getColumn( 0 ).getQuotedName( factory.getJdbcServices().getDialect() );
discriminatorValuesByTableName.put( tableName, discriminatorValue );
discriminatorColumnNameByTableName.put( tableName, columnName );
@ -755,7 +753,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
SessionFactoryImplementor factory) {
final String tableName = persistentClass.getTable().getQualifiedName(
factory.getJdbcServices().getDialect(),
factory.getSqlStringGenerationContext(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
);
@ -766,7 +764,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
while ( itr.hasNext() ) {
final Join join = itr.next();
final String secondaryTableName = join.getTable().getQualifiedName(
factory.getJdbcServices().getDialect(),
factory.getSqlStringGenerationContext(),
factory.getSettings().getDefaultCatalogName(),
factory.getSettings().getDefaultSchemaName()
);

View File

@ -143,7 +143,6 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
final SessionFactoryImplementor factory = creationContext.getSessionFactory();
final Database database = creationContext.getMetadata().getDatabase();
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
// CLASS + TABLE
@ -153,7 +152,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
isNullableTable = new boolean[joinSpan];
keyColumnNames = new String[joinSpan][];
final Table table = persistentClass.getRootTable();
qualifiedTableNames[0] = determineTableName( table, jdbcEnvironment );
qualifiedTableNames[0] = determineTableName( table );
isInverseTable[0] = false;
isNullableTable[0] = false;
@ -194,7 +193,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
final Dialect dialect = factory.getJdbcServices().getDialect();
while ( joinIter.hasNext() ) {
Join join = joinIter.next();
qualifiedTableNames[j] = determineTableName( join.getTable(), jdbcEnvironment );
qualifiedTableNames[j] = determineTableName( join.getTable() );
isInverseTable[j] = join.isInverse();
isNullableTable[j] = join.isOptional();
cascadeDeleteEnabled[j] = join.getKey().isCascadeDeleteEnabled() &&
@ -271,7 +270,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
isDeferreds.add( isDeferred );
hasDeferred |= isDeferred;
String joinTableName = determineTableName( join.getTable(), jdbcEnvironment );
String joinTableName = determineTableName( join.getTable() );
subclassTables.add( joinTableName );
Iterator<Selectable> iter = join.getKey().getColumnIterator();

View File

@ -24,6 +24,7 @@ import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.access.NaturalIdDataAccess;
import org.hibernate.cfg.Settings;
@ -104,11 +105,10 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
final SessionFactoryImplementor factory = creationContext.getSessionFactory();
final Database database = creationContext.getMetadata().getDatabase();
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
// TABLE
tableName = determineTableName( persistentClass.getTable(), jdbcEnvironment );
tableName = determineTableName( persistentClass.getTable() );
subclassTableNames = new String[]{tableName};
//Custom SQL
@ -184,7 +184,7 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
HashSet<String> subclassTables = new HashSet<>();
Iterator<Table> subclassTableIter = persistentClass.getSubclassTableClosureIterator();
while ( subclassTableIter.hasNext() ) {
subclassTables.add( determineTableName( subclassTableIter.next(), jdbcEnvironment ) );
subclassTables.add( determineTableName( subclassTableIter.next() ) );
}
subclassSpaces = ArrayHelper.toStringArray( subclassTables );
@ -214,7 +214,7 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
while ( tableIter.hasNext() ) {
Table tab = tableIter.next();
if ( !tab.isAbstractUnionTable() ) {
final String tableName = determineTableName( tab, jdbcEnvironment );
final String tableName = determineTableName( tab );
tableNames.add( tableName );
String[] key = new String[idColumnSpan];
Iterator<Column> citer = tab.getPrimaryKey().getColumnIterator();
@ -456,10 +456,11 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
Dialect dialect = getFactory().getJdbcServices().getDialect();
Settings settings = getFactory().getSettings();
SqlStringGenerationContext sqlStringGenerationContext = getFactory().getSqlStringGenerationContext();
if ( !model.hasSubclasses() ) {
return model.getTable().getQualifiedName(
dialect,
sqlStringGenerationContext,
settings.getDefaultCatalogName(),
settings.getDefaultSchemaName()
);
@ -505,7 +506,7 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
buf.append( " from " )
.append(
table.getQualifiedName(
dialect,
sqlStringGenerationContext,
settings.getDefaultCatalogName(),
settings.getDefaultSchemaName()
)

View File

@ -11,6 +11,8 @@ import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.service.ServiceRegistry;
@ -22,6 +24,7 @@ import org.hibernate.tool.schema.extract.spi.ExtractionContext;
public class ExtractionContextImpl implements ExtractionContext {
private final ServiceRegistry serviceRegistry;
private final JdbcEnvironment jdbcEnvironment;
private final SqlStringGenerationContext sqlStringGenerationContext;
private final JdbcConnectionAccess jdbcConnectionAccess;
private final DatabaseObjectAccess registeredTableAccess;
private final Identifier defaultCatalogName;
@ -39,6 +42,7 @@ public class ExtractionContextImpl implements ExtractionContext {
Identifier defaultSchemaName) {
this.serviceRegistry = serviceRegistry;
this.jdbcEnvironment = jdbcEnvironment;
this.sqlStringGenerationContext = new SqlStringGenerationContextImpl( jdbcEnvironment );
this.jdbcConnectionAccess = jdbcConnectionAccess;
this.registeredTableAccess = registeredTableAccess;
this.defaultCatalogName = defaultCatalogName;
@ -55,6 +59,11 @@ public class ExtractionContextImpl implements ExtractionContext {
return jdbcEnvironment;
}
@Override
public SqlStringGenerationContext getSqlStringGenerationContext() {
return sqlStringGenerationContext;
}
@Override
public Connection getJdbcConnection() {
if ( jdbcConnection == null ) {

View File

@ -137,11 +137,10 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl extends AbstractInform
final ExtractionContext extractionContext = getExtractionContext();
// We use this dummy query to retrieve the table information through the ResultSetMetaData
// This is significantly better than to use the DatabaseMetaData especially on Oracle with synonyms enable
final String tableName = extractionContext.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
final String tableName = extractionContext.getSqlStringGenerationContext().format(
// The name comes from the database, so the case is correct
// But we quote here to avoid issues with reserved words
tableInformation.getName().quote(),
extractionContext.getJdbcEnvironment().getDialect()
tableInformation.getName().quote()
);
try {

View File

@ -16,6 +16,7 @@ import org.hibernate.Incubating;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.service.ServiceRegistry;
@ -30,6 +31,7 @@ import org.hibernate.service.ServiceRegistry;
public interface ExtractionContext {
ServiceRegistry getServiceRegistry();
JdbcEnvironment getJdbcEnvironment();
SqlStringGenerationContext getSqlStringGenerationContext();
Connection getJdbcConnection();
DatabaseMetaData getJdbcDatabaseMetaData();
@ -83,6 +85,11 @@ public interface ExtractionContext {
return null;
}
@Override
public SqlStringGenerationContext getSqlStringGenerationContext() {
return null;
}
@Override
public Connection getJdbcConnection() {
return null;

View File

@ -22,6 +22,8 @@ import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
@ -157,7 +159,9 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace, GenerationTarget[] targets);
Namespace namespace,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget[] targets);
private void performMigration(
Metadata metadata,
@ -172,6 +176,8 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
final Set<String> exportIdentifiers = CollectionHelper.setOfSize( 50 );
final Database database = metadata.getDatabase();
SqlStringGenerationContext sqlStringGenerationContext =
new SqlStringGenerationContextImpl( database.getJdbcEnvironment() );
// Drop all AuxiliaryDatabaseObjects
for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
@ -179,7 +185,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
applySqlStrings(
true,
dialect.getAuxiliaryDatabaseObjectExporter()
.getSqlDropStrings( auxiliaryDatabaseObject, metadata ),
.getSqlDropStrings( auxiliaryDatabaseObject, metadata, sqlStringGenerationContext ),
formatter,
options,
targets
@ -192,7 +198,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
if ( !auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect( dialect ) ) {
applySqlStrings(
true,
auxiliaryDatabaseObject.sqlCreateStrings( dialect ),
auxiliaryDatabaseObject.sqlCreateStrings( sqlStringGenerationContext ),
formatter,
options,
targets
@ -225,7 +231,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
tryToCreateSchemas,
exportedCatalogs,
namespace,
targets
sqlStringGenerationContext, targets
);
tablesInformation.put( namespace, nameSpaceTablesInformation );
if ( options.getSchemaFilter().includeNamespace( namespace ) ) {
@ -240,7 +246,8 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
false,
dialect.getSequenceExporter().getSqlCreateStrings(
sequence,
metadata
metadata,
sqlStringGenerationContext
),
formatter,
options,
@ -265,7 +272,8 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
final TableInformation tableInformation = nameSpaceTablesInformation.getTableInformation( table );
if ( tableInformation == null || tableInformation.isPhysicalTable() ) {
applyForeignKeys( table, tableInformation, dialect, metadata, formatter, options, targets );
applyForeignKeys( table, tableInformation, dialect, metadata, formatter, options,
sqlStringGenerationContext, targets );
}
}
}
@ -276,7 +284,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
if ( auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect( dialect )) {
applySqlStrings(
true,
auxiliaryDatabaseObject.sqlCreateStrings( dialect ),
auxiliaryDatabaseObject.sqlCreateStrings( sqlStringGenerationContext ),
formatter,
options,
targets
@ -291,10 +299,11 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget... targets) {
applySqlStrings(
false,
dialect.getTableExporter().getSqlCreateStrings( table, metadata ),
dialect.getTableExporter().getSqlCreateStrings( table, metadata, sqlStringGenerationContext ),
formatter,
options,
targets
@ -308,6 +317,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget... targets) {
final Database database = metadata.getDatabase();
@ -319,7 +329,8 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
metadata,
tableInformation,
database.getDefaultNamespace().getPhysicalName().getCatalog(),
database.getDefaultNamespace().getPhysicalName().getSchema()
database.getDefaultNamespace().getPhysicalName().getSchema(),
sqlStringGenerationContext
),
formatter,
options,
@ -334,6 +345,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget... targets) {
final Exporter<Index> exporter = dialect.getIndexExporter();
@ -348,7 +360,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
if ( existingIndex == null ) {
applySqlStrings(
false,
exporter.getSqlCreateStrings( index, metadata ),
exporter.getSqlCreateStrings( index, metadata, sqlStringGenerationContext ),
formatter,
options,
targets
@ -369,6 +381,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget... targets) {
if ( uniqueConstraintStrategy == null ) {
uniqueConstraintStrategy = determineUniqueConstraintSchemaUpdateStrategy( metadata );
@ -391,7 +404,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
if ( uniqueConstraintStrategy == UniqueConstraintSchemaUpdateStrategy.DROP_RECREATE_QUIETLY ) {
applySqlStrings(
true,
exporter.getSqlDropStrings( uniqueKey, metadata ),
exporter.getSqlDropStrings( uniqueKey, metadata, sqlStringGenerationContext ),
formatter,
options,
targets
@ -400,7 +413,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
applySqlStrings(
true,
exporter.getSqlCreateStrings( uniqueKey, metadata ),
exporter.getSqlCreateStrings( uniqueKey, metadata, sqlStringGenerationContext ),
formatter,
options,
targets
@ -427,6 +440,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget... targets) {
if ( dialect.hasAlterTable() ) {
final Exporter<ForeignKey> exporter = dialect.getForeignKeyExporter();
@ -450,7 +464,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
// in old SchemaUpdate code, this was the trigger to "create"
applySqlStrings(
false,
exporter.getSqlCreateStrings( foreignKey, metadata ),
exporter.getSqlCreateStrings( foreignKey, metadata, sqlStringGenerationContext ),
formatter,
options,
targets

View File

@ -11,6 +11,7 @@ import java.util.Set;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.internal.Formatter;
import org.hibernate.mapping.Table;
@ -48,7 +49,9 @@ public class GroupedSchemaMigratorImpl extends AbstractSchemaMigrator {
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace, GenerationTarget[] targets) {
Namespace namespace,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget[] targets) {
final NameSpaceTablesInformation tablesInformation =
new NameSpaceTablesInformation( metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper() );
@ -73,11 +76,12 @@ public class GroupedSchemaMigratorImpl extends AbstractSchemaMigrator {
checkExportIdentifier( table, exportIdentifiers );
final TableInformation tableInformation = tables.getTableInformation( table );
if ( tableInformation == null ) {
createTable( table, dialect, metadata, formatter, options, targets );
createTable( table, dialect, metadata, formatter, options, sqlStringGenerationContext, targets );
}
else if ( tableInformation.isPhysicalTable() ) {
tablesInformation.addTableInformation( tableInformation );
migrateTable( table, tableInformation, dialect, metadata, formatter, options, targets );
migrateTable( table, tableInformation, dialect, metadata, formatter, options,
sqlStringGenerationContext, targets );
}
}
}
@ -88,8 +92,10 @@ public class GroupedSchemaMigratorImpl extends AbstractSchemaMigrator {
&& contributableInclusionFilter.matches( table ) ) {
final TableInformation tableInformation = tablesInformation.getTableInformation( table );
if ( tableInformation == null || tableInformation.isPhysicalTable() ) {
applyIndexes( table, tableInformation, dialect, metadata, formatter, options, targets );
applyUniqueKeys( table, tableInformation, dialect, metadata, formatter, options, targets );
applyIndexes( table, tableInformation, dialect, metadata, formatter, options,
sqlStringGenerationContext, targets );
applyUniqueKeys( table, tableInformation, dialect, metadata, formatter, options,
sqlStringGenerationContext, targets );
}
}
}

View File

@ -11,6 +11,7 @@ import java.util.Set;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.internal.Formatter;
import org.hibernate.mapping.Table;
@ -49,6 +50,7 @@ public class IndividuallySchemaMigratorImpl extends AbstractSchemaMigrator {
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget[] targets) {
final NameSpaceTablesInformation tablesInformation =
new NameSpaceTablesInformation( metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper() );
@ -72,11 +74,12 @@ public class IndividuallySchemaMigratorImpl extends AbstractSchemaMigrator {
checkExportIdentifier( table, exportIdentifiers );
final TableInformation tableInformation = existingDatabase.getTableInformation( table.getQualifiedTableName() );
if ( tableInformation == null ) {
createTable( table, dialect, metadata, formatter, options, targets );
createTable( table, dialect, metadata, formatter, options, sqlStringGenerationContext, targets );
}
else if ( tableInformation.isPhysicalTable() ) {
tablesInformation.addTableInformation( tableInformation );
migrateTable( table, tableInformation, dialect, metadata, formatter, options, targets );
migrateTable( table, tableInformation, dialect, metadata, formatter, options,
sqlStringGenerationContext, targets );
}
}
}
@ -87,8 +90,10 @@ public class IndividuallySchemaMigratorImpl extends AbstractSchemaMigrator {
&& contributableInclusionFilter.matches( table ) ) {
final TableInformation tableInformation = tablesInformation.getTableInformation( table );
if ( tableInformation == null || tableInformation.isPhysicalTable() ) {
applyIndexes( table, tableInformation, dialect, metadata, formatter, options, targets );
applyUniqueKeys( table, tableInformation, dialect, metadata, formatter, options, targets );
applyIndexes( table, tableInformation, dialect, metadata, formatter, options,
sqlStringGenerationContext, targets );
applyUniqueKeys( table, tableInformation, dialect, metadata, formatter, options,
sqlStringGenerationContext, targets );
}
}
}

View File

@ -24,6 +24,8 @@ import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.InitCommand;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
@ -241,6 +243,8 @@ public class SchemaCreatorImpl implements SchemaCreator {
}
final Database database = metadata.getDatabase();
SqlStringGenerationContext sqlStringGenerationContext =
new SqlStringGenerationContextImpl( database.getJdbcEnvironment() );
final Set<String> exportIdentifiers = CollectionHelper.setOfSize( 50 );
@ -290,7 +294,8 @@ public class SchemaCreatorImpl implements SchemaCreator {
applySqlStrings(
dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(
auxiliaryDatabaseObject,
metadata
metadata,
sqlStringGenerationContext
),
formatter,
options,
@ -321,7 +326,8 @@ public class SchemaCreatorImpl implements SchemaCreator {
applySqlStrings(
dialect.getSequenceExporter().getSqlCreateStrings(
sequence,
metadata
metadata,
sqlStringGenerationContext
),
// dialect.getCreateSequenceStrings(
// jdbcEnvironment.getQualifiedObjectNameFormatter().format( sequence.getName(), dialect ),
@ -351,7 +357,7 @@ public class SchemaCreatorImpl implements SchemaCreator {
checkExportIdentifier( table, exportIdentifiers );
applySqlStrings(
dialect.getTableExporter().getSqlCreateStrings( table, metadata ),
dialect.getTableExporter().getSqlCreateStrings( table, metadata, sqlStringGenerationContext ),
formatter,
options,
targets
@ -377,7 +383,9 @@ public class SchemaCreatorImpl implements SchemaCreator {
final Index index = (Index) indexItr.next();
checkExportIdentifier( index, exportIdentifiers );
applySqlStrings(
dialect.getIndexExporter().getSqlCreateStrings( index, metadata ),
dialect.getIndexExporter().getSqlCreateStrings( index, metadata,
sqlStringGenerationContext
),
formatter,
options,
targets
@ -390,7 +398,9 @@ public class SchemaCreatorImpl implements SchemaCreator {
final UniqueKey uniqueKey = (UniqueKey) ukItr.next();
checkExportIdentifier( uniqueKey, exportIdentifiers );
applySqlStrings(
dialect.getUniqueKeyExporter().getSqlCreateStrings( uniqueKey, metadata ),
dialect.getUniqueKeyExporter().getSqlCreateStrings( uniqueKey, metadata,
sqlStringGenerationContext
),
formatter,
options,
targets
@ -421,7 +431,9 @@ public class SchemaCreatorImpl implements SchemaCreator {
while ( fkItr.hasNext() ) {
final ForeignKey foreignKey = (ForeignKey) fkItr.next();
applySqlStrings(
dialect.getForeignKeyExporter().getSqlCreateStrings( foreignKey, metadata ),
dialect.getForeignKeyExporter().getSqlCreateStrings( foreignKey, metadata,
sqlStringGenerationContext
),
formatter,
options,
targets
@ -436,7 +448,9 @@ public class SchemaCreatorImpl implements SchemaCreator {
&& !auxiliaryDatabaseObject.beforeTablesOnCreation() ) {
checkExportIdentifier( auxiliaryDatabaseObject, exportIdentifiers );
applySqlStrings(
dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings( auxiliaryDatabaseObject, metadata ),
dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings( auxiliaryDatabaseObject, metadata,
sqlStringGenerationContext
),
formatter,
options,
targets

View File

@ -238,7 +238,9 @@ public class SchemaDropperImpl implements SchemaDropper {
}
applySqlStrings(
dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings( auxiliaryDatabaseObject, metadata ),
dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings( auxiliaryDatabaseObject, metadata,
sqlStringGenerationContext
),
formatter,
options,
targets
@ -252,7 +254,8 @@ public class SchemaDropperImpl implements SchemaDropper {
}
// we need to drop all constraints/indexes prior to dropping the tables
applyConstraintDropping( namespace, metadata, formatter, options, contributableInclusionFilter, targets );
applyConstraintDropping( namespace, metadata, formatter, options, sqlStringGenerationContext,
contributableInclusionFilter, targets );
// now it's safe to drop the tables
for ( Table table : namespace.getTables() ) {
@ -267,7 +270,9 @@ public class SchemaDropperImpl implements SchemaDropper {
}
checkExportIdentifier( table, exportIdentifiers );
applySqlStrings( dialect.getTableExporter().getSqlDropStrings( table, metadata ), formatter, options,targets );
applySqlStrings( dialect.getTableExporter().getSqlDropStrings( table, metadata,
sqlStringGenerationContext
), formatter, options,targets );
}
for ( Sequence sequence : namespace.getSequences() ) {
@ -279,7 +284,9 @@ public class SchemaDropperImpl implements SchemaDropper {
}
checkExportIdentifier( sequence, exportIdentifiers );
applySqlStrings( dialect.getSequenceExporter().getSqlDropStrings( sequence, metadata ), formatter, options, targets );
applySqlStrings( dialect.getSequenceExporter().getSqlDropStrings( sequence, metadata,
sqlStringGenerationContext
), formatter, options, targets );
}
}
@ -343,6 +350,7 @@ public class SchemaDropperImpl implements SchemaDropper {
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
SqlStringGenerationContext sqlStringGenerationContext,
ContributableMatcher contributableInclusionFilter,
GenerationTarget... targets) {
final Dialect dialect = metadata.getDatabase().getJdbcEnvironment().getDialect();
@ -366,7 +374,9 @@ public class SchemaDropperImpl implements SchemaDropper {
while ( fks.hasNext() ) {
final ForeignKey foreignKey = (ForeignKey) fks.next();
applySqlStrings(
dialect.getForeignKeyExporter().getSqlDropStrings( foreignKey, metadata ),
dialect.getForeignKeyExporter().getSqlDropStrings( foreignKey, metadata,
sqlStringGenerationContext
),
formatter,
options,
targets

View File

@ -8,7 +8,7 @@ package org.hibernate.tool.schema.internal;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.tool.schema.spi.Exporter;
@ -23,12 +23,14 @@ public class StandardAuxiliaryDatabaseObjectExporter implements Exporter<Auxilia
}
@Override
public String[] getSqlCreateStrings(AuxiliaryDatabaseObject object, Metadata metadata) {
return object.sqlCreateStrings( new SqlStringGenerationContextImpl( metadata.getDatabase().getJdbcEnvironment() ) );
public String[] getSqlCreateStrings(AuxiliaryDatabaseObject object, Metadata metadata,
SqlStringGenerationContext context) {
return object.sqlCreateStrings( context );
}
@Override
public String[] getSqlDropStrings(AuxiliaryDatabaseObject object, Metadata metadata) {
return object.sqlDropStrings( new SqlStringGenerationContextImpl( metadata.getDatabase().getJdbcEnvironment() ) );
public String[] getSqlDropStrings(AuxiliaryDatabaseObject object, Metadata metadata,
SqlStringGenerationContext context) {
return object.sqlDropStrings( context );
}
}

View File

@ -11,6 +11,7 @@ import java.util.Locale;
import org.hibernate.AssertionFailure;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.mapping.Column;
@ -31,7 +32,7 @@ public class StandardForeignKeyExporter implements Exporter<ForeignKey> {
}
@Override
public String[] getSqlCreateStrings(ForeignKey foreignKey, Metadata metadata) {
public String[] getSqlCreateStrings(ForeignKey foreignKey, Metadata metadata, SqlStringGenerationContext context) {
if ( !dialect.hasAlterTable() ) {
return NO_COMMANDS;
}
@ -90,15 +91,8 @@ public class StandardForeignKeyExporter implements Exporter<ForeignKey> {
i++;
}
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String sourceTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
foreignKey.getTable().getQualifiedTableName(),
dialect
);
final String targetTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
foreignKey.getReferencedTable().getQualifiedTableName(),
dialect
);
final String sourceTableName = context.format( foreignKey.getTable().getQualifiedTableName() );
final String targetTableName = context.format( foreignKey.getReferencedTable().getQualifiedTableName() );
final StringBuilder buffer = new StringBuilder( dialect.getAlterTableString( sourceTableName ) )
.append(
@ -126,7 +120,7 @@ public class StandardForeignKeyExporter implements Exporter<ForeignKey> {
}
@Override
public String[] getSqlDropStrings(ForeignKey foreignKey, Metadata metadata) {
public String[] getSqlDropStrings(ForeignKey foreignKey, Metadata metadata, SqlStringGenerationContext context) {
if ( !dialect.hasAlterTable() ) {
return NO_COMMANDS;
}
@ -139,11 +133,7 @@ public class StandardForeignKeyExporter implements Exporter<ForeignKey> {
return NO_COMMANDS;
}
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String sourceTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
foreignKey.getTable().getQualifiedTableName(),
dialect
);
final String sourceTableName = context.format( foreignKey.getTable().getQualifiedTableName() );
return new String[] {
getSqlDropStrings( sourceTableName, foreignKey, dialect )
};

View File

@ -11,6 +11,7 @@ import java.util.Map;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.QualifiedNameImpl;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.internal.util.StringHelper;
@ -34,22 +35,18 @@ public class StandardIndexExporter implements Exporter<Index> {
}
@Override
public String[] getSqlCreateStrings(Index index, Metadata metadata) {
public String[] getSqlCreateStrings(Index index, Metadata metadata, SqlStringGenerationContext context) {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
index.getTable().getQualifiedTableName(),
dialect
);
final String tableName = context.format( index.getTable().getQualifiedTableName() );
final String indexNameForCreation;
if ( dialect.qualifyIndexName() ) {
indexNameForCreation = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
indexNameForCreation = context.format(
new QualifiedNameImpl(
index.getTable().getQualifiedTableName().getCatalogName(),
index.getTable().getQualifiedTableName().getSchemaName(),
jdbcEnvironment.getIdentifierHelper().toIdentifier( index.getQuotedName( dialect ) )
),
jdbcEnvironment.getDialect()
)
);
}
else {
@ -83,16 +80,12 @@ public class StandardIndexExporter implements Exporter<Index> {
}
@Override
public String[] getSqlDropStrings(Index index, Metadata metadata) {
public String[] getSqlDropStrings(Index index, Metadata metadata, SqlStringGenerationContext context) {
if ( !dialect.dropConstraints() ) {
return NO_COMMANDS;
}
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
index.getTable().getQualifiedTableName(),
dialect
);
final String tableName = context.format( index.getTable().getQualifiedTableName() );
final String indexNameForCreation;
if ( dialect.qualifyIndexName() ) {

View File

@ -9,8 +9,8 @@ package org.hibernate.tool.schema.internal;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.tool.schema.spi.Exporter;
/**
@ -24,26 +24,23 @@ public class StandardSequenceExporter implements Exporter<Sequence> {
}
@Override
public String[] getSqlCreateStrings(Sequence sequence, Metadata metadata) {
public String[] getSqlCreateStrings(Sequence sequence, Metadata metadata, SqlStringGenerationContext context) {
return dialect.getSequenceSupport().getCreateSequenceStrings(
getFormattedSequenceName( sequence.getName(), metadata ),
getFormattedSequenceName( sequence.getName(), metadata, context ),
sequence.getInitialValue(),
sequence.getIncrementSize()
);
}
@Override
public String[] getSqlDropStrings(Sequence sequence, Metadata metadata) {
public String[] getSqlDropStrings(Sequence sequence, Metadata metadata, SqlStringGenerationContext context) {
return dialect.getSequenceSupport().getDropSequenceStrings(
getFormattedSequenceName( sequence.getName(), metadata )
getFormattedSequenceName( sequence.getName(), metadata, context )
);
}
protected String getFormattedSequenceName(QualifiedSequenceName name, Metadata metadata) {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
return jdbcEnvironment.getQualifiedObjectNameFormatter().format(
name,
jdbcEnvironment.getDialect()
);
protected String getFormattedSequenceName(QualifiedSequenceName name, Metadata metadata,
SqlStringGenerationContext context) {
return context.format( name );
}
}

View File

@ -18,9 +18,7 @@ import org.hibernate.boot.model.relational.InitCommand;
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedNameParser;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Constraint;
import org.hibernate.mapping.Table;
@ -38,7 +36,8 @@ public class StandardTableExporter implements Exporter<Table> {
}
@Override
public String[] getSqlCreateStrings(Table table, Metadata metadata) {
public String[] getSqlCreateStrings(Table table, Metadata metadata,
SqlStringGenerationContext context) {
final QualifiedName tableName = new QualifiedNameParser.NameParts(
Identifier.toIdentifier( table.getCatalog(), table.isCatalogQuoted() ),
Identifier.toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
@ -46,17 +45,10 @@ public class StandardTableExporter implements Exporter<Table> {
);
try {
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
StringBuilder buf =
new StringBuilder( tableCreateString( table.hasPrimaryKey() ) )
.append( ' ' )
.append(
jdbcEnvironment.getQualifiedObjectNameFormatter().format(
tableName,
jdbcEnvironment.getDialect()
)
)
.append( context.format( tableName ) )
.append( " (" );
@ -122,7 +114,7 @@ public class StandardTableExporter implements Exporter<Table> {
uk.addColumn( col );
buf.append(
dialect.getUniqueDelegate()
.getColumnDefinitionUniquenessFragment( col )
.getColumnDefinitionUniquenessFragment( col, context )
);
}
@ -141,7 +133,7 @@ public class StandardTableExporter implements Exporter<Table> {
.append( table.getPrimaryKey().sqlConstraintString( dialect ) );
}
buf.append( dialect.getUniqueDelegate().getTableCreationUniqueConstraintsFragment( table ) );
buf.append( dialect.getUniqueDelegate().getTableCreationUniqueConstraintsFragment( table, context ) );
applyTableCheck( table, buf );
@ -158,8 +150,6 @@ public class StandardTableExporter implements Exporter<Table> {
applyComments( table, tableName, sqlStrings );
SqlStringGenerationContext context =
new SqlStringGenerationContextImpl( metadata.getDatabase().getJdbcEnvironment() );
applyInitCommands( table, sqlStrings, context );
return sqlStrings.toArray( new String[ sqlStrings.size() ] );
@ -212,7 +202,7 @@ public class StandardTableExporter implements Exporter<Table> {
}
@Override
public String[] getSqlDropStrings(Table table, Metadata metadata) {
public String[] getSqlDropStrings(Table table, Metadata metadata, SqlStringGenerationContext context) {
StringBuilder buf = new StringBuilder( "drop table " );
if ( dialect.supportsIfExistsBeforeTableName() ) {
buf.append( "if exists " );
@ -223,8 +213,7 @@ public class StandardTableExporter implements Exporter<Table> {
Identifier.toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
table.getNameIdentifier()
);
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
buf.append( jdbcEnvironment.getQualifiedObjectNameFormatter().format( tableName, jdbcEnvironment.getDialect() ) )
buf.append( context.format( tableName ) )
.append( dialect.getCascadeConstraintsString() );
if ( dialect.supportsIfExistsAfterTableName() ) {

View File

@ -7,6 +7,7 @@
package org.hibernate.tool.schema.internal;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.mapping.Constraint;
import org.hibernate.mapping.UniqueKey;
@ -26,21 +27,25 @@ public class StandardUniqueKeyExporter implements Exporter<Constraint> {
}
@Override
public String[] getSqlCreateStrings(Constraint constraint, Metadata metadata) {
public String[] getSqlCreateStrings(Constraint constraint, Metadata metadata,
SqlStringGenerationContext context) {
return new String[] {
dialect.getUniqueDelegate().getAlterTableToAddUniqueKeyCommand(
(UniqueKey) constraint,
metadata
metadata,
context
)
};
}
@Override
public String[] getSqlDropStrings(Constraint constraint, Metadata metadata) {
public String[] getSqlDropStrings(Constraint constraint, Metadata metadata,
SqlStringGenerationContext context) {
return new String[] {
dialect.getUniqueDelegate().getAlterTableToDropUniqueKeyCommand(
(UniqueKey) constraint,
metadata
metadata,
context
)
};
}

View File

@ -11,6 +11,8 @@ import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
import org.hibernate.service.ServiceRegistry;
@ -22,6 +24,7 @@ import org.hibernate.tool.schema.extract.spi.ExtractionContext;
public class ImprovedExtractionContextImpl implements ExtractionContext {
private final ServiceRegistry serviceRegistry;
private final JdbcEnvironment jdbcEnvironment;
private final SqlStringGenerationContext sqlStringGenerationContext;
private final DdlTransactionIsolator ddlTransactionIsolator;
private final Identifier defaultCatalog;
private final Identifier defaultSchema;
@ -39,6 +42,7 @@ public class ImprovedExtractionContextImpl implements ExtractionContext {
DatabaseObjectAccess databaseObjectAccess) {
this.serviceRegistry = serviceRegistry;
this.jdbcEnvironment = jdbcEnvironment;
this.sqlStringGenerationContext = new SqlStringGenerationContextImpl( jdbcEnvironment );
this.ddlTransactionIsolator = ddlTransactionIsolator;
this.defaultCatalog = defaultCatalog;
this.defaultSchema = defaultSchema;
@ -55,6 +59,11 @@ public class ImprovedExtractionContextImpl implements ExtractionContext {
return jdbcEnvironment;
}
@Override
public SqlStringGenerationContext getSqlStringGenerationContext() {
return sqlStringGenerationContext;
}
@Override
public Connection getJdbcConnection() {
return ddlTransactionIsolator.getIsolatedConnection();

View File

@ -9,6 +9,7 @@ package org.hibernate.tool.schema.spi;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.internal.util.collections.ArrayHelper;
/**
@ -27,12 +28,12 @@ public interface Exporter<T extends Exportable> {
*
* @return The commands needed for creation scripting.
*/
String[] getSqlCreateStrings(T exportable, Metadata metadata);
String[] getSqlCreateStrings(T exportable, Metadata metadata, SqlStringGenerationContext context);
/**
* Get the commands needed for dropping.
*
* @return The commands needed for drop scripting.
*/
String[] getSqlDropStrings(T exportable, Metadata metadata);
String[] getSqlDropStrings(T exportable, Metadata metadata, SqlStringGenerationContext context);
}

View File

@ -64,9 +64,8 @@ public class QualifiedTableNamingTest extends BaseNonConfigCoreFunctionalTestCas
assertEquals( 1, namespace.getTables().size() );
expectedName = metadata().getDatabase().getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
namespace.getTables().iterator().next().getQualifiedTableName(),
getDialect()
expectedName = sessionFactory().getSqlStringGenerationContext().format(
namespace.getTables().iterator().next().getQualifiedTableName()
);
}

View File

@ -12,6 +12,7 @@ import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
@ -565,7 +566,8 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase {
// JdbcServices jdbcServices,
// JdbcConnectionAccess connectionAccess,
// MetadataImplementor metadata,
// SessionFactoryOptions sessionFactoryOptions) {
// SessionFactoryOptions sessionFactoryOptions,
// SqlStringGenerationContext sqlStringGenerationContext) {
//
// }
//

View File

@ -8,6 +8,7 @@ package org.hibernate.orm.test.hbm.uk;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
@ -103,29 +104,33 @@ public class UniqueDelegateTest extends BaseUnitTestCase {
}
@Override
public String getColumnDefinitionUniquenessFragment(Column column) {
public String getColumnDefinitionUniquenessFragment(Column column,
SqlStringGenerationContext context) {
getColumnDefinitionUniquenessFragmentCallCount++;
return super.getColumnDefinitionUniquenessFragment( column );
return super.getColumnDefinitionUniquenessFragment( column, context );
}
@Override
public String getTableCreationUniqueConstraintsFragment(Table table) {
public String getTableCreationUniqueConstraintsFragment(Table table,
SqlStringGenerationContext context) {
getTableCreationUniqueConstraintsFragmentCallCount++;
return super.getTableCreationUniqueConstraintsFragment( table );
return super.getTableCreationUniqueConstraintsFragment( table, context );
}
@Override
public String getAlterTableToAddUniqueKeyCommand(
UniqueKey uniqueKey, Metadata metadata) {
UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
getAlterTableToAddUniqueKeyCommandCallCount++;
return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata );
return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata, context );
}
@Override
public String getAlterTableToDropUniqueKeyCommand(
UniqueKey uniqueKey, Metadata metadata) {
UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
getAlterTableToDropUniqueKeyCommandCallCount++;
return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata );
return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata, context );
}
}
}

View File

@ -10,6 +10,9 @@ import java.util.Properties;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
@ -55,8 +58,13 @@ public class Db2GenerationTest {
assertEquals( 1, metadata.getDatabase().getDefaultNamespace().getTables().size() );
final Table table = metadata.getDatabase().getDefaultNamespace().getTables().iterator().next();
final String[] createCommands = new DB2Dialect().getTableExporter().getSqlCreateStrings( table, metadata );
Database database = metadata.getDatabase();
final Table table = database.getDefaultNamespace().getTables().iterator().next();
SqlStringGenerationContext sqlStringGenerationContext =
SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() );
final String[] createCommands = new DB2Dialect().getTableExporter().getSqlCreateStrings( table, metadata,
sqlStringGenerationContext
);
assertThat( createCommands[0], containsString( "sequence_name varchar(255) not null" ) );
}
finally {
@ -93,8 +101,13 @@ public class Db2GenerationTest {
assertEquals( 1, metadata.getDatabase().getDefaultNamespace().getTables().size() );
final Table table = metadata.getDatabase().getDefaultNamespace().getTables().iterator().next();
final String[] createCommands = new DB2Dialect().getTableExporter().getSqlCreateStrings( table, metadata );
Database database = metadata.getDatabase();
final Table table = database.getDefaultNamespace().getTables().iterator().next();
SqlStringGenerationContext sqlStringGenerationContext =
SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() );
final String[] createCommands = new DB2Dialect().getTableExporter().getSqlCreateStrings( table, metadata,
sqlStringGenerationContext
);
assertThat( createCommands[0], containsString( "sequence_name varchar(255) not null" ) );
}
finally {

View File

@ -16,7 +16,9 @@ import org.hibernate.annotations.GenericGenerator;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -154,7 +156,10 @@ public class GeneratedValueTests extends BaseUnitTestCase {
null,
(RootClass) entityMapping
);
generator.initialize( SqlStringGenerationContextImpl.forTests( bootModel.getDatabase().getJdbcEnvironment() ) );
Database database = bootModel.getDatabase();
SqlStringGenerationContext sqlStringGenerationContext =
SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() );
generator.initialize( sqlStringGenerationContext );
final SequenceStyleGenerator sequenceStyleGenerator = assertTyping(
SequenceStyleGenerator.class,
@ -165,8 +170,7 @@ public class GeneratedValueTests extends BaseUnitTestCase {
assertThat( sequenceStyleGenerator.getDatabaseStructure().getInitialValue(), is( 100 ) );
assertThat( sequenceStyleGenerator.getDatabaseStructure().getIncrementSize(), is( 500 ) );
final Sequence sequence = bootModel.getDatabase()
.getDefaultNamespace()
final Sequence sequence = database.getDefaultNamespace()
.locateSequence( Identifier.toIdentifier( "my_db_sequence" ) );
assertThat( sequence, notNullValue() );
assertThat( sequence.getName().getSequenceName().getText(), is( "my_db_sequence" ) );
@ -175,7 +179,8 @@ public class GeneratedValueTests extends BaseUnitTestCase {
final String[] sqlCreateStrings = new H2Dialect().getSequenceExporter().getSqlCreateStrings(
sequence,
bootModel
bootModel,
sqlStringGenerationContext
);
assertThat( sqlCreateStrings.length, is( 1 ) );
final String cmd = sqlCreateStrings[0].toLowerCase();

View File

@ -12,6 +12,7 @@ import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.Namespace.Name;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.internal.Formatter;
@ -60,12 +61,10 @@ public class CheckForExistingForeignKeyTest {
ExecutionOptions options,
ContributableMatcher inclusionFilter,
Dialect dialect,
Formatter formatter,
Set<String> exportIdentifiers,
boolean tryToCreateCatalogs,
Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace,
Set<Identifier> exportedCatalogs, Namespace namespace,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget[] targets) {
return null;
}

View File

@ -14,6 +14,7 @@ import org.hibernate.boot.model.TruthValue;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.internal.Formatter;
import org.hibernate.testing.TestForIssue;
@ -46,21 +47,12 @@ public class AbstractSchemaMigratorTest {
public void testForeignKeyPreExistenceDetectionIgnoresCaseForTableAndColumnName() {
final AbstractSchemaMigrator schemaMigrator = new AbstractSchemaMigrator(null, null) {
@Override
protected NameSpaceTablesInformation performTablesMigration(
Metadata metadata,
DatabaseInformation existingDatabase,
ExecutionOptions options,
ContributableMatcher contributableInclusionFilter,
Dialect dialect,
Formatter formatter,
Set<String> exportIdentifiers,
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace,
GenerationTarget[] targets) {
return null;
}
protected NameSpaceTablesInformation performTablesMigration(Metadata metadata,
DatabaseInformation existingDatabase, ExecutionOptions options,ContributableMatcher contributableInclusionFilter, Dialect dialect,
Formatter formatter, Set<String> exportIdentifiers, boolean tryToCreateCatalogs,
boolean tryToCreateSchemas, Set<Identifier> exportedCatalogs, Namespace namespace,
SqlStringGenerationContext sqlStringGenerationContext,
GenerationTarget[] targets) { return null; }
};
final TableInformation existingTableInformation = mock(TableInformation.class);

View File

@ -12,6 +12,8 @@ import java.util.Optional;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.envers.enhanced.OrderedSequenceGenerator;
import org.hibernate.envers.enhanced.SequenceIdRevisionEntity;
@ -43,11 +45,13 @@ public class MonotonicRevisionNumberTest extends BaseEnversFunctionalTestCase {
Assert.assertTrue( OrderedSequenceGenerator.class.isInstance( generator ) );
Database database = metadata().getDatabase();
SqlStringGenerationContext sqlStringGenerationContext =
SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() );
Optional<AuxiliaryDatabaseObject> sequenceOptional = database.getAuxiliaryDatabaseObjects().stream()
.filter( o -> "REVISION_GENERATOR".equals( o.getExportIdentifier() ) )
.findFirst();
assertThat( sequenceOptional ).isPresent();
String[] sqlCreateStrings = sequenceOptional.get().sqlCreateStrings( database.getDialect() );
String[] sqlCreateStrings = sequenceOptional.get().sqlCreateStrings( sqlStringGenerationContext );
Assert.assertTrue(
"Oracle sequence needs to be ordered in RAC environment.",
sqlCreateStrings[0].toLowerCase().endsWith( " order" )