Search in sources :

Example 1 with Overflow

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

the class ListaggGroupConcatEmulation method render.

@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, List<SortSpecification> withinGroup, SqlAstTranslator<?> translator) {
    final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
    sqlAppender.appendSql("group_concat(");
    final SqlAstNode firstArg = sqlAstArguments.get(0);
    final Expression arg;
    if (firstArg instanceof Distinct) {
        sqlAppender.appendSql("distinct ");
        arg = ((Distinct) firstArg).getExpression();
    } else {
        arg = (Expression) firstArg;
    }
    if (caseWrapper) {
        translator.getCurrentClauseStack().push(Clause.WHERE);
        sqlAppender.appendSql("case when ");
        filter.accept(translator);
        sqlAppender.appendSql(" then ");
        arg.accept(translator);
        sqlAppender.appendSql(" else null end");
        translator.getCurrentClauseStack().pop();
    } else {
        arg.accept(translator);
    }
    if (withinGroup != null && !withinGroup.isEmpty()) {
        translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
        sqlAppender.appendSql(" order by ");
        withinGroup.get(0).accept(translator);
        for (int i = 1; i < withinGroup.size(); i++) {
            sqlAppender.appendSql(',');
            withinGroup.get(i).accept(translator);
        }
        translator.getCurrentClauseStack().pop();
    }
    if (sqlAstArguments.size() != 1) {
        SqlAstNode separator = sqlAstArguments.get(1);
        // group_concat doesn't support the overflow clause, so we just omit it
        if (separator instanceof Overflow) {
            separator = ((Overflow) separator).getSeparatorExpression();
        }
        sqlAppender.appendSql(" separator ");
        separator.accept(translator);
    }
    sqlAppender.appendSql(')');
    if (!caseWrapper && filter != null) {
        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) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) Overflow(org.hibernate.sql.ast.tree.expression.Overflow)

Example 2 with Overflow

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

the class ListaggStringAggEmulation method render.

@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, List<SortSpecification> withinGroup, SqlAstTranslator<?> translator) {
    final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
    sqlAppender.appendSql(functionName);
    sqlAppender.appendSql('(');
    final SqlAstNode firstArg = sqlAstArguments.get(0);
    final Expression arg;
    if (firstArg instanceof Distinct) {
        sqlAppender.appendSql("distinct ");
        arg = ((Distinct) firstArg).getExpression();
    } else {
        arg = (Expression) firstArg;
    }
    if (caseWrapper) {
        translator.getCurrentClauseStack().push(Clause.WHERE);
        sqlAppender.appendSql("case when ");
        filter.accept(translator);
        sqlAppender.appendSql(" then ");
        renderAsString(sqlAppender, translator, arg);
        sqlAppender.appendSql(" else null end");
        translator.getCurrentClauseStack().pop();
    } else {
        renderAsString(sqlAppender, translator, arg);
    }
    if (sqlAstArguments.size() != 1) {
        SqlAstNode separator = sqlAstArguments.get(1);
        // string_agg doesn't support the overflow clause, so we just omit it
        if (separator instanceof Overflow) {
            separator = ((Overflow) separator).getSeparatorExpression();
        }
        sqlAppender.appendSql(',');
        separator.accept(translator);
        if (!withinGroupClause && withinGroup != null && !withinGroup.isEmpty()) {
            translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
            sqlAppender.appendSql(" order by ");
            withinGroup.get(0).accept(translator);
            for (int i = 1; i < withinGroup.size(); i++) {
                sqlAppender.appendSql(',');
                withinGroup.get(i).accept(translator);
            }
            translator.getCurrentClauseStack().pop();
        }
    }
    sqlAppender.appendSql(')');
    if (withinGroupClause && withinGroup != null && !withinGroup.isEmpty()) {
        translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
        sqlAppender.appendSql(" within group (order by ");
        withinGroup.get(0).accept(translator);
        for (int i = 1; i < withinGroup.size(); i++) {
            sqlAppender.appendSql(',');
            withinGroup.get(i).accept(translator);
        }
        sqlAppender.appendSql(')');
        translator.getCurrentClauseStack().pop();
    }
    if (!caseWrapper && filter != null) {
        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) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) Overflow(org.hibernate.sql.ast.tree.expression.Overflow)

Aggregations

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