Search in sources :

Example 1 with SqmInListPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmInListPredicate in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitInListPredicate.

@Override
public Predicate visitInListPredicate(SqmInListPredicate<?> predicate) {
    // handling for "expansion"
    if (predicate.getListExpressions().size() == 1) {
        final SqmExpression<?> sqmExpression = predicate.getListExpressions().get(0);
        if (sqmExpression instanceof SqmParameter) {
            final SqmParameter<?> sqmParameter = (SqmParameter<?>) sqmExpression;
            if (sqmParameter.allowMultiValuedBinding()) {
                final Predicate specialCase = processInListWithSingleParameter(predicate, sqmParameter);
                if (specialCase != null) {
                    return specialCase;
                }
            }
        }
    }
    // otherwise - no special case...
    final FromClauseIndex fromClauseIndex = fromClauseIndexStack.getCurrent();
    inferrableTypeAccessStack.push(() -> {
        for (SqmExpression<?> listExpression : predicate.getListExpressions()) {
            final MappingModelExpressible<?> mapping = determineValueMapping(listExpression, fromClauseIndex);
            if (mapping != null) {
                return mapping;
            }
        }
        return null;
    });
    final Expression testExpression;
    try {
        testExpression = (Expression) predicate.getTestExpression().accept(this);
    } finally {
        inferrableTypeAccessStack.pop();
    }
    final InListPredicate inPredicate = new InListPredicate(testExpression, predicate.isNegated(), getBooleanType());
    inferrableTypeAccessStack.push(() -> determineValueMapping(predicate.getTestExpression(), fromClauseIndex));
    try {
        for (SqmExpression<?> expression : predicate.getListExpressions()) {
            inPredicate.addExpression((Expression) expression.accept(this));
        }
    } finally {
        inferrableTypeAccessStack.pop();
    }
    return inPredicate;
}
Also used : BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SqmBooleanExpressionPredicate(org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) SqmNegatedPredicate(org.hibernate.query.sqm.tree.predicate.SqmNegatedPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) SqmAndPredicate(org.hibernate.query.sqm.tree.predicate.SqmAndPredicate) SqmMemberOfPredicate(org.hibernate.query.sqm.tree.predicate.SqmMemberOfPredicate) SqmLikePredicate(org.hibernate.query.sqm.tree.predicate.SqmLikePredicate) SqmExistsPredicate(org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate) SqmOrPredicate(org.hibernate.query.sqm.tree.predicate.SqmOrPredicate) SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmGroupedPredicate(org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) SqmBetweenPredicate(org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) SqmEmptinessPredicate(org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 2 with SqmInListPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmInListPredicate in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitInPredicate.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public SqmPredicate visitInPredicate(HqlParser.InPredicateContext ctx) {
    final boolean negated = ctx.getChildCount() == 4;
    final SqmExpression<?> testExpression = (SqmExpression<?>) ctx.getChild(0).accept(this);
    final HqlParser.InListContext inListContext = (HqlParser.InListContext) ctx.getChild(ctx.getChildCount() - 1);
    if (inListContext instanceof HqlParser.ExplicitTupleInListContext) {
        final HqlParser.ExplicitTupleInListContext tupleExpressionListContext = (HqlParser.ExplicitTupleInListContext) inListContext;
        final int size = tupleExpressionListContext.getChildCount();
        final int estimatedSize = size >> 1;
        final Class<?> testExpressionJavaType = testExpression.getJavaType();
        final boolean isEnum = testExpressionJavaType != null && testExpressionJavaType.isEnum();
        // Multi-valued bindings are only allowed if there is a single list item, hence size 3 (LP, RP and param)
        parameterDeclarationContextStack.push(() -> size == 3);
        try {
            final List<SqmExpression<?>> listExpressions = new ArrayList<>(estimatedSize);
            for (int i = 1; i < size; i++) {
                final ParseTree parseTree = tupleExpressionListContext.getChild(i);
                if (parseTree instanceof HqlParser.ExpressionOrPredicateContext) {
                    final ParseTree child = parseTree.getChild(0);
                    final HqlParser.ExpressionContext expressionContext;
                    final Map<Class<?>, Enum<?>> possibleEnumValues;
                    if (isEnum && child instanceof HqlParser.ExpressionContext && (possibleEnumValues = getPossibleEnumValues(expressionContext = (HqlParser.ExpressionContext) child)) != null) {
                        listExpressions.add(resolveEnumShorthandLiteral(expressionContext, possibleEnumValues, testExpressionJavaType));
                    } else {
                        listExpressions.add((SqmExpression<?>) child.accept(this));
                    }
                }
            }
            return new SqmInListPredicate(testExpression, listExpressions, negated, creationContext.getNodeBuilder());
        } finally {
            parameterDeclarationContextStack.pop();
        }
    } else if (inListContext instanceof HqlParser.ParamInListContext) {
        final HqlParser.ParamInListContext tupleExpressionListContext = (HqlParser.ParamInListContext) inListContext;
        parameterDeclarationContextStack.push(() -> true);
        try {
            return new SqmInListPredicate(testExpression, Collections.singletonList(tupleExpressionListContext.getChild(0).accept(this)), negated, creationContext.getNodeBuilder());
        } finally {
            parameterDeclarationContextStack.pop();
        }
    } else if (inListContext instanceof HqlParser.SubqueryInListContext) {
        final HqlParser.SubqueryInListContext subQueryOrParamInListContext = (HqlParser.SubqueryInListContext) inListContext;
        return new SqmInSubQueryPredicate(testExpression, visitSubquery((HqlParser.SubqueryContext) subQueryOrParamInListContext.getChild(1)), negated, creationContext.getNodeBuilder());
    } else if (inListContext instanceof HqlParser.PersistentCollectionReferenceInListContext) {
        if (getCreationOptions().useStrictJpaCompliance()) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.HQL_COLLECTION_FUNCTION);
        }
        final HqlParser.PersistentCollectionReferenceInListContext collectionReferenceInListContext = (HqlParser.PersistentCollectionReferenceInListContext) inListContext;
        return new SqmInSubQueryPredicate<>(testExpression, createCollectionReferenceSubQuery((HqlParser.SimplePathContext) collectionReferenceInListContext.getChild(2), (TerminalNode) collectionReferenceInListContext.getChild(0)), negated, creationContext.getNodeBuilder());
    } else {
        throw new ParsingException("Unexpected IN predicate type [" + ctx.getClass().getSimpleName() + "] : " + ctx.getText());
    }
}
Also used : ArrayList(java.util.ArrayList) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) ParsingException(org.hibernate.query.sqm.ParsingException) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) HqlParser(org.hibernate.grammars.hql.HqlParser) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with SqmInListPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmInListPredicate in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method processInSingleParameter.

