Search in sources :

Example 31 with SqlTuple

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

Example 32 with SqlTuple

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

Example 33 with SqlTuple

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

the class AbstractSqlAstTranslator method visitInListPredicate.

@Override
public void visitInListPredicate(InListPredicate inListPredicate) {
    final List<Expression> listExpressions = inListPredicate.getListExpressions();
    if (listExpressions.isEmpty()) {
        appendSql("1=0");
        return;
    }
    Function<Expression, Expression> itemAccessor = Function.identity();
    final SqlTuple lhsTuple;
    if ((lhsTuple = SqlTupleContainer.getSqlTuple(inListPredicate.getTestExpression())) != null) {
        if (lhsTuple.getExpressions().size() == 1) {
            // Special case for tuples with arity 1 as any DBMS supports scalar IN predicates
            itemAccessor = listExpression -> SqlTupleContainer.getSqlTuple(listExpression).getExpressions().get(0);
        } else if (!supportsRowValueConstructorSyntaxInInList()) {
            final ComparisonOperator comparisonOperator = inListPredicate.isNegated() ? ComparisonOperator.NOT_EQUAL : ComparisonOperator.EQUAL;
            // Some DBs like Oracle support tuples only for the IN subquery predicate
            if (supportsRowValueConstructorSyntaxInInSubQuery() && getDialect().supportsUnionAll()) {
                inListPredicate.getTestExpression().accept(this);
                if (inListPredicate.isNegated()) {
                    appendSql(" not");
                }
                appendSql(" in(");
                String separator = NO_SEPARATOR;
                for (Expression expression : listExpressions) {
                    appendSql(separator);
                    renderExpressionsAsSubquery(SqlTupleContainer.getSqlTuple(expression).getExpressions());
                    separator = " union all ";
                }
                appendSql(CLOSE_PARENTHESIS);
            } else {
                String separator = NO_SEPARATOR;
                for (Expression expression : listExpressions) {
                    appendSql(separator);
                    emulateTupleComparison(lhsTuple.getExpressions(), SqlTupleContainer.getSqlTuple(expression).getExpressions(), comparisonOperator, true);
                    separator = " or ";
                }
            }
            return;
        }
    }
    inListPredicate.getTestExpression().accept(this);
    if (inListPredicate.isNegated()) {
        appendSql(" not");
    }
    appendSql(" in(");
    String separator = NO_SEPARATOR;
    int bindValueCount = listExpressions.size();
    int bindValueMaxCount = bindValueCount;
    final Dialect dialect = getSessionFactory().getJdbcServices().getDialect();
    int inExprLimit = dialect.getInExpressionCountLimit();
    final boolean inClauseParameterPaddingEnabled = getSessionFactory().getSessionFactoryOptions().inClauseParameterPaddingEnabled() && bindValueCount > 2;
    if (inClauseParameterPaddingEnabled) {
        // bindValueCount: 1005
        // bindValuePaddingCount: 1024
        int bindValuePaddingCount = MathHelper.ceilingPowerOfTwo(bindValueCount);
        // inExprLimit: 1000
        if (inExprLimit > 0) {
            if (bindValuePaddingCount > inExprLimit) {
                // bindValuePaddingCount: 8
                if (bindValueCount < inExprLimit) {
                    bindValueMaxCount = inExprLimit;
                } else {
                    bindValueMaxCount = MathHelper.ceilingPowerOfTwo(bindValueCount % inExprLimit);
                }
            } else if (bindValueCount < bindValuePaddingCount) {
                bindValueMaxCount = bindValuePaddingCount;
            }
        } else if (bindValueCount < bindValuePaddingCount) {
            bindValueMaxCount = bindValuePaddingCount;
        }
    }
    final Iterator<Expression> iterator = listExpressions.iterator();
    int itemNumber = 0;
    while (iterator.hasNext() && (inExprLimit == 0 || itemNumber < inExprLimit)) {
        final Expression listExpression = itemAccessor.apply(iterator.next());
        appendSql(separator);
        listExpression.accept(this);
        separator = COMA_SEPARATOR;
        itemNumber++;
        // and just render through the in list expressions as they are without padding/splitting
        if (!(listExpression instanceof JdbcParameter || listExpression instanceof SqmParameterInterpretation)) {
            inExprLimit = 0;
            bindValueMaxCount = bindValueCount;
        }
    }
    if (itemNumber != inExprLimit && bindValueCount == bindValueMaxCount) {
        appendSql(CLOSE_PARENTHESIS);
        return;
    }
    if (inExprLimit > 0 && bindValueCount > inExprLimit) {
        do {
            append(") and ");
            inListPredicate.getTestExpression().accept(this);
            if (inListPredicate.isNegated()) {
                appendSql(" not");
            }
            appendSql(" in(");
            separator = NO_SEPARATOR;
            itemNumber = 0;
            while (iterator.hasNext() && itemNumber < inExprLimit) {
                final Expression listExpression = iterator.next();
                appendSql(separator);
                itemAccessor.apply(listExpression).accept(this);
                separator = COMA_SEPARATOR;
                itemNumber++;
            }
        } while (iterator.hasNext());
    }
    int i;
    if (inExprLimit > 0 && bindValueCount > inExprLimit) {
        i = bindValueCount % inExprLimit;
    } else {
        i = bindValueCount;
    }
    final Expression lastExpression = itemAccessor.apply(listExpressions.get(listExpressions.size() - 1));
    for (; i < bindValueMaxCount; i++) {
        appendSql(separator);
        lastExpression.accept(this);
        separator = COMA_SEPARATOR;
    }
    appendSql(CLOSE_PARENTHESIS);
}
Also used : ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) 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) FilterJdbcParameter(org.hibernate.internal.FilterJdbcParameter) AbstractJdbcParameter(org.hibernate.sql.exec.internal.AbstractJdbcParameter) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) SqlTypedMappingJdbcParameter(org.hibernate.sql.exec.internal.SqlTypedMappingJdbcParameter) SqmParameterInterpretation(org.hibernate.query.sqm.sql.internal.SqmParameterInterpretation) Dialect(org.hibernate.dialect.Dialect) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple)

