Search in sources :

Example 21 with SqlAstNode

use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.

the class SelfRenderingSqmOrderedSetAggregateFunction method convertToSqlAst.

@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
    final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
    List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
    ArgumentsValidator argumentsValidator = getArgumentsValidator();
    if (argumentsValidator != null) {
        argumentsValidator.validateSqlTypes(arguments, getFunctionName());
    }
    List<SortSpecification> withinGroup;
    if (this.withinGroup == null) {
        withinGroup = Collections.emptyList();
    } else {
        walker.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
        try {
            final List<SqmSortSpecification> sortSpecifications = this.withinGroup.getSortSpecifications();
            withinGroup = new ArrayList<>(sortSpecifications.size());
            for (SqmSortSpecification sortSpecification : sortSpecifications) {
                final SortSpecification specification = (SortSpecification) walker.visitSortSpecification(sortSpecification);
                if (specification != null) {
                    withinGroup.add(specification);
                }
            }
        } finally {
            walker.getCurrentClauseStack().pop();
        }
    }
    return new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), arguments, getFilter() == null ? null : walker.visitNestedTopLevelPredicate(getFilter()), withinGroup, resultType, getMappingModelExpressible(walker, resultType));
}
Also used : SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) ArgumentsValidator(org.hibernate.query.sqm.produce.function.ArgumentsValidator) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification)

Example 22 with SqlAstNode

use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.

the class NamedSqmFunctionDescriptor method render.

private void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, List<SortSpecification> withinGroup, Boolean respectNulls, Boolean fromFirst, SqlAstTranslator<?> translator) {
    final boolean useParens = useParenthesesWhenNoArgs || !sqlAstArguments.isEmpty();
    final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
    sqlAppender.appendSql(functionName);
    if (useParens) {
        sqlAppender.appendSql("(");
    }
    boolean firstPass = true;
    for (SqlAstNode arg : sqlAstArguments) {
        if (!firstPass) {
            sqlAppender.appendSql(",");
        }
        if (caseWrapper && !(arg instanceof Distinct)) {
            translator.getCurrentClauseStack().push(Clause.WHERE);
            sqlAppender.appendSql("case when ");
            filter.accept(translator);
            translator.getCurrentClauseStack().pop();
            sqlAppender.appendSql(" then ");
            if ((arg instanceof Star)) {
                sqlAppender.appendSql("1");
            } else {
                translator.render(arg, argumentRenderingMode);
            }
            sqlAppender.appendSql(" else null end");
        } else {
            translator.render(arg, argumentRenderingMode);
        }
        firstPass = false;
    }
    if (useParens) {
        sqlAppender.appendSql(")");
    }
    if (withinGroup != null && !withinGroup.isEmpty()) {
        translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
        sqlAppender.appendSql(" within group (order by");
        translator.render(withinGroup.get(0), argumentRenderingMode);
        for (int i = 1; i < withinGroup.size(); i++) {
            sqlAppender.appendSql(SqlAppender.COMA_SEPARATOR_CHAR);
            translator.render(withinGroup.get(0), argumentRenderingMode);
        }
        sqlAppender.appendSql(')');
        translator.getCurrentClauseStack().pop();
    }
    if (fromFirst != null) {
        if (fromFirst) {
            sqlAppender.appendSql(" from first");
        } else {
            sqlAppender.appendSql(" from last");
        }
    }
    if (respectNulls != null) {
        if (respectNulls) {
            sqlAppender.appendSql(" respect nulls");
        } else {
            sqlAppender.appendSql(" ignore nulls");
        }
    }
    if (filter != null && !caseWrapper) {
        translator.getCurrentClauseStack().push(Clause.WHERE);
        sqlAppender.appendSql(" filter (where ");
        filter.accept(translator);
        sqlAppender.appendSql(')');
        translator.getCurrentClauseStack().pop();
    }
}
Also used : Distinct(org.hibernate.sql.ast.tree.expression.Distinct) Star(org.hibernate.sql.ast.tree.expression.Star) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode)

Example 23 with SqlAstNode

use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.

the class SelfRenderingSqmWindowFunction method convertToSqlAst.

@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
    final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
    List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
    ArgumentsValidator argumentsValidator = getArgumentsValidator();
    if (argumentsValidator != null) {
        argumentsValidator.validateSqlTypes(arguments, getFunctionName());
    }
    return new SelfRenderingWindowFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), arguments, filter == null ? null : walker.visitNestedTopLevelPredicate(filter), respectNulls, fromFirst, resultType, getMappingModelExpressible(walker, resultType));
}
Also used : SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) ArgumentsValidator(org.hibernate.query.sqm.produce.function.ArgumentsValidator)

Aggregations

SqlAstNode (org.hibernate.sql.ast.tree.SqlAstNode)23 Expression (org.hibernate.sql.ast.tree.expression.Expression)9 Distinct (org.hibernate.sql.ast.tree.expression.Distinct)6 ArgumentsValidator (org.hibernate.query.sqm.produce.function.ArgumentsValidator)5 ArrayList (java.util.ArrayList)4 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)4 SortSpecification (org.hibernate.sql.ast.tree.select.SortSpecification)4 JdbcMappingContainer (org.hibernate.metamodel.mapping.JdbcMappingContainer)3 SqmToSqlAstConverter (org.hibernate.query.sqm.sql.SqmToSqlAstConverter)3 SqmSortSpecification (org.hibernate.query.sqm.tree.select.SqmSortSpecification)3 Over (org.hibernate.sql.ast.tree.expression.Over)3 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)3 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)3 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)3 List (java.util.List)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)2 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)2 SelfRenderingOrderedSetAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2