Search in sources :

Example 1 with QueryEngine

use of org.hibernate.query.spi.QueryEngine in project hibernate-orm by hibernate.

the class OrderingExpression method applyCollation.

static Expression applyCollation(Expression expression, String collation, SqlAstCreationState creationState) {
    final Expression sortExpression;
    if (collation == null) {
        sortExpression = expression;
    } else {
        final QueryEngine queryEngine = creationState.getCreationContext().getSessionFactory().getQueryEngine();
        final SqmToSqlAstConverter converter;
        if (creationState instanceof SqmToSqlAstConverter) {
            converter = (SqmToSqlAstConverter) creationState;
        } else {
            converter = new FakeSqmToSqlAstConverter(creationState);
        }
        sortExpression = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("collate").generateSqmExpression(new SqmSelfRenderingExpression<>(walker -> expression, null, null), null, queryEngine, queryEngine.getTypeConfiguration()).convertToSqlAst(converter);
    }
    return sortExpression;
}
Also used : QueryEngine(org.hibernate.query.spi.QueryEngine) SqmSelfRenderingExpression(org.hibernate.query.sqm.tree.expression.SqmSelfRenderingExpression) FakeSqmToSqlAstConverter(org.hibernate.query.sqm.sql.FakeSqmToSqlAstConverter) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SortOrder(org.hibernate.query.sqm.SortOrder) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) NullPrecedence(org.hibernate.query.sqm.NullPrecedence) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FakeSqmToSqlAstConverter(org.hibernate.query.sqm.sql.FakeSqmToSqlAstConverter) SqmSelfRenderingExpression(org.hibernate.query.sqm.tree.expression.SqmSelfRenderingExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) QueryEngine(org.hibernate.query.spi.QueryEngine) FakeSqmToSqlAstConverter(org.hibernate.query.sqm.sql.FakeSqmToSqlAstConverter) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter)

Example 2 with QueryEngine

use of org.hibernate.query.spi.QueryEngine in project hibernate-orm by hibernate.

the class InformixDialectTestCase method init.

@BeforeClass
public static void init() {
    final JpaMetamodelImpl jpaMetamodel = new JpaMetamodelImpl(new TypeConfiguration(), new JpaComplianceStub());
    ssr = new StandardServiceRegistryBuilder().build();
    queryEngine = new QueryEngine(null, null, jpaMetamodel, ValueHandlingMode.BIND, dialect.getPreferredSqlTypeCodeForBoolean(), false, new NamedObjectRepositoryImpl(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()), null, dialect, ssr);
}
Also used : JpaMetamodelImpl(org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) JpaComplianceStub(org.hibernate.orm.test.jpa.JpaComplianceStub) NamedObjectRepositoryImpl(org.hibernate.query.internal.NamedObjectRepositoryImpl) QueryEngine(org.hibernate.query.spi.QueryEngine) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) BeforeClass(org.junit.BeforeClass)

Example 3 with QueryEngine

use of org.hibernate.query.spi.QueryEngine in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method createQuery.

