Search in sources :

Example 71 with Expression

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

the class DerbySqlAstTranslator method visitInListPredicate.

@Override
public void visitInListPredicate(InListPredicate inListPredicate) {
    final List<Expression> listExpressions = inListPredicate.getListExpressions();
    if (listExpressions.isEmpty()) {
        appendSql("1=0");
        return;
    }
    final Expression testExpression = inListPredicate.getTestExpression();
    if (isParameter(testExpression)) {
        renderCasted(testExpression);
        if (inListPredicate.isNegated()) {
            appendSql(" not");
        }
        appendSql(" in(");
        renderCommaSeparated(listExpressions);
        appendSql(CLOSE_PARENTHESIS);
    } else {
        super.visitInListPredicate(inListPredicate);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)

Example 72 with Expression

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

the class SQLServerSqlAstTranslator method visitOffsetFetchClause.

@Override
public void visitOffsetFetchClause(QueryPart queryPart) {
    if (!isRowNumberingCurrentQueryPart()) {
        if (getDialect().getVersion().isBefore(9) && !queryPart.isRoot() && queryPart.getOffsetClauseExpression() != null) {
            throw new IllegalArgumentException("Can't emulate offset clause in subquery");
        }
        // Note that SQL Server is very strict i.e. it requires an order by clause for TOP or OFFSET
        final OffsetFetchClauseMode offsetFetchClauseMode = getOffsetFetchClauseMode(queryPart);
        if (offsetFetchClauseMode == OffsetFetchClauseMode.STANDARD) {
            if (!queryPart.hasSortSpecifications()) {
                appendSql(' ');
                renderEmptyOrderBy();
            }
            final Expression offsetExpression;
            final Expression fetchExpression;
            final FetchClauseType fetchClauseType;
            if (queryPart.isRoot() && hasLimit()) {
                prepareLimitOffsetParameters();
                offsetExpression = getOffsetParameter();
                fetchExpression = getLimitParameter();
                fetchClauseType = FetchClauseType.ROWS_ONLY;
            } else {
                offsetExpression = queryPart.getOffsetClauseExpression();
                fetchExpression = queryPart.getFetchClauseExpression();
                fetchClauseType = queryPart.getFetchClauseType();
            }
            if (offsetExpression == null) {
                appendSql(" offset 0 rows");
            } else {
                renderOffset(offsetExpression, true);
            }
            if (fetchExpression != null) {
                renderFetch(fetchExpression, null, fetchClauseType);
            }
        } else if (offsetFetchClauseMode == OffsetFetchClauseMode.TOP_ONLY && !queryPart.hasSortSpecifications()) {
            appendSql(' ');
            renderEmptyOrderBy();
        }
    }
}
Also used : FetchClauseType(org.hibernate.query.sqm.FetchClauseType) Expression(org.hibernate.sql.ast.tree.expression.Expression)

Example 73 with Expression

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

the class SybaseAnywhereSqlAstTranslator method visitAnsiCaseSearchedExpression.

// Sybase Anywhere does not allow CASE expressions where all result arms contain plain parameters.
// At least one result arm must provide some type context for inference,
// so we cast the first result arm if we encounter this condition
@Override
protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSearchedExpression)) {
        final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)

Example 74 with Expression

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

the class SybaseAnywhereSqlAstTranslator method visitAnsiCaseSimpleExpression.

@Override
protected void visitAnsiCaseSimpleExpression(CaseSimpleExpression caseSimpleExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSimpleExpression)) {
        final List<CaseSimpleExpression.WhenFragment> whenFragments = caseSimpleExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSimpleExpression(caseSimpleExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSimpleExpression(caseSimpleExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)

Example 75 with Expression

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

Expression (org.hibernate.sql.ast.tree.expression.Expression)130 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)68 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)67 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)62 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)57 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)54 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)54 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)42 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)37 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)35 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)34 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)33 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)32 ArrayList (java.util.ArrayList)31 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)31 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)28 TableReference (org.hibernate.sql.ast.tree.from.TableReference)25 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)23 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)23 NavigablePath (org.hibernate.query.spi.NavigablePath)21