Search in sources :

Example 1 with StrictJpaComplianceViolation

use of org.hibernate.query.sqm.StrictJpaComplianceViolation in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitQueryOrder.

protected void visitQueryOrder(SqmQueryPart<?> sqmQueryPart, HqlParser.QueryOrderContext ctx) {
    if (ctx == null) {
        return;
    }
    final SqmOrderByClause orderByClause;
    final HqlParser.OrderByClauseContext orderByClauseContext = (HqlParser.OrderByClauseContext) ctx.getChild(0);
    if (orderByClauseContext != null) {
        if (creationOptions.useStrictJpaCompliance() && processingStateStack.depth() > 1) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.SUBQUERY_ORDER_BY);
        }
        orderByClause = visitOrderByClause(orderByClauseContext);
        sqmQueryPart.setOrderByClause(orderByClause);
    } else {
        orderByClause = null;
    }
    int currentIndex = 1;
    final HqlParser.LimitClauseContext limitClauseContext;
    if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.LimitClauseContext) {
        limitClauseContext = (HqlParser.LimitClauseContext) ctx.getChild(currentIndex++);
    } else {
        limitClauseContext = null;
    }
    final HqlParser.OffsetClauseContext offsetClauseContext;
    if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.OffsetClauseContext) {
        offsetClauseContext = (HqlParser.OffsetClauseContext) ctx.getChild(currentIndex++);
    } else {
        offsetClauseContext = null;
    }
    final HqlParser.FetchClauseContext fetchClauseContext;
    if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.FetchClauseContext) {
        fetchClauseContext = (HqlParser.FetchClauseContext) ctx.getChild(currentIndex++);
    } else {
        fetchClauseContext = null;
    }
    if (currentIndex != 1) {
        if (getCreationOptions().useStrictJpaCompliance()) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.LIMIT_OFFSET_CLAUSE);
        }
        if (processingStateStack.depth() > 1 && orderByClause == null) {
            throw new SemanticException("limit, offset and fetch clause require an order-by clause when used in sub-query");
        }
        sqmQueryPart.setOffsetExpression(visitOffsetClause(offsetClauseContext));
        if (limitClauseContext == null) {
            sqmQueryPart.setFetchExpression(visitFetchClause(fetchClauseContext), visitFetchClauseType(fetchClauseContext));
        } else if (fetchClauseContext == null) {
            sqmQueryPart.setFetchExpression(visitLimitClause(limitClauseContext));
        } else {
            throw new SemanticException("Can't use both limit and fetch clause");
        }
    }
}
Also used : HqlParser(org.hibernate.grammars.hql.HqlParser) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) SemanticException(org.hibernate.query.SemanticException)

Example 2 with StrictJpaComplianceViolation

use of org.hibernate.query.sqm.StrictJpaComplianceViolation in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitSetQueryGroup.

