Search in sources :

Example 11 with SqlAstNode

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));
}
Also used : BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) JdbcMappingContainer(org.hibernate.metamodel.mapping.JdbcMappingContainer) QueryException(org.hibernate.QueryException) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode)

Example 12 with SqlAstNode

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();
    }
}
Also used : Distinct(org.hibernate.sql.ast.tree.expression.Distinct) Star(org.hibernate.sql.ast.tree.expression.Star) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode)

Example 13 with SqlAstNode

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;
        }
    };
}
Also used : SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) Over(org.hibernate.sql.ast.tree.expression.Over) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression) Clause(org.hibernate.sql.ast.Clause) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) ArgumentsValidator(org.hibernate.query.sqm.produce.function.ArgumentsValidator) SelfRenderingSqmOrderedSetAggregateFunction(org.hibernate.query.sqm.function.SelfRenderingSqmOrderedSetAggregateFunction)

Example 14 with SqlAstNode

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

Example 15 with SqlAstNode

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");
}
Also used : SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode)

Aggregations

SqlAstNode (org.hibernate.sql.ast.tree.SqlAstNode)23 Expression (org.hibernate.sql.ast.tree.expression.Expression)9 Distinct (org.hibernate.sql.ast.tree.expression.Distinct)6 ArgumentsValidator (org.hibernate.query.sqm.produce.function.ArgumentsValidator)5 ArrayList (java.util.ArrayList)4 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)4 SortSpecification (org.hibernate.sql.ast.tree.select.SortSpecification)4 JdbcMappingContainer (org.hibernate.metamodel.mapping.JdbcMappingContainer)3 SqmToSqlAstConverter (org.hibernate.query.sqm.sql.SqmToSqlAstConverter)3 SqmSortSpecification (org.hibernate.query.sqm.tree.select.SqmSortSpecification)3 Over (org.hibernate.sql.ast.tree.expression.Over)3 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)3 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)3 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)3 List (java.util.List)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)2 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)2 SelfRenderingOrderedSetAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2