Skip to content

Commit

Permalink
HHH-14921 Delay determination of the default catalog/schema until sch…
Browse files Browse the repository at this point in the history
…ema management tool or session factory creation
  • Loading branch information
yrodiere committed Dec 7, 2021
1 parent 36b0012 commit a9f1ada
Show file tree
Hide file tree
Showing 34 changed files with 307 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2197,8 +2197,6 @@ private void processExportableProducers() {
// for now we only handle id generators as ExportableProducers

final Dialect dialect = getDatabase().getJdbcEnvironment().getDialect();
final String defaultCatalog = extractName( getDatabase().getDefaultNamespace().getName().getCatalog(), dialect );
final String defaultSchema = extractName( getDatabase().getDefaultNamespace().getName().getSchema(), dialect );

for ( PersistentClass entityBinding : entityBindingMap.values() ) {
if ( entityBinding.isInherited() ) {
Expand All @@ -2208,8 +2206,6 @@ private void processExportableProducers() {
handleIdentifierValueBinding(
entityBinding.getIdentifier(),
dialect,
defaultCatalog,
defaultSchema,
(RootClass) entityBinding
);
}
Expand All @@ -2222,8 +2218,6 @@ private void processExportableProducers() {
handleIdentifierValueBinding(
( (IdentifierCollection) collection ).getIdentifier(),
dialect,
defaultCatalog,
defaultSchema,
null
);
}
Expand All @@ -2232,8 +2226,6 @@ private void processExportableProducers() {
private void handleIdentifierValueBinding(
KeyValue identifierValueBinding,
Dialect dialect,
String defaultCatalog,
String defaultSchema,
RootClass entityBinding) {
// todo : store this result (back into the entity or into the KeyValue, maybe?)
// This process of instantiating the id-generator is called multiple times.
Expand All @@ -2243,8 +2235,6 @@ private void handleIdentifierValueBinding(
final IdentifierGenerator ig = identifierValueBinding.createIdentifierGenerator(
getIdentifierGeneratorFactory(),
dialect,
defaultCatalog,
defaultSchema,
entityBinding
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,12 @@ public static class MappingDefaultsImpl implements MappingDefaults {
public MappingDefaultsImpl(StandardServiceRegistry serviceRegistry) {
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );

this.implicitSchemaName = configService.getSetting(
AvailableSettings.DEFAULT_SCHEMA,
StandardConverters.STRING,
null
);

this.implicitCatalogName = configService.getSetting(
AvailableSettings.DEFAULT_CATALOG,
StandardConverters.STRING,
null
);
// AvailableSettings.DEFAULT_SCHEMA and AvailableSettings.DEFAULT_CATALOG
// are taken into account later, at runtime, when rendering table/sequence names.
// These fields are exclusively about mapping defaults,
// overridden in XML mappings or through setters in MetadataBuilder.
this.implicitSchemaName = null;
this.implicitCatalogName = null;

this.implicitlyQuoteIdentifiers = configService.getSetting(
AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
import static org.hibernate.cfg.AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE;
import static org.hibernate.cfg.AvailableSettings.CUSTOM_ENTITY_DIRTINESS_STRATEGY;
import static org.hibernate.cfg.AvailableSettings.DEFAULT_BATCH_FETCH_SIZE;
import static org.hibernate.cfg.AvailableSettings.DEFAULT_CATALOG;
import static org.hibernate.cfg.AvailableSettings.DEFAULT_ENTITY_MODE;
import static org.hibernate.cfg.AvailableSettings.DEFAULT_SCHEMA;
import static org.hibernate.cfg.AvailableSettings.DELAY_ENTITY_LOADER_CREATIONS;
import static org.hibernate.cfg.AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS;
import static org.hibernate.cfg.AvailableSettings.FAIL_ON_PAGINATION_OVER_COLLECTION_FETCH;
Expand Down Expand Up @@ -242,6 +244,12 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private boolean queryParametersValidationEnabled;
private LiteralHandlingMode criteriaLiteralHandlingMode;
private ImmutableEntityUpdateQueryHandlingMode immutableEntityUpdateQueryHandlingMode;
// These two settings cannot be modified from the builder,
// in order to maintain consistency.
// Indeed, other components (the schema tools) also make use of these settings,
// and THOSE do not have access to session factory options.
private final String defaultCatalog;
private final String defaultSchema;

private Map<String, SQLFunction> sqlFunctions;

Expand Down Expand Up @@ -531,6 +539,9 @@ else if ( jdbcTimeZoneValue != null ) {
configurationSettings.get( IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE )
);

this.defaultCatalog = ConfigurationHelper.getString( DEFAULT_CATALOG, configurationSettings );
this.defaultSchema = ConfigurationHelper.getString( DEFAULT_SCHEMA, configurationSettings );

this.inClauseParameterPaddingEnabled = ConfigurationHelper.getBoolean(
IN_CLAUSE_PARAMETER_PADDING,
configurationSettings,
Expand Down Expand Up @@ -1047,6 +1058,16 @@ public ImmutableEntityUpdateQueryHandlingMode getImmutableEntityUpdateQueryHandl
return immutableEntityUpdateQueryHandlingMode;
}

@Override
public String getDefaultCatalog() {
return defaultCatalog;
}

@Override
public String getDefaultSchema() {
return defaultSchema;
}

@Override
public boolean jdbcStyleParamsZeroBased() {
return this.jdbcStyleParamsZeroBased;
Expand Down Expand Up @@ -1092,7 +1113,6 @@ public boolean isOmitJoinOfSuperclassTablesEnabled() {
return omitJoinOfSuperclassTablesEnabled;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// In-flight mutation access

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.boot.model.relational;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;

Expand All @@ -27,6 +28,22 @@ public interface SqlStringGenerationContext {
*/
IdentifierHelper getIdentifierHelper();

/**
* @return The default catalog, used for table/sequence names that do not explicitly mention a catalog.
* May be {@code null}.
* This default is generally applied automatically by the {@link #format(QualifiedName) format methods},
* but in some cases it can be useful to access it directly.
*/
Identifier getDefaultCatalog();

/**
* @return The default schema, used for table/sequence names that do not explicitly mention a schema.
* May be {@code null}.
* This default is generally applied automatically by the {@link #format(QualifiedName) format methods},
* but in some cases it can be useful to access it directly.
*/
Identifier getDefaultSchema();

/**
* Render a formatted a table name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,90 @@
*/
package org.hibernate.boot.model.relational.internal;

import java.util.Map;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.cfg.AvailableSettings;
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.NameQualifierSupport;
import org.hibernate.engine.jdbc.env.spi.QualifiedObjectNameFormatter;

public class SqlStringGenerationContextImpl
implements SqlStringGenerationContext {

/**
* @param jdbcEnvironment The JDBC environment, to extract the dialect, identifier helper, etc.
* @param database The database metadata, to retrieve the implicit namespace name configured through XML mapping.
* @param configurationMap The configuration map, holding settings such as {@link AvailableSettings#DEFAULT_SCHEMA}.
* @return An {@link SqlStringGenerationContext}.
*/
public static SqlStringGenerationContext fromConfigurationMap(JdbcEnvironment jdbcEnvironment,
Database database, Map<String, Object> configurationMap) {
String defaultCatalog = (String) configurationMap.get( AvailableSettings.DEFAULT_CATALOG );
String defaultSchema = (String) configurationMap.get( AvailableSettings.DEFAULT_SCHEMA );
return fromExplicit( jdbcEnvironment, database, defaultCatalog, defaultSchema );
}

/**
* @param jdbcEnvironment The JDBC environment, to extract the dialect, identifier helper, etc.
* @param database The database metadata, to retrieve the implicit namespace name configured through XML mapping.
* @param defaultCatalog The default catalog to use, unless an implicit catalog was configured through XML mapping.
* @param defaultSchema The default schema to use, unless an implicit schema was configured through XML mapping.
* @return An {@link SqlStringGenerationContext}.
*/
public static SqlStringGenerationContext fromExplicit(JdbcEnvironment jdbcEnvironment,
Database database, String defaultCatalog, String defaultSchema) {
Namespace.Name implicitNamespaceName = database.getDefaultNamespace().getPhysicalName();
IdentifierHelper identifierHelper = jdbcEnvironment.getIdentifierHelper();
NameQualifierSupport nameQualifierSupport = jdbcEnvironment.getNameQualifierSupport();
Identifier actualDefaultCatalog = null;
if ( nameQualifierSupport.supportsCatalogs() ) {
actualDefaultCatalog = implicitNamespaceName.getCatalog() != null
? implicitNamespaceName.getCatalog()
: identifierHelper.toIdentifier( defaultCatalog );
}
Identifier actualDefaultSchema = null;
if ( nameQualifierSupport.supportsSchemas() ) {
actualDefaultSchema = implicitNamespaceName.getSchema() != null
? implicitNamespaceName.getSchema()
: identifierHelper.toIdentifier( defaultSchema );
}
return new SqlStringGenerationContextImpl( jdbcEnvironment, actualDefaultCatalog, actualDefaultSchema );
}

public static SqlStringGenerationContext forTests(JdbcEnvironment jdbcEnvironment) {
return new SqlStringGenerationContextImpl( jdbcEnvironment );
return forTests( jdbcEnvironment, null, null );
}

public static SqlStringGenerationContext forTests(JdbcEnvironment jdbcEnvironment,
String defaultCatalog, String defaultSchema) {
IdentifierHelper identifierHelper = jdbcEnvironment.getIdentifierHelper();
return new SqlStringGenerationContextImpl( jdbcEnvironment,
identifierHelper.toIdentifier( defaultCatalog ), identifierHelper.toIdentifier( defaultSchema ) );
}

private final Dialect dialect;
private final IdentifierHelper identifierHelper;
private final QualifiedObjectNameFormatter qualifiedObjectNameFormatter;
private final Identifier defaultCatalog;
private final Identifier defaultSchema;

@SuppressWarnings("deprecation")
public SqlStringGenerationContextImpl(JdbcEnvironment jdbcEnvironment) {
private SqlStringGenerationContextImpl(JdbcEnvironment jdbcEnvironment,
Identifier defaultCatalog, Identifier defaultSchema) {
this.dialect = jdbcEnvironment.getDialect();
this.identifierHelper = jdbcEnvironment.getIdentifierHelper();
this.qualifiedObjectNameFormatter = jdbcEnvironment.getQualifiedObjectNameFormatter();
this.defaultCatalog = defaultCatalog;
this.defaultSchema = defaultSchema;
}

@Override
Expand All @@ -43,19 +102,60 @@ public IdentifierHelper getIdentifierHelper() {
return identifierHelper;
}

@Override
public Identifier getDefaultCatalog() {
return defaultCatalog;
}

@Override
public Identifier getDefaultSchema() {
return defaultSchema;
}

@Override
public String format(QualifiedTableName qualifiedName) {
return qualifiedObjectNameFormatter.format( qualifiedName, dialect );
return qualifiedObjectNameFormatter.format( withDefaults( qualifiedName ), dialect );
}

@Override
public String format(QualifiedSequenceName qualifiedName) {
return qualifiedObjectNameFormatter.format( qualifiedName, dialect );
return qualifiedObjectNameFormatter.format( withDefaults( qualifiedName ), dialect );
}

@Override
public String format(QualifiedName qualifiedName) {
return qualifiedObjectNameFormatter.format( qualifiedName, dialect );
return qualifiedObjectNameFormatter.format( withDefaults( qualifiedName ), dialect );
}

private QualifiedTableName withDefaults(QualifiedTableName name) {
if ( name.getCatalogName() == null && defaultCatalog != null
|| name.getSchemaName() == null && defaultSchema != null ) {
return new QualifiedTableName( withDefault( name.getCatalogName(), defaultCatalog ),
withDefault( name.getSchemaName(), defaultSchema ), name.getTableName() );
}
return name;
}

private QualifiedSequenceName withDefaults(QualifiedSequenceName name) {
if ( name.getCatalogName() == null && defaultCatalog != null
|| name.getSchemaName() == null && defaultSchema != null ) {
return new QualifiedSequenceName( withDefault( name.getCatalogName(), defaultCatalog ),
withDefault( name.getSchemaName(), defaultSchema ), name.getSequenceName() );
}
return name;
}

private QualifiedName withDefaults(QualifiedName name) {
if ( name.getCatalogName() == null && defaultCatalog != null
|| name.getSchemaName() == null && defaultSchema != null ) {
return new QualifiedSequenceName( withDefault( name.getCatalogName(), defaultCatalog ),
withDefault( name.getSchemaName(), defaultSchema ), name.getObjectName() );
}
return name;
}

private static Identifier withDefault(Identifier value, Identifier defaultValue) {
return value != null ? value : defaultValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,17 +804,13 @@ private void makeIdentifier(
// YUCK! but cannot think of a clean way to do this given the string-config based scheme
params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, objectNameNormalizer);

if ( database.getDefaultNamespace().getPhysicalName().getSchema() != null ) {
params.setProperty(
PersistentIdentifierGenerator.SCHEMA,
database.getDefaultNamespace().getPhysicalName().getSchema().render( database.getDialect() )
);
String implicitSchemaName = metadataBuildingContext.getMappingDefaults().getImplicitSchemaName();
if ( implicitSchemaName != null ) {
params.setProperty( PersistentIdentifierGenerator.SCHEMA, implicitSchemaName );
}
if ( database.getDefaultNamespace().getPhysicalName().getCatalog() != null ) {
params.setProperty(
PersistentIdentifierGenerator.CATALOG,
database.getDefaultNamespace().getPhysicalName().getCatalog().render( database.getDialect() )
);
String implicitCatalogName = metadataBuildingContext.getMappingDefaults().getImplicitCatalogName();
if ( implicitCatalogName != null ) {
params.setProperty( PersistentIdentifierGenerator.CATALOG, implicitCatalogName );
}

params.putAll( generator.getParameters() );
Expand Down Expand Up @@ -2961,7 +2957,7 @@ private Identifier determineCatalogName(TableSpecificationSource tableSpecSource
return database.toIdentifier( tableSpecSource.getExplicitCatalogName() );
}
else {
return database.getDefaultNamespace().getName().getCatalog();
return database.toIdentifier( metadataBuildingContext.getMappingDefaults().getImplicitCatalogName() );
}
}

Expand All @@ -2970,7 +2966,7 @@ private Identifier determineSchemaName(TableSpecificationSource tableSpecSource)
return database.toIdentifier( tableSpecSource.getExplicitSchemaName() );
}
else {
return database.getDefaultNamespace().getName().getSchema();
return database.toIdentifier( metadataBuildingContext.getMappingDefaults().getImplicitSchemaName() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ public ImmutableEntityUpdateQueryHandlingMode getImmutableEntityUpdateQueryHandl
return delegate.getImmutableEntityUpdateQueryHandlingMode();
}

@Override
public String getDefaultCatalog() {
return delegate.getDefaultCatalog();
}

@Override
public String getDefaultSchema() {
return delegate.getDefaultSchema();
}

@Override
public boolean inClauseParameterPaddingEnabled() {
return delegate.inClauseParameterPaddingEnabled();
Expand Down
Loading

0 comments on commit a9f1ada

Please sign in to comment.