pluggable SQL AST translators

This commit is contained in:
Steve Ebersole 2019-09-23 12:53:51 -05:00
parent 35860baad6
commit 4149350222
15 changed files with 174 additions and 66 deletions

View File

@ -61,7 +61,7 @@ import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.query.hql.HqlTranslator; import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.query.sqm.produce.function.SqmFunctionRegistry; import org.hibernate.query.sqm.produce.function.SqmFunctionRegistry;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverterFactory; import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder; import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
@ -214,7 +214,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private HqlTranslator hqlTranslator; private HqlTranslator hqlTranslator;
private SqmMultiTableMutationStrategy sqmMultiTableMutationStrategy; private SqmMultiTableMutationStrategy sqmMultiTableMutationStrategy;
private SqmFunctionRegistry sqmFunctionRegistry; private SqmFunctionRegistry sqmFunctionRegistry;
private SqmToSqlAstConverterFactory sqmTranslatorFactory; private SqmTranslatorFactory sqmTranslatorFactory;
private Boolean useOfJdbcNamedParametersEnabled; private Boolean useOfJdbcNamedParametersEnabled;
private Map querySubstitutions; private Map querySubstitutions;
private boolean namedQueryStartupCheckingEnabled; private boolean namedQueryStartupCheckingEnabled;
@ -622,7 +622,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
); );
} }
private SqmToSqlAstConverterFactory resolveSqmTranslator( private SqmTranslatorFactory resolveSqmTranslator(
String translatorImplFqn, String translatorImplFqn,
StandardServiceRegistry serviceRegistry, StandardServiceRegistry serviceRegistry,
StrategySelector strategySelector) { StrategySelector strategySelector) {
@ -631,7 +631,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
} }
return strategySelector.resolveStrategy( return strategySelector.resolveStrategy(
SqmToSqlAstConverterFactory.class, SqmTranslatorFactory.class,
translatorImplFqn translatorImplFqn
); );
} }
@ -863,7 +863,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
} }
@Override @Override
public SqmToSqlAstConverterFactory getSqmTranslatorFactory() { public SqmTranslatorFactory getSqmTranslatorFactory() {
return sqmTranslatorFactory; return sqmTranslatorFactory;
} }

View File

@ -33,7 +33,7 @@ import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.query.hql.HqlTranslator; import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.query.sqm.produce.function.SqmFunctionRegistry; import org.hibernate.query.sqm.produce.function.SqmFunctionRegistry;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverterFactory; import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.hibernate.tuple.entity.EntityTuplizerFactory; import org.hibernate.tuple.entity.EntityTuplizerFactory;
@ -400,7 +400,7 @@ public class AbstractDelegatingSessionFactoryOptions implements SessionFactoryOp
} }
@Override @Override
public SqmToSqlAstConverterFactory getSqmTranslatorFactory() { public SqmTranslatorFactory getSqmTranslatorFactory() {
return delegate.getSqmTranslatorFactory(); return delegate.getSqmTranslatorFactory();
} }

View File