@Override
public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultClass) {
    checkOpen();
    pulseTransactionCoordinator();
    delayedAfterCompletion();
    try {
        final QueryEngine queryEngine = getFactory().getQueryEngine();
        final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
        final QuerySqmImpl<T> query = new QuerySqmImpl<>(queryString, interpretationCache.resolveHqlInterpretation(queryString, s -> queryEngine.getHqlTranslator().translate(queryString)), resultClass, this);
        applyQuerySettingsAndHints(query);
        query.setComment(queryString);
        return query;
    } catch (RuntimeException e) {
        markForRollbackOnly();
        throw getExceptionConverter().convert(e);
    }
}
Also used : QuerySqmImpl(org.hibernate.query.sqm.internal.QuerySqmImpl) NamedCallableQueryMemento(org.hibernate.procedure.spi.NamedCallableQueryMemento) TransactionImplementor(org.hibernate.engine.transaction.spi.TransactionImplementor) EntityPersister(org.hibernate.persister.entity.EntityPersister) MutationQuery(org.hibernate.query.MutationQuery) ExceptionConverter(org.hibernate.engine.spi.ExceptionConverter) NativeQueryTupleTransformer(org.hibernate.jpa.spi.NativeQueryTupleTransformer) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) StandardRandomStrategy(org.hibernate.id.uuid.StandardRandomStrategy) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) ObjectInputStream(java.io.ObjectInputStream) CriteriaDelete(jakarta.persistence.criteria.CriteriaDelete) Interceptor(org.hibernate.Interceptor) CriteriaUpdate(jakarta.persistence.criteria.CriteriaUpdate) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) SqmDmlStatement(org.hibernate.query.sqm.tree.SqmDmlStatement) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) EntityNameResolver(org.hibernate.EntityNameResolver) Transaction(org.hibernate.Transaction) SqmStatement(org.hibernate.query.sqm.tree.SqmStatement) SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) SqmUtil(org.hibernate.query.sqm.internal.SqmUtil) Locale(java.util.Locale) SessionEventListenerManager(org.hibernate.engine.spi.SessionEventListenerManager) Query(org.hibernate.query.Query) SqmUpdateStatement(org.hibernate.query.sqm.tree.update.SqmUpdateStatement) HqlInterpretation(org.hibernate.query.spi.HqlInterpretation) FlushModeTypeHelper(org.hibernate.jpa.internal.util.FlushModeTypeHelper) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) PhysicalConnectionHandlingMode(org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode) TimeZone(java.util.TimeZone) FlushModeType(jakarta.persistence.FlushModeType) StringHelper(org.hibernate.internal.util.StringHelper) UUID(java.util.UUID) Tuple(jakarta.persistence.Tuple) EmptyInterceptor(org.hibernate.EmptyInterceptor) WorkExecutorVisitable(org.hibernate.jdbc.WorkExecutorVisitable) QuerySqmImpl(org.hibernate.query.sqm.internal.QuerySqmImpl) SessionException(org.hibernate.SessionException) CacheMode(org.hibernate.CacheMode) List(java.util.List) NativeQueryImplementor(org.hibernate.query.sql.spi.NativeQueryImplementor) QueryTypeMismatchException(org.hibernate.query.QueryTypeMismatchException) HibernateException(org.hibernate.HibernateException) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) NativeQueryImpl(org.hibernate.query.sql.internal.NativeQueryImpl) StatementInspector(org.hibernate.resource.jdbc.spi.StatementInspector) TRUE(java.lang.Boolean.TRUE) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) SessionEventListenerManagerImpl(org.hibernate.engine.internal.SessionEventListenerManagerImpl) SessionEventListener(org.hibernate.SessionEventListener) IllegalMutationQueryException(org.hibernate.query.IllegalMutationQueryException) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) CacheTransactionSynchronization(org.hibernate.cache.spi.CacheTransactionSynchronization) Function(java.util.function.Function) UnknownEntityTypeException(org.hibernate.UnknownEntityTypeException) SQLException(java.sql.SQLException) SqmSelectionQuery(org.hibernate.query.sqm.SqmSelectionQuery) QueryImplementor(org.hibernate.query.spi.QueryImplementor) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) ObjectOutputStream(java.io.ObjectOutputStream) QueryInterpretationCache(org.hibernate.query.spi.QueryInterpretationCache) NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) ProcedureCall(org.hibernate.procedure.ProcedureCall) LockMode(org.hibernate.LockMode) JdbcCoordinatorImpl(org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl) SqmSelectionQueryImpl(org.hibernate.query.sqm.internal.SqmSelectionQueryImpl) IllegalSelectQueryException(org.hibernate.query.IllegalSelectQueryException) IllegalNamedQueryOptionsException(org.hibernate.query.IllegalNamedQueryOptionsException) IOException(java.io.IOException) FlushMode(org.hibernate.FlushMode) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) EntityKey(org.hibernate.engine.spi.EntityKey) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) Work(org.hibernate.jdbc.Work) SqmQueryGroup(org.hibernate.query.sqm.tree.select.SqmQueryGroup) QueryEngine(org.hibernate.query.spi.QueryEngine) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) ProcedureCallImpl(org.hibernate.procedure.internal.ProcedureCallImpl) LobCreator(org.hibernate.engine.jdbc.LobCreator) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) ReturningWork(org.hibernate.jdbc.ReturningWork) SelectionQuery(org.hibernate.query.SelectionQuery) QueryInterpretationCache(org.hibernate.query.spi.QueryInterpretationCache) QueryEngine(org.hibernate.query.spi.QueryEngine)

Example 4 with QueryEngine

use of org.hibernate.query.spi.QueryEngine in project hibernate-orm by hibernate.

the class ConcreteSqmSelectQueryPlan method buildCacheableSqmInterpretation.

