Search in sources :

Example 1 with SimplePathReference

use of com.blazebit.persistence.impl.SimplePathReference in project blaze-persistence by Blazebit.

the class SizeTransformationVisitor method generateSubquery.

private SubqueryExpression generateSubquery(PathExpression sizeArg) {
    JoinNode sizeArgJoin = (JoinNode) sizeArg.getBaseNode();
    final Type<?> nodeType = sizeArgJoin.getNodeType();
    if (!(nodeType instanceof EntityType<?>)) {
        throw new IllegalArgumentException("Size on a collection owned by a non-entity type is not supported yet: " + sizeArg);
    }
    final EntityType<?> startType = (EntityType<?>) nodeType;
    Subquery countSubquery = (Subquery) subqueryInitFactory.createSubqueryInitiator(null, new SubqueryBuilderListenerImpl<>(), false, getClause()).from(sizeArg.getPathReference().toString()).select("COUNT(*)");
    for (SingularAttribute<?, ?> idAttribute : JpaMetamodelUtils.getIdAttributes(startType)) {
        String groupByExprString = sizeArgJoin.getAlias() + "." + idAttribute.getName();
        ResolvedExpression groupByExpr = new ResolvedExpression(groupByExprString, null);
        Set<ClauseType> clauseTypes = subqueryGroupBys.get(groupByExpr);
        if (clauseTypes == null) {
            List<PathElementExpression> pathElementExpressions = new ArrayList<>(2);
            pathElementExpressions.add(new PropertyExpression(sizeArgJoin.getAlias()));
            pathElementExpressions.add(new PropertyExpression(idAttribute.getName()));
            PathExpression pathExpression = new PathExpression(pathElementExpressions);
            pathExpression.setPathReference(new SimplePathReference(sizeArgJoin, idAttribute.getName(), metamodel.type(JpaMetamodelUtils.resolveFieldClass(startType.getJavaType(), idAttribute))));
            groupByExpr = new ResolvedExpression(groupByExprString, pathExpression);
            subqueryGroupBys.put(groupByExpr, EnumSet.of(clause));
        } else {
            clauseTypes.add(clause);
        }
    }
    return new SubqueryExpression(countSubquery);
}
Also used : JoinNode(com.blazebit.persistence.impl.JoinNode) SubqueryBuilderListenerImpl(com.blazebit.persistence.impl.SubqueryBuilderListenerImpl) ResolvedExpression(com.blazebit.persistence.impl.ResolvedExpression) ArrayList(java.util.ArrayList) Subquery(com.blazebit.persistence.parser.expression.Subquery) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) EntityType(javax.persistence.metamodel.EntityType) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) SimplePathReference(com.blazebit.persistence.impl.SimplePathReference) ClauseType(com.blazebit.persistence.impl.ClauseType)

Aggregations

ClauseType (com.blazebit.persistence.impl.ClauseType)1 JoinNode (com.blazebit.persistence.impl.JoinNode)1 ResolvedExpression (com.blazebit.persistence.impl.ResolvedExpression)1 SimplePathReference (com.blazebit.persistence.impl.SimplePathReference)1 SubqueryBuilderListenerImpl (com.blazebit.persistence.impl.SubqueryBuilderListenerImpl)1 PathElementExpression (com.blazebit.persistence.parser.expression.PathElementExpression)1 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)1 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)1 Subquery (com.blazebit.persistence.parser.expression.Subquery)1 SubqueryExpression (com.blazebit.persistence.parser.expression.SubqueryExpression)1 ArrayList (java.util.ArrayList)1 EntityType (javax.persistence.metamodel.EntityType)1