Search in sources :

Example 1 with AbstractCustomQuery

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

the class BaseFinalSetOperationBuilderImpl method getTypedQuery.

@Override
@SuppressWarnings("unchecked")
protected TypedQuery<T> getTypedQuery(StringBuilder lateralSb, JoinNode lateralJoinNode) {
    if (lateralSb != null) {
        throw new IllegalStateException("Lateral join with set operations is not yet supported!");
    }
    Set<String> parameterListNames = new HashSet<String>();
    Query leftMostQuery = setOperationManager.getStartQueryBuilder().getTypedQueryForFinalOperationBuilder();
    Query baseQuery;
    parameterManager.collectParameterListNames(leftMostQuery, parameterListNames);
    Query q = leftMostQuery;
    if (leftMostQuery instanceof TypedQueryWrapper<?>) {
        q = ((TypedQueryWrapper<?>) leftMostQuery).getDelegate();
    }
    if (q instanceof AbstractCustomQuery<?>) {
        AbstractCustomQuery<?> customQuery = (AbstractCustomQuery<?>) q;
        List<Query> customQueryParticipants = customQuery.getParticipatingQueries();
        baseQuery = customQueryParticipants.get(0);
    } else {
        baseQuery = q;
    }
    List<Query> setOperands = new ArrayList<Query>();
    for (AbstractCommonQueryBuilder<?, ?, ?, ?, ?> setOperand : setOperationManager.getSetOperations()) {
        q = setOperand.getQuery();
        setOperands.add(q);
        parameterManager.collectParameterListNames(q, parameterListNames);
    }
    String limit = null;
    String offset = null;
    // Main query will get the limit applied by the native mechanism
    if (!isMainQuery) {
        if (firstResult != 0) {
            offset = Integer.toString(firstResult);
        }
        if (maxResults != Integer.MAX_VALUE) {
            limit = Integer.toString(maxResults);
        }
    }
    // Since this builder has no query of it's own, there can be no joins
    List<String> keyRestrictedLeftJoinAliases = Collections.emptyList();
    List<EntityFunctionNode> entityFunctionNodes = getEntityFunctionNodes(baseQuery);
    boolean shouldRenderCteNodes = renderCteNodes(false);
    List<CTENode> ctes = shouldRenderCteNodes ? getCteNodes(false) : Collections.EMPTY_LIST;
    QuerySpecification querySpecification = new SetOperationQuerySpecification(this, leftMostQuery, baseQuery, setOperands, setOperationManager.getOperator(), getOrderByElements(), setOperationManager.isNested(), parameterManager.getParameterImpls(), parameterListNames, limit, offset, keyRestrictedLeftJoinAliases, entityFunctionNodes, mainQuery.cteManager.isRecursive(), ctes, shouldRenderCteNodes, mainQuery.getQueryConfiguration().isQueryPlanCacheEnabled());
    // Unfortunately we need this little adapter here
    @SuppressWarnings("rawtypes") TypedQuery<T> query = new CustomSQLTypedQuery<T>(querySpecification, baseQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
    // The main query will use the native mechanism for limit/offset
    if (isMainQuery) {
        if (firstResult != 0) {
            query.setFirstResult(firstResult);
        }
        if (maxResults != Integer.MAX_VALUE) {
            query.setMaxResults(maxResults);
        }
    }
    parameterManager.parameterizeQuery(query);
    return applyObjectBuilder(query);
}
Also used : AbstractCustomQuery(com.blazebit.persistence.impl.query.AbstractCustomQuery) TypedQuery(javax.persistence.TypedQuery) CustomSQLTypedQuery(com.blazebit.persistence.impl.query.CustomSQLTypedQuery) Query(javax.persistence.Query) AbstractCustomQuery(com.blazebit.persistence.impl.query.AbstractCustomQuery) ArrayList(java.util.ArrayList) QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) SetOperationQuerySpecification(com.blazebit.persistence.impl.query.SetOperationQuerySpecification) CustomSQLTypedQuery(com.blazebit.persistence.impl.query.CustomSQLTypedQuery) EntityFunctionNode(com.blazebit.persistence.impl.query.EntityFunctionNode) CTENode(com.blazebit.persistence.impl.query.CTENode) SetOperationQuerySpecification(com.blazebit.persistence.impl.query.SetOperationQuerySpecification) HashSet(java.util.HashSet) TypedQueryWrapper(com.blazebit.persistence.impl.query.TypedQueryWrapper)

Aggregations

AbstractCustomQuery (com.blazebit.persistence.impl.query.AbstractCustomQuery)1 CTENode (com.blazebit.persistence.impl.query.CTENode)1 CustomSQLTypedQuery (com.blazebit.persistence.impl.query.CustomSQLTypedQuery)1 EntityFunctionNode (com.blazebit.persistence.impl.query.EntityFunctionNode)1 QuerySpecification (com.blazebit.persistence.impl.query.QuerySpecification)1 SetOperationQuerySpecification (com.blazebit.persistence.impl.query.SetOperationQuerySpecification)1 TypedQueryWrapper (com.blazebit.persistence.impl.query.TypedQueryWrapper)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1