miscellaneous code cleanups and refactoring

This commit is contained in:
Gavin King 2024-10-04 21:26:06 +02:00
parent 3521857ef2
commit 072d3e257b
4 changed files with 156 additions and 152 deletions

View File

@ -68,8 +68,6 @@ import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.stat.Statistics;
import org.hibernate.type.format.FormatMapper;
import org.hibernate.type.format.jackson.JacksonIntegration;
import org.hibernate.type.format.jakartajson.JakartaJsonIntegration;
import org.hibernate.type.format.jaxb.JaxbXmlFormatMapper;
import jakarta.persistence.criteria.Nulls;
@ -142,6 +140,9 @@ import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
import static org.hibernate.internal.util.config.ConfigurationHelper.getInt;
import static org.hibernate.internal.util.config.ConfigurationHelper.getInteger;
import static org.hibernate.internal.util.config.ConfigurationHelper.getString;
import static org.hibernate.type.format.jackson.JacksonIntegration.getJsonJacksonFormatMapperOrNull;
import static org.hibernate.type.format.jackson.JacksonIntegration.getXMLJacksonFormatMapperOrNull;
import static org.hibernate.type.format.jakartajson.JakartaJsonIntegration.getJakartaJsonBFormatMapperOrNull;
/**
* In-flight state of {@link SessionFactoryOptions} during {@link org.hibernate.boot.SessionFactoryBuilder}
@ -648,10 +649,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private boolean disallowBatchUpdates(Dialect dialect, ExtractedDatabaseMetaData meta) {
final Boolean dialectAnswer = dialect.supportsBatchUpdates();
if ( dialectAnswer != null ) {
return !dialectAnswer;
}
return !meta.supportsBatchUpdates();
return dialectAnswer != null ? !dialectAnswer : !meta.supportsBatchUpdates();
}
@SuppressWarnings("unchecked")
@ -759,19 +757,20 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
if ( isEmpty( producerName ) ) {
return null;
}
//noinspection Convert2Lambda
return strategySelector.resolveDefaultableStrategy(
HqlTranslator.class,
producerName,
new Callable<>() {
@Override
public HqlTranslator call() throws Exception {
return (HqlTranslator) serviceRegistry.requireService( ClassLoaderService.class )
.classForName( producerName ).newInstance();
else {
//noinspection Convert2Lambda
return strategySelector.resolveDefaultableStrategy(
HqlTranslator.class,
producerName,
new Callable<>() {
@Override
public HqlTranslator call() throws Exception {
return (HqlTranslator) serviceRegistry.requireService( ClassLoaderService.class )
.classForName( producerName ).newInstance();
}
}
}
);
);
}
}
private SqmTranslatorFactory resolveSqmTranslator(
@ -780,21 +779,20 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
if ( isEmpty( translatorImplFqn ) ) {
return null;
}
return strategySelector.resolveStrategy(
SqmTranslatorFactory.class,
translatorImplFqn
);
else {
return strategySelector.resolveStrategy(
SqmTranslatorFactory.class,
translatorImplFqn
);
}
}
private static Interceptor determineInterceptor(
Map<String,Object> configurationSettings,
StrategySelector strategySelector) {
Object setting = configurationSettings.get( INTERCEPTOR );
return strategySelector.resolveStrategy(
Interceptor.class,
setting
configurationSettings.get( INTERCEPTOR )
);
}
@ -811,8 +809,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
return (Supplier<? extends Interceptor>) setting;
}
else if ( setting instanceof Class ) {
Class<? extends Interceptor> clazz = (Class<? extends Interceptor>) setting;
return interceptorSupplier( clazz );
return interceptorSupplier( (Class<? extends Interceptor>) setting );
}
else {
return interceptorSupplier(
@ -839,15 +836,12 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private PhysicalConnectionHandlingMode interpretConnectionHandlingMode(
Map<String,Object> configurationSettings,
StandardServiceRegistry serviceRegistry) {
final PhysicalConnectionHandlingMode specifiedHandlingMode = PhysicalConnectionHandlingMode.interpret(
configurationSettings.get( CONNECTION_HANDLING )
);
if ( specifiedHandlingMode != null ) {
return specifiedHandlingMode;
}
return serviceRegistry.requireService( TransactionCoordinatorBuilder.class ).getDefaultConnectionHandlingMode();
final PhysicalConnectionHandlingMode specifiedHandlingMode =
PhysicalConnectionHandlingMode.interpret( configurationSettings.get( CONNECTION_HANDLING ) );
return specifiedHandlingMode != null
? specifiedHandlingMode
: serviceRegistry.requireService( TransactionCoordinatorBuilder.class )
.getDefaultConnectionHandlingMode();
}
private static FormatMapper determineJsonFormatMapper(Object setting, StrategySelector strategySelector) {
@ -855,13 +849,8 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
FormatMapper.class,
setting,
(Callable<FormatMapper>) () -> {
final FormatMapper jsonJacksonFormatMapper = JacksonIntegration.getJsonJacksonFormatMapperOrNull();
if (jsonJacksonFormatMapper != null) {
return jsonJacksonFormatMapper;
}
else {
return JakartaJsonIntegration.getJakartaJsonBFormatMapperOrNull();
}
final FormatMapper jsonJacksonFormatMapper = getJsonJacksonFormatMapperOrNull();
return jsonJacksonFormatMapper != null ? jsonJacksonFormatMapper : getJakartaJsonBFormatMapperOrNull();
}
);
}
@ -871,11 +860,8 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
FormatMapper.class,
setting,
(Callable<FormatMapper>) () -> {
final FormatMapper jacksonFormatMapper = JacksonIntegration.getXMLJacksonFormatMapperOrNull();
if (jacksonFormatMapper != null) {
return jacksonFormatMapper;
}
return new JaxbXmlFormatMapper();
final FormatMapper jacksonFormatMapper = getXMLJacksonFormatMapperOrNull();
return jacksonFormatMapper != null ? jacksonFormatMapper : new JaxbXmlFormatMapper();
}
);
}
@ -1393,7 +1379,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
}
public void addSessionFactoryObservers(SessionFactoryObserver... observers) {
Collections.addAll( this.sessionFactoryObserverList, observers );
Collections.addAll( sessionFactoryObserverList, observers );
}
public void applyInterceptor(Interceptor interceptor) {
@ -1401,13 +1387,14 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
}
public void applyStatelessInterceptor(Class<? extends Interceptor> statelessInterceptorClass) {
this.applyStatelessInterceptorSupplier(
applyStatelessInterceptorSupplier(
() -> {
try {
return statelessInterceptorClass.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new HibernateException( String.format( "Could not supply stateless Interceptor of class %s", statelessInterceptorClass.getName()), e );
throw new HibernateException( "Could not supply stateless Interceptor of class '"
+ statelessInterceptorClass.getName() + "'", e );
}
}
);
@ -1563,10 +1550,10 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
}
public void applySqlFunction(String registrationName, SqmFunctionDescriptor sqlFunction) {
if ( this.sqlFunctions == null ) {
this.sqlFunctions = new HashMap<>();
if ( sqlFunctions == null ) {
sqlFunctions = new HashMap<>();
}
this.sqlFunctions.put( registrationName, sqlFunction );
sqlFunctions.put( registrationName, sqlFunction );
}
public void allowOutOfTransactionUpdateOperations(boolean allow) {
@ -1582,11 +1569,12 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
}
private MutableJpaCompliance mutableJpaCompliance() {
if ( !(this.jpaCompliance instanceof MutableJpaCompliance) ) {
if ( jpaCompliance instanceof MutableJpaCompliance mutableJpaCompliance ) {
return mutableJpaCompliance;
}
else {
throw new IllegalStateException( "JpaCompliance is no longer mutable" );
}
return (MutableJpaCompliance) this.jpaCompliance;
}
public void enableJpaTransactionCompliance(boolean enabled) {
@ -1627,10 +1615,9 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
}
public SessionFactoryOptions buildOptions() {
if ( this.jpaCompliance instanceof MutableJpaCompliance ) {
this.jpaCompliance = mutableJpaCompliance().immutableCopy();
if ( jpaCompliance instanceof MutableJpaCompliance ) {
jpaCompliance = mutableJpaCompliance().immutableCopy();
}
return this;
}
}

View File

@ -81,27 +81,13 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService, dialect ) );
final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
identifierHelperBuilder.setGloballyQuoteIdentifiers( globalQuoting( cfgService ) );
identifierHelperBuilder.setSkipGlobalQuotingForColumnDefinitions( globalQuotingSkippedForColumnDefinitions(
cfgService ) );
identifierHelperBuilder.setAutoQuoteKeywords( autoKeywordQuoting( cfgService ) );
identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );
final IdentifierHelperBuilder identifierHelperBuilder =
identifierHelperBuilder( cfgService, nameQualifierSupport );
IdentifierHelper identifierHelper = null;
ExtractedDatabaseMetaDataImpl.Builder dbMetaDataBuilder = new ExtractedDatabaseMetaDataImpl.Builder( this, false, null );
try {
identifierHelper = dialect.buildIdentifierHelper( identifierHelperBuilder, null );
dbMetaDataBuilder.setSupportsNamedParameters( dialect.supportsNamedParameters( null ) );
}
catch (SQLException sqle) {
// should never ever happen
log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
}
if ( identifierHelper == null ) {
identifierHelper = identifierHelperBuilder.build();
}
this.identifierHelper = identifierHelper;
final ExtractedDatabaseMetaDataImpl.Builder dbMetaDataBuilder =
new ExtractedDatabaseMetaDataImpl.Builder( this, false, null );
this.identifierHelper = identifierHelper( dialect, identifierHelperBuilder, dbMetaDataBuilder );;
this.extractedMetaDataSupport = dbMetaDataBuilder.build();
@ -117,12 +103,39 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder();
}
private static SqlAstTranslatorFactory resolveSqlAstTranslatorFactory(Dialect dialect) {
if ( dialect.getSqlAstTranslatorFactory() != null ) {
return dialect.getSqlAstTranslatorFactory();
}
private IdentifierHelperBuilder identifierHelperBuilder(
ConfigurationService cfgService, NameQualifierSupport nameQualifierSupport) {
final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
identifierHelperBuilder.setGloballyQuoteIdentifiers( globalQuoting( cfgService ) );
identifierHelperBuilder.setSkipGlobalQuotingForColumnDefinitions( globalQuotingSkippedForColumnDefinitions(
cfgService ) );
identifierHelperBuilder.setAutoQuoteKeywords( autoKeywordQuoting( cfgService ) );
identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );
return identifierHelperBuilder;
}
return new StandardSqlAstTranslatorFactory();
private static IdentifierHelper identifierHelper(
Dialect dialect,
IdentifierHelperBuilder identifierHelperBuilder,
ExtractedDatabaseMetaDataImpl.Builder dbMetaDataBuilder) {
try {
final IdentifierHelper identifierHelper = dialect.buildIdentifierHelper( identifierHelperBuilder, null );
dbMetaDataBuilder.setSupportsNamedParameters( dialect.supportsNamedParameters( null ) );
if ( identifierHelper != null ) {
return identifierHelper;
}
}
catch (SQLException sqle) {
// should never ever happen
log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
}
return identifierHelperBuilder.build();
}
private static SqlAstTranslatorFactory resolveSqlAstTranslatorFactory(Dialect dialect) {
return dialect.getSqlAstTranslatorFactory() != null
? dialect.getSqlAstTranslatorFactory()
: new StandardSqlAstTranslatorFactory();
}
private static boolean logWarnings(ConfigurationService cfgService, Dialect dialect) {
@ -170,31 +183,15 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, false );
NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport();
if ( nameQualifierSupport == null ) {
nameQualifierSupport = determineNameQualifierSupport( databaseMetaData );
}
this.nameQualifierSupport = nameQualifierSupport;
this.nameQualifierSupport = nameQualifierSupport( databaseMetaData, dialect );
final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );
IdentifierHelper identifierHelper = null;
try {
identifierHelper = dialect.buildIdentifierHelper( identifierHelperBuilder, databaseMetaData );
}
catch (SQLException sqle) {
// should never ever happen
log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
}
if ( identifierHelper == null ) {
identifierHelper = identifierHelperBuilder.build();
}
this.identifierHelper = identifierHelper;
this.identifierHelper = identifierHelper( databaseMetaData, dialect );
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this, true, jdbcConnectionAccess )
.apply( databaseMetaData )
.setSupportsNamedParameters( databaseMetaData.supportsNamedParameters() )
.build();
this.extractedMetaDataSupport =
new ExtractedDatabaseMetaDataImpl.Builder( this, true, jdbcConnectionAccess )
.apply( databaseMetaData )
.setSupportsNamedParameters( databaseMetaData.supportsNamedParameters() )
.build();
this.currentCatalog = null;
this.currentSchema = null;
@ -207,6 +204,29 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder();
}
private IdentifierHelper identifierHelper(DatabaseMetaData databaseMetaData, Dialect dialect) {
final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );
try {
final IdentifierHelper identifierHelper =
dialect.buildIdentifierHelper( identifierHelperBuilder, databaseMetaData );
if ( identifierHelper != null ) {
return identifierHelper;
}
}
catch (SQLException sqle) {
// should never ever happen
log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
}
return identifierHelperBuilder.build();
}
private NameQualifierSupport nameQualifierSupport(DatabaseMetaData databaseMetaData, Dialect dialect)
throws SQLException {
final NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport();
return nameQualifierSupport == null ? determineNameQualifierSupport( databaseMetaData ) : nameQualifierSupport;
}
private NameQualifierSupport determineNameQualifierSupport(DatabaseMetaData databaseMetaData) throws SQLException {
final boolean supportsCatalogs = databaseMetaData.supportsCatalogsInTableDefinitions();
final boolean supportsSchemas = databaseMetaData.supportsSchemasInTableDefinitions();
@ -261,36 +281,20 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService, dialect ) );
NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport();
if ( nameQualifierSupport == null ) {
nameQualifierSupport = determineNameQualifierSupport( databaseMetaData );
}
NameQualifierSupport nameQualifierSupport = nameQualifierSupport( databaseMetaData,
dialect );
this.nameQualifierSupport = nameQualifierSupport;
final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
identifierHelperBuilder.setGloballyQuoteIdentifiers( globalQuoting( cfgService ) );
identifierHelperBuilder.setSkipGlobalQuotingForColumnDefinitions( globalQuotingSkippedForColumnDefinitions(
cfgService ) );
identifierHelperBuilder.setAutoQuoteKeywords( autoKeywordQuoting( cfgService ) );
identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );
IdentifierHelper identifierHelper = null;
try {
identifierHelper = dialect.buildIdentifierHelper( identifierHelperBuilder, databaseMetaData );
}
catch (SQLException sqle) {
// should never ever happen
log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
}
if ( identifierHelper == null ) {
identifierHelper = identifierHelperBuilder.build();
}
this.identifierHelper = identifierHelper;
final IdentifierHelperBuilder identifierHelperBuilder =
identifierHelperBuilder( cfgService, nameQualifierSupport );
this.identifierHelper = identifierHelper( dialect, databaseMetaData, identifierHelperBuilder );
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this, true, jdbcConnectionAccess )
.apply( databaseMetaData )
.setConnectionSchemaName( determineCurrentSchemaName( databaseMetaData, serviceRegistry, dialect ) )
.setSupportsNamedParameters( dialect.supportsNamedParameters( databaseMetaData ) )
.build();
this.extractedMetaDataSupport =
new ExtractedDatabaseMetaDataImpl.Builder( this, true, jdbcConnectionAccess )
.apply( databaseMetaData )
.setConnectionSchemaName( determineCurrentSchemaName( databaseMetaData, serviceRegistry, dialect ) )
.setSupportsNamedParameters( dialect.supportsNamedParameters( databaseMetaData ) )
.build();
// and that current-catalog and current-schema happen after it
this.currentCatalog = identifierHelper.toIdentifier( extractedMetaDataSupport.getConnectionCatalogName() );
@ -308,25 +312,43 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
);
}
private static IdentifierHelper identifierHelper(
Dialect dialect, DatabaseMetaData databaseMetaData, IdentifierHelperBuilder identifierHelperBuilder) {
try {
final IdentifierHelper identifierHelper =
dialect.buildIdentifierHelper( identifierHelperBuilder, databaseMetaData );
if ( identifierHelper != null ) {
return identifierHelper;
}
}
catch (SQLException sqle) {
// should never ever happen
log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
}
return identifierHelperBuilder.build();
}
public static final String SCHEMA_NAME_RESOLVER = "hibernate.schema_name_resolver";
private String determineCurrentSchemaName(
DatabaseMetaData databaseMetaData,
ServiceRegistry serviceRegistry,
Dialect dialect) {
final Object setting =
serviceRegistry.requireService( ConfigurationService.class )
.getSettings().get( SCHEMA_NAME_RESOLVER );
final SchemaNameResolver schemaNameResolver;
final Object setting = serviceRegistry.requireService( ConfigurationService.class ).getSettings().get(
SCHEMA_NAME_RESOLVER );
if ( setting == null ) {
schemaNameResolver = dialect.getSchemaNameResolver();
}
else {
schemaNameResolver = serviceRegistry.requireService( StrategySelector.class ).resolveDefaultableStrategy(
SchemaNameResolver.class,
setting,
dialect.getSchemaNameResolver()
);
schemaNameResolver =
serviceRegistry.requireService( StrategySelector.class )
.resolveDefaultableStrategy(
SchemaNameResolver.class,
setting,
dialect.getSchemaNameResolver()
);
}
try {

View File

@ -43,7 +43,6 @@ import org.hibernate.metamodel.model.domain.SimpleDomainType;
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
import org.hibernate.metamodel.model.domain.internal.EntitySqmPathSource;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.BindableType;
import org.hibernate.query.IllegalQueryOperationException;
import org.hibernate.query.IllegalSelectQueryException;
import org.hibernate.query.Order;

View File

@ -1,16 +1,12 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.engine.internal;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.CoreMessageLogger;
@ -183,7 +179,7 @@ public class StatisticalLoggingSessionEventListenerTest {
// Number of lines
assertThat( sessionMetricsLog.lines().count() )
.as( "The StatisticalLoggingSessionEventListener should write a line per metric ("
+ numberOfMetrics + " lines) plus a header and a footer (2 lines)" )
+ numberOfMetrics + " lines) plus a header and a footer (2 lines)" )
.isEqualTo( numberOfMetrics + 2 );
// Total time
long sumDuration = metricList.stream().map( SessionMetric::getDuration ).mapToLong( Long::longValue ).sum();