use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.
the class SelfRenderingSqmOrderedSetAggregateFunction method convertToSqlAst.
@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
ArgumentsValidator argumentsValidator = getArgumentsValidator();
if (argumentsValidator != null) {
argumentsValidator.validateSqlTypes(arguments, getFunctionName());
}
List<SortSpecification> withinGroup;
if (this.withinGroup == null) {
withinGroup = Collections.emptyList();
} else {
walker.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
try {
final List<SqmSortSpecification> sortSpecifications = this.withinGroup.getSortSpecifications();
withinGroup = new ArrayList<>(sortSpecifications.size());
for (SqmSortSpecification sortSpecification : sortSpecifications) {
final SortSpecification specification = (SortSpecification) walker.visitSortSpecification(sortSpecification);
if (specification != null) {
withinGroup.add(specification);
}
}
} finally {
walker.getCurrentClauseStack().pop();
}
}
return new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), arguments, getFilter() == null ? null : walker.visitNestedTopLevelPredicate(getFilter()), withinGroup, resultType, getMappingModelExpressible(walker, resultType));
}
use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.
the class NamedSqmFunctionDescriptor method render.
private void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, List<SortSpecification> withinGroup, Boolean respectNulls, Boolean fromFirst, SqlAstTranslator<?> translator) {
final boolean useParens = useParenthesesWhenNoArgs || !sqlAstArguments.isEmpty();
final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
sqlAppender.appendSql(functionName);
if (useParens) {
sqlAppender.appendSql("(");
}
boolean firstPass = true;
for (SqlAstNode arg : sqlAstArguments) {
if (!firstPass) {
sqlAppender.appendSql(",");
}
if (caseWrapper && !(arg instanceof Distinct)) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" then ");
if ((arg instanceof Star)) {
sqlAppender.appendSql("1");
} else {
translator.render(arg, argumentRenderingMode);
}
sqlAppender.appendSql(" else null end");
} else {
translator.render(arg, argumentRenderingMode);
}
firstPass = false;
}
if (useParens) {
sqlAppender.appendSql(")");
}
if (withinGroup != null && !withinGroup.isEmpty()) {
translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
sqlAppender.appendSql(" within group (order by");
translator.render(withinGroup.get(0), argumentRenderingMode);
for (int i = 1; i < withinGroup.size(); i++) {
sqlAppender.appendSql(SqlAppender.COMA_SEPARATOR_CHAR);
translator.render(withinGroup.get(0), argumentRenderingMode);
}
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
if (fromFirst != null) {
if (fromFirst) {
sqlAppender.appendSql(" from first");
} else {
sqlAppender.appendSql(" from last");
}
}
if (respectNulls != null) {
if (respectNulls) {
sqlAppender.appendSql(" respect nulls");
} else {
sqlAppender.appendSql(" ignore nulls");
}
}
if (filter != null && !caseWrapper) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql(" filter (where ");
filter.accept(translator);
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
}
use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.
the class SelfRenderingSqmWindowFunction method convertToSqlAst.
@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
ArgumentsValidator argumentsValidator = getArgumentsValidator();
if (argumentsValidator != null) {
argumentsValidator.validateSqlTypes(arguments, getFunctionName());
}
return new SelfRenderingWindowFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), arguments, filter == null ? null : walker.visitNestedTopLevelPredicate(filter), respectNulls, fromFirst, resultType, getMappingModelExpressible(walker, resultType));
}
Aggregations