use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.
the class StandardFunctionReturnTypeResolvers method extractArgumentValuedMapping.
public static BasicValuedMapping extractArgumentValuedMapping(List<? extends SqlAstNode> arguments, int position) {
final SqlAstNode specifiedArgument = arguments.get(position - 1);
final JdbcMappingContainer specifiedArgType = specifiedArgument instanceof Expression ? ((Expression) specifiedArgument).getExpressionType() : null;
if (specifiedArgType instanceof BasicValuedMapping) {
return (BasicValuedMapping) specifiedArgType;
}
throw new QueryException(String.format(Locale.ROOT, "Function argument [%s] at specified position [%d] in call arguments was not typed as an allowable function return type", specifiedArgument, position));
}
use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.
the class PatternRenderer method render.
private void render(SqlAppender sqlAppender, List<? extends SqlAstNode> args, Predicate filter, List<SortSpecification> withinGroup, Boolean respectNulls, Boolean fromFirst, SqlAstTranslator<?> translator) {
final int numberOfArguments = args.size();
final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
if (numberOfArguments < maxParamIndex) {
LOG.missingArguments(maxParamIndex, numberOfArguments);
}
for (int i = 0; i < chunks.length; i++) {
if (i == varargParam) {
for (int j = i; j < numberOfArguments; j++) {
final SqlAstNode arg = args.get(j);
if (arg != null) {
sqlAppender.appendSql(chunks[i]);
if (caseWrapper && !(arg instanceof Distinct) && !(arg instanceof Star)) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" then ");
translator.render(arg, argumentRenderingMode);
sqlAppender.appendSql(" else null end");
} else {
translator.render(arg, argumentRenderingMode);
}
}
}
} else if (i < paramIndexes.length) {
final int index = paramIndexes[i] - 1;
final SqlAstNode arg = index < numberOfArguments ? args.get(index) : null;
if (arg != null || i == 0) {
sqlAppender.appendSql(chunks[i]);
}
if (arg != null) {
if (caseWrapper && !(arg instanceof Distinct) && !(arg instanceof Star)) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" then ");
translator.render(arg, argumentRenderingMode);
sqlAppender.appendSql(" else null end");
} else {
translator.render(arg, argumentRenderingMode);
}
}
} else {
sqlAppender.appendSql(chunks[i]);
}
}
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 HypotheticalSetWindowEmulation method generateSqmOrderedSetAggregateFunctionExpression.
@Override
public <T> SelfRenderingSqmOrderedSetAggregateFunction<T> generateSqmOrderedSetAggregateFunctionExpression(List<? extends SqmTypedNode<?>> arguments, SqmPredicate filter, SqmOrderByClause withinGroupClause, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
return new SelfRenderingSqmOrderedSetAggregateFunction<>(this, this, arguments, filter, withinGroupClause, impliedResultType, getArgumentsValidator(), getReturnTypeResolver(), queryEngine.getCriteriaBuilder(), getName()) {
@Override
public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
final Clause currentClause = walker.getCurrentClauseStack().getCurrent();
if (currentClause == Clause.OVER) {
return super.convertToSqlAst(walker);
} else if (currentClause != Clause.SELECT) {
throw new IllegalArgumentException("Can't emulate [" + getName() + "] in clause " + currentClause + ". Only the SELECT clause is supported!");
}
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.getWithinGroup() == null) {
withinGroup = Collections.emptyList();
} else {
walker.getCurrentClauseStack().push(Clause.ORDER);
try {
final List<SqmSortSpecification> sortSpecifications = this.getWithinGroup().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();
}
}
final SelfRenderingFunctionSqlAstExpression function = new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), Collections.emptyList(), getFilter() == null ? null : (Predicate) getFilter().accept(walker), Collections.emptyList(), resultType, getMappingModelExpressible(walker, resultType));
final Over<Object> windowFunction = new Over<>(function, new ArrayList<>(), withinGroup);
walker.registerQueryTransformer(new AggregateWindowEmulationQueryTransformer(windowFunction, withinGroup, arguments));
return windowFunction;
}
};
}
use of org.hibernate.sql.ast.tree.SqlAstNode 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();
}
}
use of org.hibernate.sql.ast.tree.SqlAstNode in project hibernate-orm by hibernate.
the class DerbyRpadEmulation method render.
@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> arguments, SqlAstTranslator<?> walker) {
final SqlAstNode string = arguments.get(0);
final SqlAstNode length = arguments.get(1);
sqlAppender.appendSql("case when length(");
walker.render(string, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER);
sqlAppender.appendSql(")<");
walker.render(length, SqlAstNodeRenderingMode.DEFAULT);
sqlAppender.appendSql(" then substr(");
walker.render(string, SqlAstNodeRenderingMode.DEFAULT);
sqlAppender.appendSql("||char('',");
// The char function for Derby always needs a literal value
walker.render(length, SqlAstNodeRenderingMode.INLINE_PARAMETERS);
sqlAppender.appendSql("),1,");
walker.render(length, SqlAstNodeRenderingMode.DEFAULT);
sqlAppender.appendSql(") else ");
walker.render(string, SqlAstNodeRenderingMode.DEFAULT);
sqlAppender.appendSql(" end");
}
Aggregations