Search in sources :

Example 6 with ParsingException

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

the class SemanticQueryBuilder method visitSortSpecification.

@Override
public SqmSortSpecification visitSortSpecification(HqlParser.SortSpecificationContext ctx) {
    final SqmExpression<?> sortExpression = visitSortExpression((HqlParser.SortExpressionContext) ctx.getChild(0));
    if (sortExpression == null) {
        throw new ParsingException("Could not resolve sort-expression : " + ctx.getChild(0).getText());
    }
    if (sortExpression instanceof SqmLiteral || sortExpression instanceof SqmParameter) {
        HqlLogging.QUERY_LOGGER.debugf("Questionable sorting by constant value : %s", sortExpression);
    }
    final SortOrder sortOrder;
    final NullPrecedence nullPrecedence;
    int nextIndex = 1;
    if (nextIndex < ctx.getChildCount()) {
        ParseTree parseTree = ctx.getChild(nextIndex);
        if (parseTree instanceof HqlParser.SortDirectionContext) {
            switch(((TerminalNode) parseTree.getChild(0)).getSymbol().getType()) {
                case HqlParser.ASC:
                    sortOrder = SortOrder.ASCENDING;
                    break;
                case HqlParser.DESC:
                    sortOrder = SortOrder.DESCENDING;
                    break;
                default:
                    throw new SemanticException("Unrecognized sort ordering: " + parseTree.getText());
            }
            nextIndex++;
        } else {
            sortOrder = null;
        }
        parseTree = ctx.getChild(nextIndex);
        if (parseTree instanceof HqlParser.NullsPrecedenceContext) {
            switch(((TerminalNode) parseTree.getChild(1)).getSymbol().getType()) {
                case HqlParser.FIRST:
                    nullPrecedence = NullPrecedence.FIRST;
                    break;
                case HqlParser.LAST:
                    nullPrecedence = NullPrecedence.LAST;
                    break;
                default:
                    throw new SemanticException("Unrecognized null precedence: " + parseTree.getText());
            }
        } else {
            nullPrecedence = null;
        }
    } else {
        sortOrder = null;
        nullPrecedence = null;
    }
    return new SqmSortSpecification(sortExpression, sortOrder, nullPrecedence);
}
Also used : NullPrecedence(org.hibernate.query.sqm.NullPrecedence) SortOrder(org.hibernate.query.sqm.SortOrder) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) HqlParser(org.hibernate.grammars.hql.HqlParser) ParsingException(org.hibernate.query.sqm.ParsingException) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Example 7 with ParsingException

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

the class SemanticQueryBuilder method createCollectionReferenceSubQuery.

private <X> SqmSubQuery<X> createCollectionReferenceSubQuery(HqlParser.SimplePathContext pathCtx, TerminalNode collectionReferenceCtx) {
    final SqmPath<?> pluralAttributePath = consumeDomainPath(pathCtx);
    final SqmPathSource<?> referencedPathSource = pluralAttributePath.getReferencedPathSource();
    if (!(referencedPathSource instanceof PluralPersistentAttribute)) {
        throw new PathException("Path is not a plural path '" + pluralAttributePath.getNavigablePath() + "'");
    }
    final SqmSubQuery<?> subQuery = new SqmSubQuery<>(processingStateStack.getCurrent().getProcessingQuery(), creationContext.getNodeBuilder());
    final SqmSelectClause selectClause = new SqmSelectClause(false, 1, creationContext.getNodeBuilder());
    final SqmFromClause fromClause = new SqmFromClause(1);
    SqmPath<?> lhs = pluralAttributePath.getLhs();
    final List<String> implicitJoinPaths = new ArrayList<>();
    while (!(lhs instanceof AbstractSqmFrom<?, ?>)) {
        implicitJoinPaths.add(lhs.getNavigablePath().getUnaliasedLocalName());
        lhs = lhs.getLhs();
    }
    final AbstractSqmFrom<?, ?> correlationBase = (AbstractSqmFrom<?, ?>) lhs;
    final SqmCorrelation<?, ?> correlation = correlationBase.createCorrelation();
    SqmFrom<?, ?> joinBase = correlation;
    for (int i = implicitJoinPaths.size() - 1; i >= 0; i--) {
        joinBase = joinBase.join(implicitJoinPaths.get(i));
    }
    final SqmAttributeJoin<?, ?> collectionJoin = joinBase.join(pluralAttributePath.getNavigablePath().getUnaliasedLocalName());
    fromClause.addRoot(correlation.getCorrelatedRoot());
    if (collectionReferenceCtx == null) {
        final SqmLiteral<Integer> literal = new SqmLiteral<>(1, creationContext.getNodeBuilder().getIntegerType(), creationContext.getNodeBuilder());
        subQuery.applyInferableType(literal.getNodeType());
        selectClause.setSelection(literal);
    } else {
        final String partName;
        switch(collectionReferenceCtx.getSymbol().getType()) {
            case HqlParser.ELEMENTS:
                partName = CollectionPart.Nature.ELEMENT.getName();
                break;
            case HqlParser.INDICES:
                partName = CollectionPart.Nature.INDEX.getName();
                break;
            default:
                throw new ParsingException("Unexpected collection reference : " + collectionReferenceCtx.getText());
        }
        final SqmPath<?> path = collectionJoin.resolvePathPart(partName, true, this);
        subQuery.applyInferableType(path.getNodeType());
        selectClause.setSelection(path);
    }
    final SqmQuerySpec<?> querySpec = subQuery.getQuerySpec();
    querySpec.setFromClause(fromClause);
    querySpec.setSelectClause(selectClause);
    // noinspection unchecked
    return (SqmSubQuery<X>) subQuery;
}
Also used : SqmSubQuery(org.hibernate.query.sqm.tree.select.SqmSubQuery) PluralPersistentAttribute(org.hibernate.metamodel.model.domain.PluralPersistentAttribute) SqmSelectClause(org.hibernate.query.sqm.tree.select.SqmSelectClause) ArrayList(java.util.ArrayList) AbstractSqmFrom(org.hibernate.query.sqm.tree.domain.AbstractSqmFrom) PathException(org.hibernate.query.PathException) BigInteger(java.math.BigInteger) ParsingException(org.hibernate.query.sqm.ParsingException) SqmFromClause(org.hibernate.query.sqm.tree.from.SqmFromClause) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Example 8 with ParsingException

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

