HHH-16716 Expose ParameterMarkerStrategy on JDBCServices

This commit is contained in:
Sanne Grinovero 2023-05-30 09:02:52 +01:00 committed by Sanne Grinovero
parent 9da4ef0239
commit 3a40f9c829
5 changed files with 33 additions and 20 deletions

View File

@ -8,7 +8,6 @@ package org.hibernate.engine.jdbc.internal;
import java.util.Map; import java.util.Map;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.LobCreator; import org.hibernate.engine.jdbc.LobCreator;
@ -19,10 +18,10 @@ import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger; import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.spi.Configurable; import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceRegistryAwareService; import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.sql.ast.spi.ParameterMarkerStrategy;
/** /**
* Standard implementation of the {@link JdbcServices} contract * Standard implementation of the {@link JdbcServices} contract
@ -34,6 +33,7 @@ public class JdbcServicesImpl implements JdbcServices, ServiceRegistryAwareServi
private JdbcEnvironment jdbcEnvironment; private JdbcEnvironment jdbcEnvironment;
private SqlStatementLogger sqlStatementLogger; private SqlStatementLogger sqlStatementLogger;
private ParameterMarkerStrategy parameterMarkerStrategy;
@Override @Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) { public void injectServices(ServiceRegistryImplementor serviceRegistry) {
@ -46,6 +46,7 @@ public class JdbcServicesImpl implements JdbcServices, ServiceRegistryAwareServi
assert jdbcEnvironment != null : "JdbcEnvironment was not found"; assert jdbcEnvironment != null : "JdbcEnvironment was not found";
this.sqlStatementLogger = serviceRegistry.getService( SqlStatementLogger.class ); this.sqlStatementLogger = serviceRegistry.getService( SqlStatementLogger.class );
this.parameterMarkerStrategy = serviceRegistry.getService( ParameterMarkerStrategy.class );
} }
@Override @Override
@ -71,6 +72,11 @@ public class JdbcServicesImpl implements JdbcServices, ServiceRegistryAwareServi
return sqlStatementLogger; return sqlStatementLogger;
} }
@Override
public ParameterMarkerStrategy getParameterMarkerStrategy() {
return parameterMarkerStrategy;
}
@Override @Override
public SqlExceptionHelper getSqlExceptionHelper() { public SqlExceptionHelper getSqlExceptionHelper() {
if ( jdbcEnvironment != null ) { if ( jdbcEnvironment != null ) {

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.engine.jdbc.spi; package org.hibernate.engine.jdbc.spi;
import org.hibernate.Incubating;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.LobCreator; import org.hibernate.engine.jdbc.LobCreator;
@ -13,6 +14,7 @@ import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData; import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.service.Service; import org.hibernate.service.Service;
import org.hibernate.sql.ast.spi.ParameterMarkerStrategy;
import org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl; import org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl;
import org.hibernate.sql.exec.internal.StandardJdbcMutationExecutor; import org.hibernate.sql.exec.internal.StandardJdbcMutationExecutor;
import org.hibernate.sql.exec.spi.JdbcMutationExecutor; import org.hibernate.sql.exec.spi.JdbcMutationExecutor;
@ -51,6 +53,13 @@ public interface JdbcServices extends Service {
*/ */
SqlStatementLogger getSqlStatementLogger(); SqlStatementLogger getSqlStatementLogger();
/**
* Obtains the service used for marking SQL parameters
* @return the registered ParameterMarkerStrategy implementation.
*/
@Incubating
ParameterMarkerStrategy getParameterMarkerStrategy();
/** /**
* Obtain service for dealing with exceptions. * Obtain service for dealing with exceptions.
* *

View File

@ -17,8 +17,8 @@ import org.hibernate.Internal;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.FastSessionServices;
import org.hibernate.sql.ast.spi.ParameterMarkerStrategy; import org.hibernate.sql.ast.spi.ParameterMarkerStrategy;
/** /**
@ -42,21 +42,10 @@ public class SimpleSelect implements RestrictionRenderingContext {
private final ParameterMarkerStrategy parameterMarkerStrategy; private final ParameterMarkerStrategy parameterMarkerStrategy;
private int parameterCount; private int parameterCount;
public SimpleSelect(SessionFactoryImplementor factory) { public SimpleSelect(final SessionFactoryImplementor factory) {
this.dialect = lookupDialect( factory ); final JdbcServices jdbcServices = factory.getJdbcServices();
this.parameterMarkerStrategy = lookupParameterMarkerStrategy( factory ); this.dialect = jdbcServices.getDialect();
} this.parameterMarkerStrategy = jdbcServices.getParameterMarkerStrategy();
//A SimpleSelect constructor might be invoked before the FastSessionServices are defined
private static Dialect lookupDialect(final SessionFactoryImplementor factory) {
final FastSessionServices fastSessionServices = factory.getFastSessionServices();
return fastSessionServices != null ? fastSessionServices.dialect : factory.getJdbcServices().getDialect();
}
//A SimpleSelect constructor might be invoked before the FastSessionServices are defined
private static ParameterMarkerStrategy lookupParameterMarkerStrategy(final SessionFactoryImplementor factory) {
final FastSessionServices fastSessionServices = factory.getFastSessionServices();
return fastSessionServices != null ? fastSessionServices.parameterMarkerStrategy : factory.getServiceRegistry().getService( ParameterMarkerStrategy.class );
} }
@Override @Override

View File

@ -319,9 +319,10 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
protected AbstractSqlAstTranslator(SessionFactoryImplementor sessionFactory, Statement statement) { protected AbstractSqlAstTranslator(SessionFactoryImplementor sessionFactory, Statement statement) {
this.sessionFactory = sessionFactory; this.sessionFactory = sessionFactory;
this.dialect = sessionFactory.getJdbcServices().getDialect(); final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
this.dialect = jdbcServices.getDialect();
this.statementStack.push( statement ); this.statementStack.push( statement );
this.parameterMarkerStrategy = sessionFactory.getServiceRegistry().getService( ParameterMarkerStrategy.class ); this.parameterMarkerStrategy = jdbcServices.getParameterMarkerStrategy();
} }
private static Clause matchWithClause(Clause clause) { private static Clause matchWithClause(Clause clause) {

View File

@ -24,6 +24,7 @@ import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceRegistryAwareService; import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.Stoppable; import org.hibernate.service.spi.Stoppable;
import org.hibernate.sql.ast.spi.ParameterMarkerStrategy;
import org.hibernate.testing.env.ConnectionProviderBuilder; import org.hibernate.testing.env.ConnectionProviderBuilder;
@ -44,6 +45,7 @@ public class BasicTestingJdbcServiceImpl implements JdbcServices, ServiceRegistr
private JdbcConnectionAccess jdbcConnectionAccess; private JdbcConnectionAccess jdbcConnectionAccess;
private ServiceRegistry serviceRegistry; private ServiceRegistry serviceRegistry;
private ParameterMarkerStrategy parameterMarkerStrategy;
public void start() { public void start() {
} }
@ -100,6 +102,11 @@ public class BasicTestingJdbcServiceImpl implements JdbcServices, ServiceRegistr
return sqlStatementLogger; return sqlStatementLogger;
} }
@Override
public ParameterMarkerStrategy getParameterMarkerStrategy() {
return parameterMarkerStrategy;
}
public SqlExceptionHelper getSqlExceptionHelper() { public SqlExceptionHelper getSqlExceptionHelper() {
return jdbcEnvironment.getSqlExceptionHelper(); return jdbcEnvironment.getSqlExceptionHelper();
} }
@ -111,5 +118,6 @@ public class BasicTestingJdbcServiceImpl implements JdbcServices, ServiceRegistr
@Override @Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) { public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry; this.serviceRegistry = serviceRegistry;
this.parameterMarkerStrategy = serviceRegistry.getService( ParameterMarkerStrategy.class );
} }
} }