Search in sources :

Example 1 with DotIdentifierConsumer

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

the class SemanticQueryBuilder method resolveOrderByOrGroupByExpression.

private SqmExpression<?> resolveOrderByOrGroupByExpression(ParseTree child, boolean definedCollate) {
    if (child instanceof TerminalNode) {
        if (definedCollate) {
            // This is syntactically disallowed
            throw new ParsingException("COLLATE is not allowed for position based order-by or group-by items");
        }
        final int position = Integer.parseInt(child.getText());
        // make sure this selection exists
        final SqmAliasedNode<?> nodeByPosition = getCurrentProcessingState().getPathRegistry().findAliasedNodeByPosition(position);
        if (nodeByPosition == null) {
            throw new ParsingException("Numeric literal '" + position + "' used in group-by does not match a registered select-item");
        }
        return new SqmAliasedNodeRef(position, integerDomainType, creationContext.getNodeBuilder());
    } else if (child instanceof HqlParser.IdentifierContext) {
        final String identifierText = visitIdentifier((HqlParser.IdentifierContext) child);
        final Integer correspondingPosition = getCurrentProcessingState().getPathRegistry().findAliasedNodePosition(identifierText);
        if (correspondingPosition != null) {
            if (definedCollate) {
                // This is syntactically disallowed
                throw new ParsingException("COLLATE is not allowed for alias based order-by or group-by items");
            }
            return new SqmAliasedNodeRef(correspondingPosition, integerDomainType, creationContext.getNodeBuilder());
        }
        final SqmFrom<?, ?> sqmFrom = getCurrentProcessingState().getPathRegistry().findFromByAlias(identifierText, true);
        if (sqmFrom != null) {
            if (definedCollate) {
                // This is syntactically disallowed
                throw new ParsingException("COLLATE is not allowed for alias based order-by or group-by items");
            }
            // this will group-by all of the sub-parts in the from-element's model part
            return sqmFrom;
        }
        final DotIdentifierConsumer dotIdentifierConsumer = dotIdentifierConsumerStack.getCurrent();
        dotIdentifierConsumer.consumeIdentifier(identifierText, true, true);
        return (SqmExpression<?>) dotIdentifierConsumer.getConsumedPart();
    }
    return (SqmExpression<?>) child.accept(this);
}
Also used : SqmAliasedNodeRef(org.hibernate.query.sqm.tree.expression.SqmAliasedNodeRef) SqmFrom(org.hibernate.query.sqm.tree.from.SqmFrom) AbstractSqmFrom(org.hibernate.query.sqm.tree.domain.AbstractSqmFrom) BigInteger(java.math.BigInteger) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) ParsingException(org.hibernate.query.sqm.ParsingException) HqlParser(org.hibernate.grammars.hql.HqlParser) DotIdentifierConsumer(org.hibernate.query.hql.spi.DotIdentifierConsumer) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 2 with DotIdentifierConsumer

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

the class SemanticQueryBuilder method visitTreatedNavigablePath.

@Override
public SqmPath<?> visitTreatedNavigablePath(HqlParser.TreatedNavigablePathContext 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;
    }
    consumeManagedTypeReference((HqlParser.PathContext) ctx.getChild(2));
    final String treatTargetName = ctx.getChild(4).getText();
    final String treatTargetEntityName = getCreationContext().getJpaMetamodel().qualifyImportableName(treatTargetName);
    final boolean hasContinuation = ctx.getChildCount() == 7;
    consumer.consumeTreat(treatTargetEntityName, !hasContinuation);
    SqmPath<?> result = (SqmPath<?>) consumer.getConsumedPart();
    if (hasContinuation) {
        if (madeNested) {
            // Reset the nested state before consuming the terminal identifier
            ((QualifiedJoinPathConsumer) consumer).setNested(false);
        }
        final boolean addConsumer = !(consumer instanceof QualifiedJoinPathConsumer);
        if (addConsumer) {
            dotIdentifierConsumerStack.push(new BasicDotIdentifierConsumer(result, this) {

                @Override
                protected void reset() {
                }
            });
        }
        try {
            result = consumeDomainPath((HqlParser.SimplePathContext) ctx.getChild(6).getChild(1));
        } finally {
            if (addConsumer) {
                dotIdentifierConsumerStack.pop();
            }
        }
    }
    return result;
}
Also used : HqlParser(org.hibernate.grammars.hql.HqlParser) DotIdentifierConsumer(org.hibernate.query.hql.spi.DotIdentifierConsumer) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath)