@ -36,7 +36,7 @@ import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.query.hql.HqlTranslator; import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.query.sqm.produce.function.SqmFunctionRegistry; import org.hibernate.query.sqm.produce.function.SqmFunctionRegistry;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverterFactory; import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.hibernate.stat.Statistics; import org.hibernate.stat.Statistics;
@ -146,7 +146,7 @@ public interface SessionFactoryOptions {
HqlTranslator getHqlTranslator(); HqlTranslator getHqlTranslator();
SqmToSqlAstConverterFactory getSqmTranslatorFactory(); SqmTranslatorFactory getSqmTranslatorFactory();
SqmMultiTableMutationStrategy getSqmMultiTableMutationStrategy(); SqmMultiTableMutationStrategy getSqmMultiTableMutationStrategy();

View File

@ -21,7 +21,7 @@ import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.internal.ParameterMetadataImpl; import org.hibernate.query.internal.ParameterMetadataImpl;
import org.hibernate.query.spi.QueryInterpretationCache; import org.hibernate.query.spi.QueryInterpretationCache;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverterFactory; import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager; import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
import org.hibernate.resource.transaction.spi.TransactionCoordinator; import org.hibernate.resource.transaction.spi.TransactionCoordinator;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder; import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
@ -931,7 +931,7 @@ public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings {
String SEMANTIC_QUERY_PRODUCER = "hibernate.query.hql.translator"; String SEMANTIC_QUERY_PRODUCER = "hibernate.query.hql.translator";
/** /**
* Names the {@link SqmToSqlAstConverterFactory} class to use. * Names the {@link SqmTranslatorFactory} class to use.
*/ */
String SEMANTIC_QUERY_TRANSLATOR = "hibernate.query.sqm.translator"; String SEMANTIC_QUERY_TRANSLATOR = "hibernate.query.sqm.translator";

View File

@ -68,6 +68,7 @@ import org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver;
import org.hibernate.engine.jdbc.env.spi.AnsiSqlKeywords; import org.hibernate.engine.jdbc.env.spi.AnsiSqlKeywords;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper; import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder; import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport; import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
import org.hibernate.engine.jdbc.env.spi.SchemaNameResolver; import org.hibernate.engine.jdbc.env.spi.SchemaNameResolver;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
@ -83,7 +84,6 @@ import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.io.StreamCopier; import org.hibernate.internal.util.io.StreamCopier;
import org.hibernate.loader.BatchLoadSizingStrategy; import org.hibernate.loader.BatchLoadSizingStrategy;
@ -100,13 +100,15 @@ import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.spi.QueryEngine; import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverterFactory; import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.ANSICaseFragment; import org.hibernate.sql.ANSICaseFragment;
import org.hibernate.sql.ANSIJoinFragment; import org.hibernate.sql.ANSIJoinFragment;
import org.hibernate.sql.CaseFragment; import org.hibernate.sql.CaseFragment;
import org.hibernate.sql.ForUpdateFragment; import org.hibernate.sql.ForUpdateFragment;
import org.hibernate.sql.JoinFragment; import org.hibernate.sql.JoinFragment;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.spi.StandardSqlAstTranslatorFactory;
import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl; import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl; import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
@ -3097,9 +3099,20 @@ public abstract class Dialect implements ConversionContext {
* precedence as it comes directly from the user config * precedence as it comes directly from the user config
* *
* @see org.hibernate.query.sqm.sql.internal.StandardSqmSelectToSqlAstConverter * @see org.hibernate.query.sqm.sql.internal.StandardSqmSelectToSqlAstConverter
* @see QueryEngine#getSqmTranslator() * @see QueryEngine#getSqmTranslatorFactory()
*/ */
public SqmToSqlAstConverterFactory getSqmTranslatorFactory() { public SqmTranslatorFactory getSqmTranslatorFactory() {
return null;
}
/**
* Return an SqlAstTranslatorFactory specific for the Dialect. Return {@code null}
* to use Hibernate's standard translator.
*
* @see StandardSqlAstTranslatorFactory
* @see JdbcEnvironment#getSqlAstTranslatorFactory()
*/
public SqlAstTranslatorFactory getSqlAstTranslatorFactory() {
return null; return null;
} }
} }

View File

@ -39,6 +39,8 @@ import org.hibernate.exception.internal.SQLStateConversionDelegate;
import org.hibernate.exception.internal.StandardSQLExceptionConverter; import org.hibernate.exception.internal.StandardSQLExceptionConverter;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.spi.StandardSqlAstTranslatorFactory;
import org.hibernate.tool.schema.extract.spi.ExtractionContext; import org.hibernate.tool.schema.extract.spi.ExtractionContext;
import org.hibernate.tool.schema.extract.spi.SequenceInformation; import org.hibernate.tool.schema.extract.spi.SequenceInformation;
@ -52,6 +54,8 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
private final Dialect dialect; private final Dialect dialect;
private final SqlAstTranslatorFactory sqlAstTranslatorFactory;
private final SqlExceptionHelper sqlExceptionHelper; private final SqlExceptionHelper sqlExceptionHelper;
private final ExtractedDatabaseMetaData extractedMetaDataSupport; private final ExtractedDatabaseMetaData extractedMetaDataSupport;
private final Identifier currentCatalog; private final Identifier currentCatalog;
@ -72,6 +76,8 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
public JdbcEnvironmentImpl(final ServiceRegistryImplementor serviceRegistry, Dialect dialect) { public JdbcEnvironmentImpl(final ServiceRegistryImplementor serviceRegistry, Dialect dialect) {
this.dialect = dialect; this.dialect = dialect;
this.sqlAstTranslatorFactory = resolveSqlAstTranslatorFactory( dialect );
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport(); NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport();
@ -119,6 +125,14 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder(); this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder();
} }
private static SqlAstTranslatorFactory resolveSqlAstTranslatorFactory(Dialect dialect) {
if ( dialect.getSqlAstTranslatorFactory() != null ) {
return dialect.getSqlAstTranslatorFactory();
}
return new StandardSqlAstTranslatorFactory();
}
private static boolean logWarnings(ConfigurationService cfgService, Dialect dialect) { private static boolean logWarnings(ConfigurationService cfgService, Dialect dialect) {
return cfgService.getSetting( return cfgService.getSetting(
AvailableSettings.LOG_JDBC_WARNINGS, AvailableSettings.LOG_JDBC_WARNINGS,
@ -159,6 +173,8 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
public JdbcEnvironmentImpl(DatabaseMetaData databaseMetaData, Dialect dialect) throws SQLException { public JdbcEnvironmentImpl(DatabaseMetaData databaseMetaData, Dialect dialect) throws SQLException {
this.dialect = dialect; this.dialect = dialect;
this.sqlAstTranslatorFactory = resolveSqlAstTranslatorFactory( dialect );
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, false ); this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, false );
NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport(); NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport();
@ -232,6 +248,8 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
DatabaseMetaData databaseMetaData) throws SQLException { DatabaseMetaData databaseMetaData) throws SQLException {
this.dialect = dialect; this.dialect = dialect;
this.sqlAstTranslatorFactory = resolveSqlAstTranslatorFactory( dialect );
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService, dialect ) ); this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService, dialect ) );
@ -339,6 +357,11 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
return dialect; return dialect;
} }
@Override
public SqlAstTranslatorFactory getSqlAstTranslatorFactory() {
return sqlAstTranslatorFactory;
}
@Override @Override
public ExtractedDatabaseMetaData getExtractedDatabaseMetaData() { public ExtractedDatabaseMetaData getExtractedDatabaseMetaData() {
return extractedMetaDataSupport; return extractedMetaDataSupport;

View File

@ -11,6 +11,7 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.TypeInfo; import org.hibernate.engine.jdbc.spi.TypeInfo;
import org.hibernate.service.Service; import org.hibernate.service.Service;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
/** /**
* Initial look at this concept we keep talking about with merging information from {@link java.sql.DatabaseMetaData} * Initial look at this concept we keep talking about with merging information from {@link java.sql.DatabaseMetaData}
@ -26,6 +27,8 @@ public interface JdbcEnvironment extends Service {
*/ */
Dialect getDialect(); Dialect getDialect();
SqlAstTranslatorFactory getSqlAstTranslatorFactory();
/** /**
* Access to the bits of information we pulled off the JDBC {@link java.sql.DatabaseMetaData} (that did not get * Access to the bits of information we pulled off the JDBC {@link java.sql.DatabaseMetaData} (that did not get
* "interpreted" into the helpers/delegates available here). * "interpreted" into the helpers/delegates available here).

View File

@ -27,7 +27,7 @@ import org.hibernate.query.hql.spi.SqmCreationOptions;
import org.hibernate.query.internal.QueryInterpretationCacheDisabledImpl; import org.hibernate.query.internal.QueryInterpretationCacheDisabledImpl;
import org.hibernate.query.internal.QueryInterpretationCacheStandardImpl; import org.hibernate.query.internal.QueryInterpretationCacheStandardImpl;
import org.hibernate.query.named.NamedQueryRepository; import org.hibernate.query.named.NamedQueryRepository;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverterFactory; import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.query.sqm.internal.DomainParameterXref; import org.hibernate.query.sqm.internal.DomainParameterXref;
import org.hibernate.query.sqm.internal.SqmCreationOptionsStandard; import org.hibernate.query.sqm.internal.SqmCreationOptionsStandard;
import org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder; import org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder;
@ -63,7 +63,7 @@ public class QueryEngine {
private final NamedQueryRepository namedQueryRepository; private final NamedQueryRepository namedQueryRepository;
private final SqmCriteriaNodeBuilder criteriaBuilder; private final SqmCriteriaNodeBuilder criteriaBuilder;
private final HqlTranslator hqlTranslator; private final HqlTranslator hqlTranslator;
private final SqmToSqlAstConverterFactory sqmToSqlAstConverterFactory; private final SqmTranslatorFactory sqmTranslatorFactory;
private final QueryInterpretationCache interpretationCache; private final QueryInterpretationCache interpretationCache;
private final SqmFunctionRegistry sqmFunctionRegistry; private final SqmFunctionRegistry sqmFunctionRegistry;
@ -88,7 +88,7 @@ public class QueryEngine {
sqmCreationOptions sqmCreationOptions
); );
this.sqmToSqlAstConverterFactory = resolveSqmToSqlAstConverterFactory( this.sqmTranslatorFactory = resolveSqmTranslatorFactory(
runtimeOptions, runtimeOptions,
dialect, dialect,
sqmCreationContext, sqmCreationContext,
@ -126,7 +126,7 @@ public class QueryEngine {
return new StandardHqlTranslator( sqmCreationContext, sqmCreationOptions ); return new StandardHqlTranslator( sqmCreationContext, sqmCreationOptions );
} }
private SqmToSqlAstConverterFactory resolveSqmToSqlAstConverterFactory( private SqmTranslatorFactory resolveSqmTranslatorFactory(
SessionFactoryOptions runtimeOptions, SessionFactoryOptions runtimeOptions,
Dialect dialect, Dialect dialect,
SqmCreationContext sqmCreationContext, SqmCreationContext sqmCreationContext,
@ -140,7 +140,7 @@ public class QueryEngine {
} }
//noinspection Convert2Lambda //noinspection Convert2Lambda
return new SqmToSqlAstConverterFactory() { return new SqmTranslatorFactory() {
@Override @Override
public SqmSelectToSqlAstConverter createSelectConverter( public SqmSelectToSqlAstConverter createSelectConverter(
QueryOptions queryOptions, QueryOptions queryOptions,
@ -214,8 +214,8 @@ public class QueryEngine {
return hqlTranslator; return hqlTranslator;
} }
public SqmToSqlAstConverterFactory getSqmTranslatorFactory() { public SqmTranslatorFactory getSqmTranslatorFactory() {
return sqmToSqlAstConverterFactory; return sqmTranslatorFactory;
} }
public QueryInterpretationCache getInterpretationCache() { public QueryInterpretationCache getInterpretationCache() {

View File

@ -14,22 +14,25 @@ import javax.persistence.TupleElement;
import org.hibernate.NotYetImplementedFor6Exception; import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.ScrollMode; import org.hibernate.ScrollMode;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.streams.StingArrayCollector; import org.hibernate.internal.util.streams.StingArrayCollector;
import org.hibernate.query.IllegalQueryOperationException; import org.hibernate.query.IllegalQueryOperationException;
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.spi.QueryParameterImplementor; import org.hibernate.query.spi.QueryParameterImplementor;
import org.hibernate.query.spi.ScrollableResultsImplementor; import org.hibernate.query.spi.ScrollableResultsImplementor;
import org.hibernate.query.spi.SelectQueryPlan; import org.hibernate.query.spi.SelectQueryPlan;
import org.hibernate.query.sqm.sql.SqmSelectToSqlAstConverter; import org.hibernate.query.sqm.sql.SqmSelectToSqlAstConverter;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverterFactory; import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.query.sqm.sql.internal.SqmSelectInterpretation; import org.hibernate.query.sqm.sql.internal.SqmSelectInterpretation;
import org.hibernate.query.sqm.tree.SqmStatement; import org.hibernate.query.sqm.tree.SqmStatement;
import org.hibernate.query.sqm.tree.expression.SqmParameter; import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement; import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelection; import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.hibernate.sql.ast.spi.SqlAstSelectToJdbcSelectConverter; import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.exec.spi.ExecutionContext; import org.hibernate.sql.exec.spi.ExecutionContext;
import org.hibernate.sql.exec.spi.JdbcParameter; import org.hibernate.sql.exec.spi.JdbcParameter;
import org.hibernate.sql.exec.spi.JdbcParameterBindings; import org.hibernate.sql.exec.spi.JdbcParameterBindings;
@ -157,8 +160,9 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
// - this is similar to the plan for loaders // - this is similar to the plan for loaders
final SessionFactoryImplementor sessionFactory = session.getFactory(); final SessionFactoryImplementor sessionFactory = session.getFactory();
final QueryEngine queryEngine = sessionFactory.getQueryEngine();
final SqmToSqlAstConverterFactory sqmTranslatorFactory = sessionFactory.getQueryEngine().getSqmTranslatorFactory(); final SqmTranslatorFactory sqmTranslatorFactory = queryEngine.getSqmTranslatorFactory();
final SqmSelectToSqlAstConverter sqmConverter = sqmTranslatorFactory.createSelectConverter( final SqmSelectToSqlAstConverter sqmConverter = sqmTranslatorFactory.createSelectConverter(
executionContext.getQueryOptions(), executionContext.getQueryOptions(),
@ -170,11 +174,12 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
final SqmSelectInterpretation interpretation = sqmConverter.interpret( sqm ); final SqmSelectInterpretation interpretation = sqmConverter.interpret( sqm );
// todo (6.0) : allow Dialect to specify SQL -> JdbcCall converter to use final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
jdbcSelect = SqlAstSelectToJdbcSelectConverter.interpret( final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
interpretation.getSqlAst(), final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
sessionFactory
); jdbcSelect = sqlAstTranslatorFactory.buildSelectConverter( sessionFactory )
.interpret( interpretation.getSqlAst() );
this.jdbcParamsXref = SqmUtil.generateJdbcParamsXref( this.jdbcParamsXref = SqmUtil.generateJdbcParamsXref(
domainParameterXref, domainParameterXref,

View File

@ -16,7 +16,7 @@ import org.hibernate.sql.ast.spi.SqlAstCreationContext;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface SqmToSqlAstConverterFactory { public interface SqmTranslatorFactory {
SqmSelectToSqlAstConverter createSelectConverter( SqmSelectToSqlAstConverter createSelectConverter(
QueryOptions queryOptions, QueryOptions queryOptions,
DomainParameterXref domainParameterXref, DomainParameterXref domainParameterXref,

View File

@ -0,0 +1,29 @@
/*
* 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
*/
package org.hibernate.sql.ast;
import org.hibernate.sql.ast.spi.SqlAstToJdbcOperationConverter;
import org.hibernate.sql.ast.spi.SqlSelectAstWalker;
import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.sql.exec.spi.JdbcSelect;
/**
* @author Steve Ebersole
*/
public interface SqlAstSelectTranslator extends SqlSelectAstWalker, SqlAstToJdbcOperationConverter {
/**
* Translate the SelectStatement into the executable JdbcSelect
*/
JdbcSelect interpret(SelectStatement selectStatement);
/**
* Translate the QuerySpec into the executable JdbcSelect
*/
JdbcSelect interpret(QuerySpec querySpec);
}

View File

@ -0,0 +1,18 @@
/*
* 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
*/
package org.hibernate.sql.ast;
import org.hibernate.engine.spi.SessionFactoryImplementor;
/**
* @author Steve Ebersole
*/
public interface SqlAstTranslatorFactory {
SqlAstSelectTranslator buildSelectConverter(SessionFactoryImplementor sessionFactory);
// todo (6.0) : update, delete, etc
}

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.ast.spi;
import java.util.Collections; import java.util.Collections;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.sql.ast.SqlAstSelectTranslator;
import org.hibernate.sql.ast.SqlTreePrinter; import org.hibernate.sql.ast.SqlTreePrinter;
import org.hibernate.sql.ast.tree.SqlAstTreeLogger; import org.hibernate.sql.ast.tree.SqlAstTreeLogger;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
@ -23,49 +24,49 @@ import org.hibernate.sql.results.internal.JdbcValuesMappingProducerStandard;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class SqlAstSelectToJdbcSelectConverter extends AbstractSqlAstToJdbcOperationConverter implements SqlSelectAstWalker { public class StandardSqlAstSelectTranslator
/** extends AbstractSqlAstToJdbcOperationConverter
* Perform interpretation of a SQL AST QuerySpec implements SqlAstSelectTranslator {
*
* @return The interpretation result @SuppressWarnings("WeakerAccess")
*/ public StandardSqlAstSelectTranslator(SessionFactoryImplementor sessionFactory) {
public static JdbcSelect interpret(QuerySpec querySpec, SessionFactoryImplementor sessionFactory) { super( sessionFactory );
final SqlAstSelectToJdbcSelectConverter walker = new SqlAstSelectToJdbcSelectConverter( sessionFactory ); }
walker.visitQuerySpec( querySpec );
@Override
public JdbcSelect interpret(QuerySpec querySpec) {
visitQuerySpec( querySpec );
return new JdbcSelect( return new JdbcSelect(
walker.getSql(), getSql(),
walker.getParameterBinders(), getParameterBinders(),
new JdbcValuesMappingProducerStandard( new JdbcValuesMappingProducerStandard(
querySpec.getSelectClause().getSqlSelections(), querySpec.getSelectClause().getSqlSelections(),
Collections.emptyList() Collections.emptyList()
), ),
walker.getAffectedTableNames() getAffectedTableNames()
); );
} }
public static JdbcSelect interpret(SelectStatement sqlSelectPlan, SessionFactoryImplementor sessionFactory) { @Override
public JdbcSelect interpret(SelectStatement sqlAstSelect) {
if ( SqlAstTreeLogger.DEBUG_ENABLED ) { if ( SqlAstTreeLogger.DEBUG_ENABLED ) {
SqlTreePrinter.print( sqlSelectPlan ); SqlTreePrinter.print( sqlAstSelect );
} }
final SqlAstSelectToJdbcSelectConverter walker = new SqlAstSelectToJdbcSelectConverter( sessionFactory ); visitQuerySpec( sqlAstSelect.getQuerySpec() );
walker.visitSelectQuery( sqlSelectPlan );
return new JdbcSelect( return new JdbcSelect(
walker.getSql(), getSql(),
walker.getParameterBinders(), getParameterBinders(),
new JdbcValuesMappingProducerStandard( new JdbcValuesMappingProducerStandard(
sqlSelectPlan.getQuerySpec().getSelectClause().getSqlSelections(), sqlAstSelect.getQuerySpec().getSelectClause().getSqlSelections(),
sqlSelectPlan.getDomainResultDescriptors() sqlAstSelect.getDomainResultDescriptors()
), ),
sqlSelectPlan.getAffectedTableExpressions() sqlAstSelect.getAffectedTableExpressions()
); );
} }
private SqlAstSelectToJdbcSelectConverter(SessionFactoryImplementor sessionFactory) {
super( sessionFactory );
}
@Override @Override
public void visitSelectQuery(SelectStatement selectQuery) { public void visitSelectQuery(SelectStatement selectQuery) {
visitQuerySpec( selectQuery.getQuerySpec() ); visitQuerySpec( selectQuery.getQuerySpec() );

View File

@ -0,0 +1,21 @@
/*
* 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
*/
package org.hibernate.sql.ast.spi;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.SqlAstSelectTranslator;
/**
* @author Steve Ebersole
*/
public class StandardSqlAstTranslatorFactory implements SqlAstTranslatorFactory {
@Override
public SqlAstSelectTranslator buildSelectConverter(SessionFactoryImplementor sessionFactory) {
return new StandardSqlAstSelectTranslator( sessionFactory);
}
}

View File

@ -21,7 +21,7 @@ import org.hibernate.query.sqm.internal.QuerySqmImpl;
import org.hibernate.query.sqm.sql.internal.SqmSelectInterpretation; import org.hibernate.query.sqm.sql.internal.SqmSelectInterpretation;
import org.hibernate.query.sqm.sql.internal.StandardSqmSelectToSqlAstConverter; import org.hibernate.query.sqm.sql.internal.StandardSqmSelectToSqlAstConverter;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement; import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.sql.ast.spi.SqlAstSelectToJdbcSelectConverter; import org.hibernate.sql.ast.spi.StandardSqlAstSelectTranslator;
import org.hibernate.sql.ast.spi.SqlSelection; import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
@ -111,10 +111,8 @@ public class SmokeTests {
assertThat( sqlSelection.getValuesArrayPosition(), is( 0 ) ); assertThat( sqlSelection.getValuesArrayPosition(), is( 0 ) );
assertThat( sqlSelection.getJdbcValueExtractor(), notNullValue() ); assertThat( sqlSelection.getJdbcValueExtractor(), notNullValue() );
final JdbcSelect jdbcSelectOperation = SqlAstSelectToJdbcSelectConverter.interpret( final JdbcSelect jdbcSelectOperation = new StandardSqlAstSelectTranslator( session.getSessionFactory() )
sqlAst, .interpret( sqlAst );
session.getSessionFactory()
);
assertThat( assertThat(
jdbcSelectOperation.getSql(), jdbcSelectOperation.getSql(),
@ -208,11 +206,8 @@ public class SmokeTests {
assertThat( valueConverter, notNullValue() ); assertThat( valueConverter, notNullValue() );
assertThat( valueConverter, instanceOf( OrdinalEnumValueConverter.class ) ); assertThat( valueConverter, instanceOf( OrdinalEnumValueConverter.class ) );
final JdbcSelect jdbcSelectOperation = new StandardSqlAstSelectTranslator( session.getSessionFactory() )
final JdbcSelect jdbcSelectOperation = SqlAstSelectToJdbcSelectConverter.interpret( .interpret( sqlAst );
sqlAst,
session.getSessionFactory()
);
assertThat( assertThat(
jdbcSelectOperation.getSql(), jdbcSelectOperation.getSql(),