the class SemanticQueryBuilder method visitUnaryNumericLiteralExpression.

@Override
public SqmExpression<?> visitUnaryNumericLiteralExpression(HqlParser.UnaryNumericLiteralExpressionContext ctx) {
    final TerminalNode node = (TerminalNode) ctx.getChild(1).getChild(0);
    final String text;
    if (((TerminalNode) ctx.getChild(0).getChild(0)).getSymbol().getType() == HqlParser.MINUS) {
        text = "-" + node.getText();
    } else {
        text = node.getText();
    }
    switch(node.getSymbol().getType()) {
        case HqlParser.INTEGER_LITERAL:
            return integerOrLongLiteral(text);
        case HqlParser.LONG_LITERAL:
            return longLiteral(text);
        case HqlParser.BIG_INTEGER_LITERAL:
            return bigIntegerLiteral(text);
        case HqlParser.HEX_LITERAL:
            return hexLiteral(text);
        case HqlParser.FLOAT_LITERAL:
            return floatLiteral(text);
        case HqlParser.DOUBLE_LITERAL:
            return doubleLiteral(text);
        case HqlParser.BIG_DECIMAL_LITERAL:
            return bigDecimalLiteral(text);
        default:
            throw new ParsingException("Unexpected terminal node [" + text + "]");
    }
}
Also used : ParsingException(org.hibernate.query.sqm.ParsingException) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 9 with ParsingException

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

the class SemanticQueryBuilder method visitMultiplicationExpression.

@Override
public Object visitMultiplicationExpression(HqlParser.MultiplicationExpressionContext ctx) {
    if (ctx.getChildCount() != 3) {
        throw new ParsingException("Expecting 2 operands to the multiplicative operator");
    }
    final SqmExpression<?> left = (SqmExpression<?>) ctx.getChild(0).accept(this);
    final SqmExpression<?> right = (SqmExpression<?>) ctx.getChild(2).accept(this);
    final BinaryArithmeticOperator operator = (BinaryArithmeticOperator) ctx.getChild(1).accept(this);
    if (operator == BinaryArithmeticOperator.MODULO) {
        return getFunctionDescriptor("mod").generateSqmExpression(asList(left, right), null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
    } else {
        return new SqmBinaryArithmetic<>(operator, left, right, creationContext.getJpaMetamodel(), creationContext.getNodeBuilder());
    }
}
Also used : SqmBinaryArithmetic(org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) ParsingException(org.hibernate.query.sqm.ParsingException) BinaryArithmeticOperator(org.hibernate.query.sqm.BinaryArithmeticOperator)

Example 10 with ParsingException

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

the class StandardHqlTranslator method parseHql.

private HqlParser.StatementContext parseHql(String hql) {
    // Build the lexer
    final HqlLexer hqlLexer = HqlParseTreeBuilder.INSTANCE.buildHqlLexer(hql);
    // Build the parse tree
    final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser(hql, hqlLexer);
    // try to use SLL(k)-based parsing first - its faster
    hqlLexer.addErrorListener(ERR_LISTENER);
    hqlParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    hqlParser.removeErrorListeners();
    hqlParser.addErrorListener(ERR_LISTENER);
    hqlParser.setErrorHandler(new BailErrorStrategy());
    try {
        return hqlParser.statement();
    } catch (ParseCancellationException e) {
        // reset the input token stream and parser state
        hqlLexer.reset();
        hqlParser.reset();
        // fall back to LL(k)-based parsing
        hqlParser.getInterpreter().setPredictionMode(PredictionMode.LL);
        hqlParser.setErrorHandler(new DefaultErrorStrategy());
        return hqlParser.statement();
    } catch (ParsingException ex) {
        throw new SemanticException("A query exception occurred", hql, ex);
    }
}
Also used : ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) HqlLexer(org.hibernate.grammars.hql.HqlLexer) HqlParser(org.hibernate.grammars.hql.HqlParser) ParsingException(org.hibernate.query.sqm.ParsingException) SemanticException(org.hibernate.query.SemanticException)

Aggregations

ParsingException (org.hibernate.query.sqm.ParsingException)11 HqlParser (org.hibernate.grammars.hql.HqlParser)6 ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 SemanticException (org.hibernate.query.SemanticException)4 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)3 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)3 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 PluralPersistentAttribute (org.hibernate.metamodel.model.domain.PluralPersistentAttribute)2 PathException (org.hibernate.query.PathException)2 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)2 EntityExistsException (jakarta.persistence.EntityExistsException)1 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)1 LockTimeoutException (jakarta.persistence.LockTimeoutException)1 NoResultException (jakarta.persistence.NoResultException)1 NonUniqueResultException (jakarta.persistence.NonUniqueResultException)1 OptimisticLockException (jakarta.persistence.OptimisticLockException)1 PersistenceException (jakarta.persistence.PersistenceException)1 PessimisticLockException (jakarta.persistence.PessimisticLockException)1