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);
}
}
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();
}
}
}
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);
}
}
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);
}
}
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();
}
}
Aggregations