Search in sources :

Example 1 with SqlAstNodeRenderingMode

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

the class AbstractSqlAstTranslator method visitAnsiCaseSearchedExpression.

protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    appendSql("case");
    final SqlAstNodeRenderingMode original = this.parameterRenderingMode;
    for (CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments()) {
        if (original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS) {
            this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
        }
        appendSql(" when ");
        whenFragment.getPredicate().accept(this);
        this.parameterRenderingMode = original;
        appendSql(" then ");
        resultRenderer.accept(whenFragment.getResult());
    }
    final Expression otherwise = caseSearchedExpression.getOtherwise();
    if (otherwise != null) {
        appendSql(" else ");
        resultRenderer.accept(otherwise);
    }
    appendSql(" end");
}
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) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SqlAstNodeRenderingMode(org.hibernate.sql.ast.SqlAstNodeRenderingMode)

Example 2 with SqlAstNodeRenderingMode

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

the class AbstractSqlAstTranslator method visitDecodeCaseSearchedExpression.

protected void visitDecodeCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression) {
    appendSql("decode( ");
    final SqlAstNodeRenderingMode original = this.parameterRenderingMode;
    final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
    final int caseNumber = whenFragments.size();
    CaseSearchedExpression.WhenFragment firstWhenFragment = null;
    for (int i = 0; i < caseNumber; i++) {
        final CaseSearchedExpression.WhenFragment whenFragment = whenFragments.get(i);
        Predicate predicate = whenFragment.getPredicate();
        if (original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS) {
            this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
        }
        if (i != 0) {
            appendSql(',');
            getLeftHandExpression(predicate).accept(this);
            this.parameterRenderingMode = original;
            appendSql(',');
            whenFragment.getResult().accept(this);
        } else {
            getLeftHandExpression(predicate).accept(this);
            firstWhenFragment = whenFragment;
        }
    }
    this.parameterRenderingMode = original;
    appendSql(',');
    firstWhenFragment.getResult().accept(this);
    final Expression otherwise = caseSearchedExpression.getOtherwise();
    if (otherwise != null) {
        appendSql(',');
        otherwise.accept(this);
    }
    appendSql(CLOSE_PARENTHESIS);
}
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) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SqlAstNodeRenderingMode(org.hibernate.sql.ast.SqlAstNodeRenderingMode) SqlFragmentPredicate(org.hibernate.persister.internal.SqlFragmentPredicate) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 3 with SqlAstNodeRenderingMode

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

the class AbstractSqlAstTranslator method visitSqlSelections.

protected void visitSqlSelections(SelectClause selectClause) {
    final List<SqlSelection> sqlSelections = selectClause.getSqlSelections();
    final int size = sqlSelections.size();
    final SelectItemReferenceStrategy referenceStrategy = getDialect().getGroupBySelectItemReferenceStrategy();
    // When the dialect needs to render the aliased expression and there are aliased group by items,
    // we need to inline parameters as the database would otherwise not be able to match the group by item
    // to the select item, ultimately leading to a query error
    final BitSet selectItemsToInline;
    if (referenceStrategy == SelectItemReferenceStrategy.EXPRESSION) {
        selectItemsToInline = getSelectItemsToInline();
    } else {
        selectItemsToInline = null;
    }
    final SqlAstNodeRenderingMode original = parameterRenderingMode;
    if (needsSelectAliases || referenceStrategy == SelectItemReferenceStrategy.ALIAS && hasSelectAliasInGroupByClause()) {
        String separator = NO_SEPARATOR;
        if (columnAliases == null) {
            for (int i = 0; i < size; i++) {
                final SqlSelection sqlSelection = sqlSelections.get(i);
                appendSql(separator);
                if (selectItemsToInline != null && selectItemsToInline.get(i)) {
                    parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
                } else {
                    parameterRenderingMode = SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER;
                }
                visitSqlSelection(sqlSelection);
                parameterRenderingMode = original;
                appendSql(" c");
                appendSql(i);
                separator = COMA_SEPARATOR;
            }
        } else {
            for (int i = 0; i < size; i++) {
                final SqlSelection sqlSelection = sqlSelections.get(i);
                appendSql(separator);
                if (selectItemsToInline != null && selectItemsToInline.get(i)) {
                    parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
                } else {
                    parameterRenderingMode = SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER;
                }
                visitSqlSelection(sqlSelection);
                parameterRenderingMode = original;
                appendSql(WHITESPACE);
                appendSql(columnAliases.get(i));
                separator = COMA_SEPARATOR;
            }
        }
        if (queryPartForRowNumbering != null) {
            renderRowNumberingSelectItems(selectClause, queryPartForRowNumbering);
        }
    } else if (columnAliases == null) {
        String separator = NO_SEPARATOR;
        for (int i = 0; i < size; i++) {
            final SqlSelection sqlSelection = sqlSelections.get(i);
            appendSql(separator);
            if (selectItemsToInline != null && selectItemsToInline.get(i)) {
                parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
            } else {
                parameterRenderingMode = SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER;
            }
            visitSqlSelection(sqlSelection);
            parameterRenderingMode = original;
            separator = COMA_SEPARATOR;
        }
    } else {
        String separator = NO_SEPARATOR;
        for (int i = 0; i < size; i++) {
            final SqlSelection sqlSelection = sqlSelections.get(i);
            appendSql(separator);
            if (selectItemsToInline != null && selectItemsToInline.get(i)) {
                parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
            } else {
                parameterRenderingMode = SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER;
            }
            visitSqlSelection(sqlSelection);
            appendSql(WHITESPACE);
            appendSql(columnAliases.get(i));
            parameterRenderingMode = original;
            separator = COMA_SEPARATOR;
        }
    }
}
Also used : SelectItemReferenceStrategy(org.hibernate.dialect.SelectItemReferenceStrategy) BitSet(java.util.BitSet) SqlAstNodeRenderingMode(org.hibernate.sql.ast.SqlAstNodeRenderingMode)