@Override
public SqmQueryGroup<Object> visitSetQueryGroup(HqlParser.SetQueryGroupContext ctx) {
    if (creationOptions.useStrictJpaCompliance()) {
        throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.SET_OPERATIONS);
    }
    final List<ParseTree> children = ctx.children;
    // noinspection unchecked
    final SqmQueryPart<Object> firstQueryPart = (SqmQueryPart<Object>) children.get(0).accept(this);
    SqmQueryGroup<Object> queryGroup;
    if (firstQueryPart instanceof SqmQueryGroup<?>) {
        queryGroup = (SqmQueryGroup<Object>) firstQueryPart;
    } else {
        queryGroup = new SqmQueryGroup<>(firstQueryPart);
    }
    setCurrentQueryPart(queryGroup);
    final int size = children.size();
    final SqmCreationProcessingState firstProcessingState = processingStateStack.pop();
    for (int i = 1; i < size; i += 2) {
        final SetOperator operator = visitSetOperator((HqlParser.SetOperatorContext) children.get(i));
        final HqlParser.OrderedQueryContext simpleQueryCtx = (HqlParser.OrderedQueryContext) children.get(i + 1);
        final List<SqmQueryPart<Object>> queryParts;
        if (queryGroup.getSetOperator() == null || queryGroup.getSetOperator() == operator) {
            queryGroup.setSetOperator(operator);
            queryParts = queryGroup.queryParts();
        } else {
            queryParts = new ArrayList<>(size - (i >> 1));
            queryParts.add(queryGroup);
            queryGroup = new SqmQueryGroup<>(creationContext.getNodeBuilder(), operator, queryParts);
            setCurrentQueryPart(queryGroup);
        }
        final SqmQueryPart<Object> queryPart;
        try {
            processingStateStack.push(new SqmQuerySpecCreationProcessingStateStandardImpl(processingStateStack.getCurrent(), (SqmSelectQuery<?>) firstProcessingState.getProcessingQuery(), this));
            final List<ParseTree> subChildren = simpleQueryCtx.children;
            if (subChildren.get(0) instanceof HqlParser.QueryContext) {
                final SqmQuerySpec<Object> querySpec = new SqmQuerySpec<>(creationContext.getNodeBuilder());
                queryParts.add(querySpec);
                visitQuerySpecExpression((HqlParser.QuerySpecExpressionContext) simpleQueryCtx);
            } else {
                try {
                    final SqmSelectStatement<Object> selectStatement = new SqmSelectStatement<>(creationContext.getNodeBuilder());
                    processingStateStack.push(new SqmQuerySpecCreationProcessingStateStandardImpl(processingStateStack.getCurrent(), selectStatement, this));
                    queryPart = visitNestedQueryExpression((HqlParser.NestedQueryExpressionContext) simpleQueryCtx);
                    queryParts.add(queryPart);
                } finally {
                    processingStateStack.pop();
                }
            }
        } finally {
            processingStateStack.pop();
        }
    }
    processingStateStack.push(firstProcessingState);
    return queryGroup;
}
Also used : SqmCreationProcessingState(org.hibernate.query.hql.spi.SqmCreationProcessingState) SqmQueryGroup(org.hibernate.query.sqm.tree.select.SqmQueryGroup) SqmQuerySpecCreationProcessingStateStandardImpl(org.hibernate.query.sqm.internal.SqmQuerySpecCreationProcessingStateStandardImpl) SetOperator(org.hibernate.query.sqm.SetOperator) SqmSelectQuery(org.hibernate.query.sqm.tree.select.SqmSelectQuery) AbstractSqmSelectQuery(org.hibernate.query.sqm.tree.select.AbstractSqmSelectQuery) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SqmQueryPart(org.hibernate.query.sqm.tree.select.SqmQueryPart) HqlParser(org.hibernate.grammars.hql.HqlParser) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with StrictJpaComplianceViolation

use of org.hibernate.query.sqm.StrictJpaComplianceViolation in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitCollectionFunctionMisuse.

@Override
public SqmPath<?> visitCollectionFunctionMisuse(HqlParser.CollectionFunctionMisuseContext ctx) {
    if (getCreationOptions().useStrictJpaCompliance()) {
        throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.HQL_COLLECTION_FUNCTION);
    }
    final SqmPath<?> pluralAttributePath = consumeDomainPath((HqlParser.PathContext) ctx.getChild(2));
    final SqmPathSource<?> referencedPathSource = pluralAttributePath.getReferencedPathSource();
    final TerminalNode firstNode = (TerminalNode) ctx.getChild(0);
    if (!(referencedPathSource instanceof PluralPersistentAttribute<?, ?, ?>)) {
        throw new PathException(String.format("Argument of '%s' is not a plural path '%s'", firstNode.getSymbol().getText(), pluralAttributePath.getNavigablePath()));
    }
    CollectionPart.Nature nature;
    switch(firstNode.getSymbol().getType()) {
        case ELEMENTS:
            nature = CollectionPart.Nature.ELEMENT;
            break;
        case INDICES:
            nature = CollectionPart.Nature.INDEX;
            break;
        default:
            throw new ParsingException("Impossible symbol");
    }
    return pluralAttributePath.resolvePathPart(nature.getName(), true, this);
}
Also used : PathException(org.hibernate.query.PathException) PluralPersistentAttribute(org.hibernate.metamodel.model.domain.PluralPersistentAttribute) HqlParser(org.hibernate.grammars.hql.HqlParser) ParsingException(org.hibernate.query.sqm.ParsingException) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart)

Example 4 with StrictJpaComplianceViolation

use of org.hibernate.query.sqm.StrictJpaComplianceViolation in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitCollateFunction.

@Override
public Object visitCollateFunction(HqlParser.CollateFunctionContext ctx) {
    if (creationOptions.useStrictJpaCompliance()) {
        throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.COLLATIONS);
    }
    final SqmExpression<?> expressionToCollate = (SqmExpression<?>) ctx.getChild(2).accept(this);
    final SqmCollation castTargetExpression = (SqmCollation) ctx.getChild(4).accept(this);
    return getFunctionDescriptor("collate").generateSqmExpression(asList(expressionToCollate, castTargetExpression), // why not string?
    null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
}
Also used : SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) SqmCollation(org.hibernate.query.sqm.tree.expression.SqmCollation)

