Search in sources :

Example 1 with TypedQueryWrapper

use of com.blazebit.persistence.impl.query.TypedQueryWrapper 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)

Example 2 with TypedQueryWrapper

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

the class PaginatedCriteriaBuilderImpl method getObjectQuery.

@SuppressWarnings("unchecked")
private Map.Entry<TypedQuery<T>, ObjectBuilder<T>> getObjectQuery(boolean normalQueryMode, Set<JoinNode> keyRestrictedLeftJoins, List<JoinNode> entityFunctions) {
    String queryString = getBaseQueryString(null, null);
    Class<?> expectedResultType;
    // When the keyset is included the query obviously produces an array
    if (keysetExtraction || firstResult < maximumCount && withCountQuery && withInlineCountQuery) {
        expectedResultType = Object[].class;
    } else {
        expectedResultType = selectManager.getExpectedQueryResultType();
    }
    Set<ClauseType> clauseExclusions;
    if (isWithInlineIdQuery() && (hasCollections || withForceIdQuery)) {
        clauseExclusions = OBJECT_QUERY_CLAUSE_EXCLUSIONS;
    } else if (hasGroupBy) {
        clauseExclusions = NO_CLAUSE_EXCLUSION;
    } else {
        clauseExclusions = OBJECT_QUERY_WITHOUT_GROUP_BY_EXCLUSIONS;
    }
    TypedQuery<T> query;
    if (normalQueryMode && isEmpty(keyRestrictedLeftJoins, clauseExclusions)) {
        query = (TypedQuery<T>) em.createQuery(queryString, expectedResultType);
        if (isCacheable()) {
            mainQuery.jpaProvider.setCacheable(query);
        }
        boolean externalIdQuery = !isWithInlineIdQuery() && (hasCollections || withForceIdQuery);
        if (!externalIdQuery && firstResult < maximumCount && withCountQuery && withInlineCountQuery && maximumCount != Long.MAX_VALUE) {
            parameterManager.parameterizeQuery(query, getDualNodeAlias());
            query.setParameter(getDualNodeAlias() + "_value_0", 0L);
        } else {
            parameterManager.parameterizeQuery(query);
        }
    } else {
        TypedQuery<T> baseQuery = (TypedQuery<T>) em.createQuery(queryString, expectedResultType);
        Set<String> parameterListNames = parameterManager.getParameterListNames(baseQuery);
        List<String> keyRestrictedLeftJoinAliases = getKeyRestrictedLeftJoinAliases(baseQuery, keyRestrictedLeftJoins, clauseExclusions);
        List<EntityFunctionNode> entityFunctionNodes = getEntityFunctionNodes(baseQuery, entityFunctions);
        boolean shouldRenderCteNodes = renderCteNodes(false);
        List<CTENode> ctes = shouldRenderCteNodes ? getCteNodes(false) : Collections.EMPTY_LIST;
        Collection<Parameter<?>> parameters;
        Map<String, String> valuesParameters = parameterManager.getValuesParameters();
        Map<String, ValuesParameterBinder> valuesBinders = parameterManager.getValuesBinders();
        boolean externalIdQuery = !isWithInlineIdQuery() && (hasCollections || withForceIdQuery);
        JoinNode dualNode = null;
        if (!externalIdQuery && firstResult < maximumCount && withCountQuery && withInlineCountQuery && maximumCount != Long.MAX_VALUE) {
            parameters = new ArrayList<>(parameterManager.getParameters());
            dualNode = createDualNode();
            String valueParameterName = dualNode.getAlias() + "_value_0";
            String[][] parameterNames = new String[1][1];
            parameterNames[0][0] = valueParameterName;
            ParameterManager.ValuesParameterWrapper valuesParameterWrapper = new ParameterManager.ValuesParameterWrapper(dualNode.getJavaType(), parameterNames, new AttributeAccessor[1]);
            parameters.add(new ParameterManager.ParameterImpl<Object>(dualNode.getAlias(), false, null, null, valuesParameterWrapper));
            valuesParameters = new HashMap<>(valuesParameters);
            valuesParameters.put(valueParameterName, dualNode.getAlias());
            valuesBinders.put(dualNode.getAlias(), valuesParameterWrapper.getBinder());
        } else {
            parameters = (Collection<Parameter<?>>) (Collection<?>) parameterManager.getParameterImpls();
        }
        QuerySpecification querySpecification = new CustomQuerySpecification(this, baseQuery, parameters, parameterListNames, null, null, keyRestrictedLeftJoinAliases, entityFunctionNodes, mainQuery.cteManager.isRecursive(), ctes, shouldRenderCteNodes, mainQuery.getQueryConfiguration().isQueryPlanCacheEnabled(), null);
        query = new CustomSQLTypedQuery<T>(querySpecification, baseQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), valuesParameters, valuesBinders);
        if (dualNode == null) {
            parameterManager.parameterizeQuery(query);
        } else {
            parameterManager.parameterizeQuery(query, dualNode.getAlias());
            query.setParameter(dualNode.getAlias(), Collections.singleton(0L));
        }
    }
    ObjectBuilder<T> objectBuilder = null;
    ObjectBuilder<T> transformerObjectBuilder = selectManager.getSelectObjectBuilder();
    boolean inlinedCountQuery = firstResult < maximumCount && withCountQuery && withInlineCountQuery;
    if (keysetExtraction) {
        if (transformerObjectBuilder == null) {
            objectBuilder = new KeysetExtractionObjectBuilder<T>(keysetToSelectIndexMapping, keysetMode, maxResults, highestOffset, selectManager.getExpectedQueryResultType() != Object[].class, withExtractAllKeysets, inlinedCountQuery);
        } else {
            objectBuilder = new DelegatingKeysetExtractionObjectBuilder<T>(transformerObjectBuilder, keysetToSelectIndexMapping, keysetMode, maxResults, highestOffset, withExtractAllKeysets, inlinedCountQuery);
        }
        transformerObjectBuilder = objectBuilder;
    } else if (inlinedCountQuery && transformerObjectBuilder != null) {
        transformerObjectBuilder = objectBuilder = new CountExtractionObjectBuilder<>(transformerObjectBuilder);
    }
    if (transformerObjectBuilder != null) {
        query = new ObjectBuilderTypedQuery<>(query, query instanceof AbstractCustomQuery<?> ? null : parameterManager.getCriteriaNameMapping(), transformerObjectBuilder);
    } else if (parameterManager.getCriteriaNameMapping() != null) {
        if (!(query instanceof CustomSQLTypedQuery<?>)) {
            query = new TypedQueryWrapper<>(query, parameterManager.getCriteriaNameMapping());
        }
    }
    return new AbstractMap.SimpleEntry<TypedQuery<T>, ObjectBuilder<T>>(query, objectBuilder);
}
Also used : CustomSQLTypedQuery(com.blazebit.persistence.impl.query.CustomSQLTypedQuery) ObjectBuilderTypedQuery(com.blazebit.persistence.impl.query.ObjectBuilderTypedQuery) TypedQuery(javax.persistence.TypedQuery) QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) CustomQuerySpecification(com.blazebit.persistence.impl.query.CustomQuerySpecification) EntityFunctionNode(com.blazebit.persistence.impl.query.EntityFunctionNode) CustomQuerySpecification(com.blazebit.persistence.impl.query.CustomQuerySpecification) CTENode(com.blazebit.persistence.impl.query.CTENode) TypedQueryWrapper(com.blazebit.persistence.impl.query.TypedQueryWrapper) Parameter(javax.persistence.Parameter) Collection(java.util.Collection)

Aggregations

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