Example 4 with SqlAstNodeRenderingMode

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

the class AbstractSqlAstTranslator method visitAnsiCaseSimpleExpression.

protected void visitAnsiCaseSimpleExpression(CaseSimpleExpression caseSimpleExpression, Consumer<Expression> resultRenderer) {
    appendSql("case ");
    final SqlAstNodeRenderingMode original = this.parameterRenderingMode;
    if (original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS) {
        this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
    }
    caseSimpleExpression.getFixture().accept(this);
    for (CaseSimpleExpression.WhenFragment whenFragment : caseSimpleExpression.getWhenFragments()) {
        if (original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS) {
            this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
        }
        appendSql(" when ");
        whenFragment.getCheckValue().accept(this);
        this.parameterRenderingMode = original;
        appendSql(" then ");
        resultRenderer.accept(whenFragment.getResult());
    }
    this.parameterRenderingMode = original;
    final Expression otherwise = caseSimpleExpression.getOtherwise();
    if (otherwise != null) {
        appendSql(" else ");
        resultRenderer.accept(otherwise);
    }
    appendSql(" end");
}
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) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlAstNodeRenderingMode(org.hibernate.sql.ast.SqlAstNodeRenderingMode)

Example 5 with SqlAstNodeRenderingMode

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

the class AbstractSqlAstTranslator method visitPartitionExpressions.

protected final void visitPartitionExpressions(List<Expression> partitionExpressions, Function<Expression, Expression> resolveAliasExpression, boolean inlineParametersOfAliasedExpressions) {
    String separator = "";
    for (Expression partitionExpression : partitionExpressions) {
        final SqlTuple sqlTuple = SqlTupleContainer.getSqlTuple(partitionExpression);
        if (sqlTuple != null) {
            for (Expression e : sqlTuple.getExpressions()) {
                appendSql(separator);
                final Expression resolved = resolveAliasExpression.apply(e);
                if (inlineParametersOfAliasedExpressions && resolved != e) {
                    final SqlAstNodeRenderingMode original = parameterRenderingMode;
                    parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
                    renderPartitionItem(resolved);
                    parameterRenderingMode = original;
                } else {
                    renderPartitionItem(resolved);
                }
                separator = COMA_SEPARATOR;
            }
        } else {
            appendSql(separator);
            final Expression resolved = resolveAliasExpression.apply(partitionExpression);
            if (inlineParametersOfAliasedExpressions && resolved != partitionExpression) {
                final SqlAstNodeRenderingMode original = parameterRenderingMode;
                parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
                renderPartitionItem(resolved);
                parameterRenderingMode = original;
            } else {
                renderPartitionItem(resolved);
            }
        }
        separator = COMA_SEPARATOR;
    }
}
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) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) SqlAstNodeRenderingMode(org.hibernate.sql.ast.SqlAstNodeRenderingMode)

Aggregations

SqlAstNodeRenderingMode (org.hibernate.sql.ast.SqlAstNodeRenderingMode)5 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)4 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)4 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)4 Expression (org.hibernate.sql.ast.tree.expression.Expression)4 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)4 OrderedSetAggregateFunctionExpression (org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression)4 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)4 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)4 BitSet (java.util.BitSet)1 SelectItemReferenceStrategy (org.hibernate.dialect.SelectItemReferenceStrategy)1 SqlFragmentPredicate (org.hibernate.persister.internal.SqlFragmentPredicate)1 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)1 BetweenPredicate (org.hibernate.sql.ast.tree.predicate.BetweenPredicate)1 BooleanExpressionPredicate (org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate)1 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)1 ExistsPredicate (org.hibernate.sql.ast.tree.predicate.ExistsPredicate)1 FilterPredicate (org.hibernate.sql.ast.tree.predicate.FilterPredicate)1 GroupedPredicate (org.hibernate.sql.ast.tree.predicate.GroupedPredicate)1 InListPredicate (org.hibernate.sql.ast.tree.predicate.InListPredicate)1