Example 5 with StrictJpaComplianceViolation

use of org.hibernate.query.sqm.StrictJpaComplianceViolation in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitCollectionValueNavigablePath.

@Override
public SqmPath<?> visitCollectionValueNavigablePath(HqlParser.CollectionValueNavigablePathContext ctx) {
    final DotIdentifierConsumer consumer = dotIdentifierConsumerStack.getCurrent();
    final boolean madeNested;
    if (consumer instanceof QualifiedJoinPathConsumer) {
        final QualifiedJoinPathConsumer qualifiedJoinPathConsumer = (QualifiedJoinPathConsumer) consumer;
        madeNested = !qualifiedJoinPathConsumer.isNested();
        if (madeNested) {
            qualifiedJoinPathConsumer.setNested(true);
        }
    } else {
        madeNested = false;
    }
    final SqmPath<?> sqmPath = consumeDomainPath((HqlParser.PathContext) ctx.getChild(2));
    final boolean hasContinuation = ctx.getChildCount() == 5;
    final SqmPathSource<?> referencedPathSource = sqmPath.getReferencedPathSource();
    final TerminalNode firstNode = (TerminalNode) ctx.getChild(0);
    checkPluralPath(sqmPath, referencedPathSource, firstNode);
    if (getCreationOptions().useStrictJpaCompliance()) {
        final PluralPersistentAttribute<?, ?, ?> attribute = (PluralPersistentAttribute<?, ?, ?>) referencedPathSource;
        if (attribute.getCollectionClassification() != CollectionClassification.MAP && firstNode.getSymbol().getType() == HqlParser.VALUE) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.VALUE_FUNCTION_ON_NON_MAP);
        }
    }
    SqmPath<?> result;
    if (consumer instanceof QualifiedJoinPathConsumer) {
        if (madeNested && !hasContinuation) {
            // Reset the nested state before consuming the terminal identifier
            ((QualifiedJoinPathConsumer) consumer).setNested(false);
        }
        consumer.consumeIdentifier(CollectionPart.Nature.ELEMENT.getName(), false, !hasContinuation);
        result = (SqmPath<?>) consumer.getConsumedPart();
    } else {
        result = sqmPath.resolvePathPart(CollectionPart.Nature.ELEMENT.getName(), true, this);
    }
    if (hasContinuation) {
        if (madeNested) {
            // Reset the nested state before consuming the terminal identifier
            ((QualifiedJoinPathConsumer) consumer).setNested(false);
        }
        final HqlParser.SimplePathContext identCtx = (HqlParser.SimplePathContext) ctx.getChild(4).getChild(1);
        if (consumer instanceof QualifiedJoinPathConsumer) {
            result = consumeDomainPath(identCtx);
        } else {
            dotIdentifierConsumerStack.push(new BasicDotIdentifierConsumer(result, this) {

                @Override
                protected void reset() {
                }
            });
            try {
                result = consumeDomainPath(identCtx);
            } finally {
                dotIdentifierConsumerStack.pop();
            }
        }
    }
    return result;
}
Also used : PluralPersistentAttribute(org.hibernate.metamodel.model.domain.PluralPersistentAttribute) HqlParser(org.hibernate.grammars.hql.HqlParser) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) DotIdentifierConsumer(org.hibernate.query.hql.spi.DotIdentifierConsumer) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Aggregations

StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)15 HqlParser (org.hibernate.grammars.hql.HqlParser)11 ParseTree (org.antlr.v4.runtime.tree.ParseTree)7 SemanticException (org.hibernate.query.SemanticException)7 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)5 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)5 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)4 PluralPersistentAttribute (org.hibernate.metamodel.model.domain.PluralPersistentAttribute)3 DotIdentifierConsumer (org.hibernate.query.hql.spi.DotIdentifierConsumer)3 ParsingException (org.hibernate.query.sqm.ParsingException)3 SqmOrderByClause (org.hibernate.query.sqm.tree.select.SqmOrderByClause)3 ArrayList (java.util.ArrayList)2 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)2 SqmCreationProcessingState (org.hibernate.query.hql.spi.SqmCreationProcessingState)2 NamedSqmFunctionDescriptor (org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor)2 SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)2 SqmTypedNode (org.hibernate.query.sqm.tree.SqmTypedNode)2 SqmQuerySpec (org.hibernate.query.sqm.tree.select.SqmQuerySpec)2 SqmSubQuery (org.hibernate.query.sqm.tree.select.SqmSubQuery)2 OnOverflowClauseContext (org.hibernate.grammars.hql.HqlParser.OnOverflowClauseContext)1