Example 34 with SqlTuple

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

the class AbstractSqlAstTranslator method visitUpdateStatementOnly.

protected void visitUpdateStatementOnly(UpdateStatement statement) {
    // todo (6.0) : to support joins we need dialect support
    appendSql("update ");
    final Stack<Clause> clauseStack = getClauseStack();
    try {
        clauseStack.push(Clause.UPDATE);
        renderNamedTableReference(statement.getTargetTable(), LockMode.NONE);
    } finally {
        clauseStack.pop();
    }
    appendSql(" set ");
    boolean firstPass = true;
    try {
        clauseStack.push(Clause.SET);
        for (Assignment assignment : statement.getAssignments()) {
            if (firstPass) {
                firstPass = false;
            } else {
                appendSql(COMA_SEPARATOR_CHAR);
            }
            final List<ColumnReference> columnReferences = assignment.getAssignable().getColumnReferences();
            if (columnReferences.size() == 1) {
                columnReferences.get(0).accept(this);
                appendSql('=');
                final Expression assignedValue = assignment.getAssignedValue();
                final SqlTuple sqlTuple = SqlTupleContainer.getSqlTuple(assignedValue);
                if (sqlTuple != null) {
                    final Expression expression = sqlTuple.getExpressions().get(0);
                    expression.accept(this);
                } else {
                    assignedValue.accept(this);
                }
            } else {
                appendSql(OPEN_PARENTHESIS);
                for (ColumnReference columnReference : columnReferences) {
                    columnReference.accept(this);
                }
                appendSql(")=");
                assignment.getAssignedValue().accept(this);
            }
        }
    } finally {
        clauseStack.pop();
    }
    visitWhereClause(statement.getRestriction());
    visitReturningColumns(statement);
}
Also used : Assignment(org.hibernate.sql.ast.tree.update.Assignment) 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) Clause(org.hibernate.sql.ast.Clause) FromClause(org.hibernate.sql.ast.tree.from.FromClause) SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)34 Expression (org.hibernate.sql.ast.tree.expression.Expression)30 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)20 ArrayList (java.util.ArrayList)17 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)15 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)14 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)14 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)14 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)14 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)14 NavigablePath (org.hibernate.query.spi.NavigablePath)12 TableReference (org.hibernate.sql.ast.tree.from.TableReference)11 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)10 ModelPart (org.hibernate.metamodel.mapping.ModelPart)10 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)10 List (java.util.List)9 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)9 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)9 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)9 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)9