Search in sources :

Example 1 with Star

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

the class PatternRenderer method render.

private void render(SqlAppender sqlAppender, List<? extends SqlAstNode> args, Predicate filter, List<SortSpecification> withinGroup, Boolean respectNulls, Boolean fromFirst, SqlAstTranslator<?> translator) {
    final int numberOfArguments = args.size();
    final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
    if (numberOfArguments < maxParamIndex) {
        LOG.missingArguments(maxParamIndex, numberOfArguments);
    }
    for (int i = 0; i < chunks.length; i++) {
        if (i == varargParam) {
            for (int j = i; j < numberOfArguments; j++) {
                final SqlAstNode arg = args.get(j);
                if (arg != null) {
                    sqlAppender.appendSql(chunks[i]);
                    if (caseWrapper && !(arg instanceof Distinct) && !(arg instanceof Star)) {
                        translator.getCurrentClauseStack().push(Clause.WHERE);
                        sqlAppender.appendSql("case when ");
                        filter.accept(translator);
                        translator.getCurrentClauseStack().pop();
                        sqlAppender.appendSql(" then ");
                        translator.render(arg, argumentRenderingMode);
                        sqlAppender.appendSql(" else null end");
                    } else {
                        translator.render(arg, argumentRenderingMode);
                    }
                }
            }
        } else if (i < paramIndexes.length) {
            final int index = paramIndexes[i] - 1;
            final SqlAstNode arg = index < numberOfArguments ? args.get(index) : null;
            if (arg != null || i == 0) {
                sqlAppender.appendSql(chunks[i]);
            }
            if (arg != null) {
                if (caseWrapper && !(arg instanceof Distinct) && !(arg instanceof Star)) {
                    translator.getCurrentClauseStack().push(Clause.WHERE);
                    sqlAppender.appendSql("case when ");
                    filter.accept(translator);
                    translator.getCurrentClauseStack().pop();
                    sqlAppender.appendSql(" then ");
                    translator.render(arg, argumentRenderingMode);
                    sqlAppender.appendSql(" else null end");
                } else {
                    translator.render(arg, argumentRenderingMode);
                }
            }
        } else {
            sqlAppender.appendSql(chunks[i]);
        }
    }
    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 2 with Star

use of org.hibernate.sql.ast.tree.expression.Star 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)

Aggregations

SqlAstNode (org.hibernate.sql.ast.tree.SqlAstNode)2 Distinct (org.hibernate.sql.ast.tree.expression.Distinct)2 Star (org.hibernate.sql.ast.tree.expression.Star)2