private Predicate processInSingleParameter(SqmInListPredicate<?> sqmPredicate, SqmParameter<?> sqmParameter, QueryParameterImplementor<?> domainParam, QueryParameterBinding<?> domainParamBinding) {
    final Iterator<?> iterator = domainParamBinding.getBindValues().iterator();
    final InListPredicate inListPredicate = new InListPredicate((Expression) sqmPredicate.getTestExpression().accept(this), sqmPredicate.isNegated(), getBooleanType());
    if (!iterator.hasNext()) {
        return inListPredicate;
    }
    final FromClauseIndex fromClauseIndex = fromClauseIndexStack.getCurrent();
    inferrableTypeAccessStack.push(() -> determineValueMapping(sqmPredicate.getTestExpression(), fromClauseIndex));
    try {
        inListPredicate.addExpression(consumeSingleSqmParameter(sqmParameter));
        iterator.next();
        while (iterator.hasNext()) {
            iterator.next();
            // for each bind value create an "expansion"
            final SqmParameter<?> sqmParamToConsume = sqmParameter.copy();
            domainParameterXref.addExpansion(domainParam, sqmParameter, sqmParamToConsume);
            inListPredicate.addExpression(consumeSingleSqmParameter(sqmParamToConsume));
        }
        return inListPredicate;
    } finally {
        inferrableTypeAccessStack.pop();
    }
}
Also used : SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate)

Aggregations

SqmInListPredicate (org.hibernate.query.sqm.tree.predicate.SqmInListPredicate)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 SqmInSubQueryPredicate (org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate)2 InListPredicate (org.hibernate.sql.ast.tree.predicate.InListPredicate)2 ArrayList (java.util.ArrayList)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 ParsingException (org.hibernate.query.sqm.ParsingException)1 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)1 SqmParameter (org.hibernate.query.sqm.tree.expression.SqmParameter)1 SqmAndPredicate (org.hibernate.query.sqm.tree.predicate.SqmAndPredicate)1 SqmBetweenPredicate (org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate)1 SqmBooleanExpressionPredicate (org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate)1 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)1 SqmEmptinessPredicate (org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate)1 SqmExistsPredicate (org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate)1 SqmGroupedPredicate (org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate)1