private static CacheableSqmInterpretation buildCacheableSqmInterpretation(SqmSelectStatement<?> sqm, DomainParameterXref domainParameterXref, DomainQueryExecutionContext executionContext) {
    final SharedSessionContractImplementor session = executionContext.getSession();
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    final QueryEngine queryEngine = sessionFactory.getQueryEngine();
    final SqmTranslatorFactory sqmTranslatorFactory = queryEngine.getSqmTranslatorFactory();
    final SqmTranslator<SelectStatement> sqmConverter = sqmTranslatorFactory.createSelectTranslator(sqm, executionContext.getQueryOptions(), domainParameterXref, executionContext.getQueryParameterBindings(), executionContext.getSession().getLoadQueryInfluencers(), sessionFactory, true);
    // tableGroupAccess = sqmConverter.getFromClauseAccess();
    final SqmTranslation<SelectStatement> sqmInterpretation = sqmConverter.translate();
    final FromClauseAccess tableGroupAccess = sqmConverter.getFromClauseAccess();
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final SqlAstTranslator<JdbcSelect> selectTranslator = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqmInterpretation.getSqlAst());
    final Map<QueryParameterImplementor<?>, Map<SqmParameter<?>, List<List<JdbcParameter>>>> jdbcParamsXref = SqmUtil.generateJdbcParamsXref(domainParameterXref, sqmInterpretation::getJdbcParamsBySqmParam);
    final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, jdbcParamsXref, session.getFactory().getRuntimeMetamodels().getMappingMetamodel(), tableGroupAccess::findTableGroup, new SqmParameterMappingModelResolutionAccess() {

        @Override
        @SuppressWarnings("unchecked")
        public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T> parameter) {
            return (MappingModelExpressible<T>) sqmInterpretation.getSqmParameterMappingModelTypeResolutions().get(parameter);
        }
    }, session);
    final JdbcSelect jdbcSelect = selectTranslator.translate(jdbcParameterBindings, executionContext.getQueryOptions());
    return new CacheableSqmInterpretation(sqmInterpretation.getSqlAst(), jdbcSelect, tableGroupAccess, jdbcParamsXref, sqmInterpretation.getSqmParameterMappingModelTypeResolutions(), jdbcParameterBindings);
}
Also used : MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) QueryEngine(org.hibernate.query.spi.QueryEngine) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) ArrayList(java.util.ArrayList) List(java.util.List) SqmTranslatorFactory(org.hibernate.query.sqm.sql.SqmTranslatorFactory) JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) SqmParameterMappingModelResolutionAccess(org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) QueryParameterImplementor(org.hibernate.query.spi.QueryParameterImplementor) Map(java.util.Map) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 5 with QueryEngine

use of org.hibernate.query.spi.QueryEngine in project hibernate-orm by hibernate.

the class SimpleDeleteQueryPlan method createDeleteTranslator.

private SqlAstTranslator<JdbcDelete> createDeleteTranslator(DomainQueryExecutionContext executionContext) {
    final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
    final QueryEngine queryEngine = factory.getQueryEngine();
    final SqmTranslatorFactory translatorFactory = queryEngine.getSqmTranslatorFactory();
    final SqmTranslator<DeleteStatement> translator = translatorFactory.createSimpleDeleteTranslator(sqmDelete, executionContext.getQueryOptions(), domainParameterXref, executionContext.getQueryParameterBindings(), executionContext.getSession().getLoadQueryInfluencers(), factory);
    sqmInterpretation = translator.translate();
    this.jdbcParamsXref = SqmUtil.generateJdbcParamsXref(domainParameterXref, sqmInterpretation::getJdbcParamsBySqmParam);
    return factory.getJdbcServices().getJdbcEnvironment().getSqlAstTranslatorFactory().buildDeleteTranslator(factory, sqmInterpretation.getSqlAst());
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryEngine(org.hibernate.query.spi.QueryEngine) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) DeleteStatement(org.hibernate.sql.ast.tree.delete.DeleteStatement) SqmTranslatorFactory(org.hibernate.query.sqm.sql.SqmTranslatorFactory)

Aggregations

QueryEngine (org.hibernate.query.spi.QueryEngine)13 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)7 SqmTranslatorFactory (org.hibernate.query.sqm.sql.SqmTranslatorFactory)6 SqmSelectStatement (org.hibernate.query.sqm.tree.select.SqmSelectStatement)5 List (java.util.List)2 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)2 NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)2 QueryInterpretationCache (org.hibernate.query.spi.QueryInterpretationCache)2 SqmDeleteStatement (org.hibernate.query.sqm.tree.delete.SqmDeleteStatement)2 SqmUpdateStatement (org.hibernate.query.sqm.tree.update.SqmUpdateStatement)2 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)2 FlushModeType (jakarta.persistence.FlushModeType)1 NamedNativeQuery (jakarta.persistence.NamedNativeQuery)1 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)1 Tuple (jakarta.persistence.Tuple)1 CriteriaDelete (jakarta.persistence.criteria.CriteriaDelete)1 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 CriteriaUpdate (jakarta.persistence.criteria.CriteriaUpdate)1