Search in sources :

Example 1 with NullPrecedence

use of org.hibernate.query.sqm.NullPrecedence 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 2 with NullPrecedence

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

the class AbstractSqlAstTranslator method visitSortSpecification.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ORDER BY clause
@Override
public void visitSortSpecification(SortSpecification sortSpecification) {
    final Expression sortExpression = sortSpecification.getSortExpression();
    final NullPrecedence nullPrecedence = sortSpecification.getNullPrecedence();
    final SortOrder sortOrder = sortSpecification.getSortOrder();
    final SqlTuple sqlTuple = SqlTupleContainer.getSqlTuple(sortExpression);
    if (sqlTuple != null) {
        String separator = NO_SEPARATOR;
        for (Expression expression : sqlTuple.getExpressions()) {
            appendSql(separator);
            visitSortSpecification(expression, sortOrder, nullPrecedence);
            separator = COMA_SEPARATOR;
        }
    } else {
        visitSortSpecification(sortExpression, sortOrder, nullPrecedence);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) NullPrecedence(org.hibernate.query.sqm.NullPrecedence) SortOrder(org.hibernate.query.sqm.SortOrder) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple)

Aggregations

NullPrecedence (org.hibernate.query.sqm.NullPrecedence)2 SortOrder (org.hibernate.query.sqm.SortOrder)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 SemanticException (org.hibernate.query.SemanticException)1 ParsingException (org.hibernate.query.sqm.ParsingException)1 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)1 SqmParameter (org.hibernate.query.sqm.tree.expression.SqmParameter)1 SqmSortSpecification (org.hibernate.query.sqm.tree.select.SqmSortSpecification)1 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)1 OrderedSetAggregateFunctionExpression (org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression)1 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)1 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)1 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)1