Search in sources :

Example 1 with SqmListJoin

use of org.hibernate.query.sqm.tree.domain.SqmListJoin in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitMapKeyNavigablePath.

@Override
public SqmPath<?> visitMapKeyNavigablePath(HqlParser.MapKeyNavigablePathContext 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.KEY) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.KEY_FUNCTION_ON_NON_MAP);
        }
    }
    SqmPath<?> result;
    if (sqmPath instanceof SqmMapJoin) {
        final SqmMapJoin<?, ?, ?> sqmMapJoin = (SqmMapJoin<?, ?, ?>) sqmPath;
        if (consumer instanceof QualifiedJoinPathConsumer) {
            if (madeNested && !hasContinuation) {
                // Reset the nested state before consuming the terminal identifier
                ((QualifiedJoinPathConsumer) consumer).setNested(false);
            }
            consumer.consumeIdentifier(CollectionPart.Nature.INDEX.getName(), false, !hasContinuation);
            result = (SqmPath<?>) consumer.getConsumedPart();
        } else {
            result = sqmMapJoin.key();
        }
    } else if (sqmPath instanceof SqmListJoin) {
        if (hasContinuation) {
            throw new SemanticException("list index may not be dereferenced");
        }
        SqmListJoin<?, ?> listJoin = (SqmListJoin<?, ?>) sqmPath;
        result = listJoin.resolvePathPart(CollectionPart.Nature.INDEX.getName(), true, this);
    } else {
        assert sqmPath instanceof SqmPluralValuedSimplePath;
        final SqmPluralValuedSimplePath<?> mapPath = (SqmPluralValuedSimplePath<?>) sqmPath;
        result = mapPath.resolvePathPart(CollectionPart.Nature.INDEX.getName(), !hasContinuation, 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) SqmListJoin(org.hibernate.query.sqm.tree.domain.SqmListJoin) SqmMapJoin(org.hibernate.query.sqm.tree.domain.SqmMapJoin) SqmPluralValuedSimplePath(org.hibernate.query.sqm.tree.domain.SqmPluralValuedSimplePath) 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) SemanticException(org.hibernate.query.SemanticException)

Aggregations

TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 PluralPersistentAttribute (org.hibernate.metamodel.model.domain.PluralPersistentAttribute)1 SemanticException (org.hibernate.query.SemanticException)1 DotIdentifierConsumer (org.hibernate.query.hql.spi.DotIdentifierConsumer)1 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)1 SqmListJoin (org.hibernate.query.sqm.tree.domain.SqmListJoin)1 SqmMapJoin (org.hibernate.query.sqm.tree.domain.SqmMapJoin)1 SqmPluralValuedSimplePath (org.hibernate.query.sqm.tree.domain.SqmPluralValuedSimplePath)1