simplify ObjectNameNormalizer

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-20 11:33:59 +02:00 committed by Steve Ebersole
parent 3946cfaf0b
commit 51acbd04dc
5 changed files with 21 additions and 52 deletions

View File

@ -36,12 +36,7 @@ public class MetadataBuildingContextRootImpl implements MetadataBuildingContext
this.options = options; this.options = options;
this.mappingDefaults = mappingDefaults; this.mappingDefaults = mappingDefaults;
this.metadataCollector = metadataCollector; this.metadataCollector = metadataCollector;
this.objectNameNormalizer = new ObjectNameNormalizer() { this.objectNameNormalizer = new ObjectNameNormalizer(this);
@Override
protected MetadataBuildingContext getBuildingContext() {
return MetadataBuildingContextRootImpl.this;
}
};
this.typeDefinitionRegistry = new TypeDefinitionRegistryStandardImpl(); this.typeDefinitionRegistry = new TypeDefinitionRegistryStandardImpl();
} }

View File

@ -15,8 +15,12 @@ import org.hibernate.internal.util.StringHelper;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public abstract class ObjectNameNormalizer { public class ObjectNameNormalizer {
private Database database; private final MetadataBuildingContext context;
public ObjectNameNormalizer(MetadataBuildingContext context) {
this.context = context;
}
/** /**
* Normalizes the quoting of identifiers. * Normalizes the quoting of identifiers.
@ -35,17 +39,11 @@ public abstract class ObjectNameNormalizer {
} }
protected Database database() { protected Database database() {
if ( database == null ) { return getBuildingContext().getMetadataCollector().getDatabase();
database = getBuildingContext().getMetadataCollector().getDatabase();
}
return database;
} }
public Identifier normalizeIdentifierQuoting(Identifier identifier) { public Identifier normalizeIdentifierQuoting(Identifier identifier) {
return getBuildingContext().getMetadataCollector() return database().getJdbcEnvironment().getIdentifierHelper()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper()
.normalizeQuoting( identifier ); .normalizeQuoting( identifier );
} }
@ -74,20 +72,10 @@ public abstract class ObjectNameNormalizer {
* @return The logical name * @return The logical name
*/ */
public Identifier determineLogicalName(String explicitName, NamingStrategyHelper namingStrategyHelper) { public Identifier determineLogicalName(String explicitName, NamingStrategyHelper namingStrategyHelper) {
Identifier logicalName; final Identifier logicalName = StringHelper.isEmpty( explicitName )
if ( StringHelper.isEmpty( explicitName ) ) { ? namingStrategyHelper.determineImplicitName( getBuildingContext() )
logicalName = namingStrategyHelper.determineImplicitName( getBuildingContext() ); : namingStrategyHelper.handleExplicitName( explicitName, getBuildingContext() );
} return database().getJdbcEnvironment().getIdentifierHelper().normalizeQuoting( logicalName );
else {
logicalName = namingStrategyHelper.handleExplicitName( explicitName, getBuildingContext() );
}
logicalName = getBuildingContext().getMetadataCollector()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper()
.normalizeQuoting( logicalName );
return logicalName;
} }
/** /**
@ -116,5 +104,7 @@ public abstract class ObjectNameNormalizer {
* *
* @return The current building context * @return The current building context
*/ */
protected abstract MetadataBuildingContext getBuildingContext(); protected MetadataBuildingContext getBuildingContext() {
return context;
}
} }

View File

@ -90,8 +90,7 @@ public class IncrementGenerator implements IdentifierGenerator, StandardGenerato
returnClass = type.getReturnedClass(); returnClass = type.getReturnedClass();
final JdbcEnvironment jdbcEnvironment = serviceRegistry.requireService( JdbcEnvironment.class ); final JdbcEnvironment jdbcEnvironment = serviceRegistry.requireService( JdbcEnvironment.class );
final ObjectNameNormalizer normalizer = final ObjectNameNormalizer normalizer = (ObjectNameNormalizer) parameters.get( IDENTIFIER_NORMALIZER );
(ObjectNameNormalizer) parameters.get( IDENTIFIER_NORMALIZER );
column = parameters.getProperty( COLUMN ); column = parameters.getProperty( COLUMN );
if ( column == null ) { if ( column == null ) {
@ -101,12 +100,8 @@ public class IncrementGenerator implements IdentifierGenerator, StandardGenerato
IdentifierHelper identifierHelper = jdbcEnvironment.getIdentifierHelper(); IdentifierHelper identifierHelper = jdbcEnvironment.getIdentifierHelper();
final String schema = normalizer.toDatabaseIdentifierText( final String schema = normalizer.toDatabaseIdentifierText( parameters.getProperty( SCHEMA ) );
parameters.getProperty( SCHEMA ) final String catalog = normalizer.toDatabaseIdentifierText( parameters.getProperty( CATALOG ) );
);
final String catalog = normalizer.toDatabaseIdentifierText(
parameters.getProperty( CATALOG )
);
String tableList = parameters.getProperty( TABLES ); String tableList = parameters.getProperty( TABLES );
if ( tableList == null ) { if ( tableList == null ) {

View File

@ -48,12 +48,7 @@ public class EnversMetadataBuildingContextImpl implements EnversMetadataBuilding
this.auditEntityNameRegistry = new AuditEntityNameRegister(); this.auditEntityNameRegistry = new AuditEntityNameRegister();
this.auditEntityConfigurationRegistry = new AuditEntityConfigurationRegistry(); this.auditEntityConfigurationRegistry = new AuditEntityConfigurationRegistry();
this.objectNameNormalizer = new ObjectNameNormalizer() { this.objectNameNormalizer = new ObjectNameNormalizer(this);
@Override
protected MetadataBuildingContext getBuildingContext() {
return EnversMetadataBuildingContextImpl.this;
}
};
} }
@Override @Override

View File

@ -17,7 +17,6 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.spi.BootstrapContext; import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.EffectiveMappingDefaults; import org.hibernate.boot.spi.EffectiveMappingDefaults;
import org.hibernate.boot.spi.InFlightMetadataCollector; import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MappingDefaults;
import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.MetadataBuildingOptions; import org.hibernate.boot.spi.MetadataBuildingOptions;
@ -41,12 +40,7 @@ public class MetadataBuildingContextTestingImpl implements MetadataBuildingConte
new PersistenceUnitMetadataImpl() new PersistenceUnitMetadataImpl()
); );
metadataCollector = new InFlightMetadataCollectorImpl( bootstrapContext, buildingOptions ); metadataCollector = new InFlightMetadataCollectorImpl( bootstrapContext, buildingOptions );
objectNameNormalizer = new ObjectNameNormalizer() { objectNameNormalizer = new ObjectNameNormalizer(this);
@Override
protected MetadataBuildingContext getBuildingContext() {
return MetadataBuildingContextTestingImpl.this;
}
};
typeDefinitionRegistry = new TypeDefinitionRegistryStandardImpl(); typeDefinitionRegistry = new TypeDefinitionRegistryStandardImpl();
bootstrapContext.getTypeConfiguration().scope( this ); bootstrapContext.getTypeConfiguration().scope( this );
} }