Example 3 with DotIdentifierConsumer

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

the class SemanticQueryBuilder method visitSimplePath.

@Override
public SemanticPathPart visitSimplePath(HqlParser.SimplePathContext ctx) {
    final int numberOfContinuations = ctx.getChildCount() - 1;
    final boolean hasContinuations = numberOfContinuations != 0;
    final DotIdentifierConsumer dotIdentifierConsumer = dotIdentifierConsumerStack.getCurrent();
    final HqlParser.IdentifierContext identifierContext = (HqlParser.IdentifierContext) ctx.getChild(0);
    assert identifierContext.getChildCount() == 1;
    dotIdentifierConsumer.consumeIdentifier(visitIdentifier(identifierContext), true, !hasContinuations);
    if (hasContinuations) {
        for (int i = 1; i < ctx.getChildCount(); i++) {
            final HqlParser.SimplePathElementContext continuation = (HqlParser.SimplePathElementContext) ctx.getChild(i);
            final HqlParser.IdentifierContext identifier = (HqlParser.IdentifierContext) continuation.getChild(1);
            assert identifier.getChildCount() == 1;
            dotIdentifierConsumer.consumeIdentifier(visitIdentifier(identifier), false, i >= numberOfContinuations);
        }
    }
    return dotIdentifierConsumer.getConsumedPart();
}
Also used : HqlParser(org.hibernate.grammars.hql.HqlParser) DotIdentifierConsumer(org.hibernate.query.hql.spi.DotIdentifierConsumer)

Example 4 with DotIdentifierConsumer

use of org.hibernate.query.hql.spi.DotIdentifierConsumer 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)

Example 5 with DotIdentifierConsumer

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

the class SemanticQueryBuilder method resolveEnumShorthandLiteral.

private SqmExpression<?> resolveEnumShorthandLiteral(HqlParser.ExpressionContext expressionContext, Map<Class<?>, Enum<?>> possibleEnumValues, Class<?> enumType) {
    final Enum<?> enumValue;
    if (possibleEnumValues != null && (enumValue = possibleEnumValues.get(enumType)) != null) {
        DotIdentifierConsumer dotIdentifierConsumer = dotIdentifierConsumerStack.getCurrent();
        dotIdentifierConsumer.consumeIdentifier(enumValue.getClass().getCanonicalName(), true, false);
        dotIdentifierConsumer.consumeIdentifier(enumValue.name(), false, true);
        return (SqmExpression<?>) dotIdentifierConsumerStack.getCurrent().getConsumedPart();
    } else {
        return (SqmExpression<?>) expressionContext.accept(this);
    }
}
Also used : SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) DotIdentifierConsumer(org.hibernate.query.hql.spi.DotIdentifierConsumer)

Aggregations

DotIdentifierConsumer (org.hibernate.query.hql.spi.DotIdentifierConsumer)7 HqlParser (org.hibernate.grammars.hql.HqlParser)6 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)3 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)3 PluralPersistentAttribute (org.hibernate.metamodel.model.domain.PluralPersistentAttribute)2 SemanticException (org.hibernate.query.SemanticException)2 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 BigInteger (java.math.BigInteger)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 SqmCreationProcessingState (org.hibernate.query.hql.spi.SqmCreationProcessingState)1 SqmPathRegistry (org.hibernate.query.hql.spi.SqmPathRegistry)1 ParsingException (org.hibernate.query.sqm.ParsingException)1 UnknownEntityException (org.hibernate.query.sqm.UnknownEntityException)1 AbstractSqmFrom (org.hibernate.query.sqm.tree.domain.AbstractSqmFrom)1 SqmCorrelation (org.hibernate.query.sqm.tree.domain.SqmCorrelation)1 SqmListJoin (org.hibernate.query.sqm.tree.domain.SqmListJoin)1 SqmMapJoin (org.hibernate.query.sqm.tree.domain.SqmMapJoin)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)1 SqmPluralValuedSimplePath (org.hibernate.query.sqm.tree.domain.SqmPluralValuedSimplePath)1