Search in sources :

Example 16 with QuerySpecification

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

the class BaseDeleteCriteriaBuilderImpl method getExecuteWithReturningQuery.

@Override
protected <R> TypedQuery<ReturningResult<R>> getExecuteWithReturningQuery(TypedQuery<Object[]> exampleQuery, Query baseQuery, String[] returningColumns, ReturningObjectBuilder<R> objectBuilder) {
    QuerySpecification querySpecification = getQuerySpecification(baseQuery, exampleQuery, returningColumns, objectBuilder, null);
    CustomReturningSQLTypedQuery<R> query = new CustomReturningSQLTypedQuery<R>(querySpecification, exampleQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);
    parameterManager.parameterizeQuery(query);
    return query;
}
Also used : QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) DeleteModificationQuerySpecification(com.blazebit.persistence.impl.query.DeleteModificationQuerySpecification) CustomReturningSQLTypedQuery(com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)

Example 17 with QuerySpecification

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

the class AbstractInsertCollectionCriteriaBuilder method getExecuteWithReturningQuery.

@Override
protected <R> TypedQuery<ReturningResult<R>> getExecuteWithReturningQuery(TypedQuery<Object[]> exampleQuery, Query baseQuery, String[] returningColumns, ReturningObjectBuilder<R> objectBuilder) {
    QuerySpecification querySpecification = getQuerySpecification(baseQuery, exampleQuery, returningColumns, objectBuilder, null);
    CustomReturningSQLTypedQuery query = new CustomReturningSQLTypedQuery<R>(querySpecification, exampleQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
    parameterManager.parameterizeQuery(query);
    baseQuery.setFirstResult(firstResult);
    baseQuery.setMaxResults(maxResults);
    return query;
}
Also used : QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) CollectionInsertModificationQuerySpecification(com.blazebit.persistence.impl.query.CollectionInsertModificationQuerySpecification) CustomReturningSQLTypedQuery(com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)

Example 18 with QuerySpecification

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

the class AbstractInsertCollectionCriteriaBuilder method getQuery.

@Override
protected Query getQuery(Map<DbmsModificationState, String> includedModificationStates) {
    Query baseQuery = em.createQuery(getBaseQueryStringWithCheck(null, null));
    QuerySpecification querySpecification = getQuerySpecification(baseQuery, getCountExampleQuery(), getReturningColumns(), null, includedModificationStates);
    CustomSQLQuery query = new CustomSQLQuery(querySpecification, baseQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
    parameterManager.parameterizeQuery(query);
    baseQuery.setFirstResult(firstResult);
    baseQuery.setMaxResults(maxResults);
    return query;
}
Also used : QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) CollectionInsertModificationQuerySpecification(com.blazebit.persistence.impl.query.CollectionInsertModificationQuerySpecification) TypedQuery(javax.persistence.TypedQuery) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery) Query(javax.persistence.Query) CustomReturningSQLTypedQuery(com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery)

Example 19 with QuerySpecification

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

Example 20 with QuerySpecification

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

the class PaginatedCriteriaBuilderImpl method getCountQuery.

private <X> TypedQuery<X> getCountQuery(String countQueryString, Class<X> resultType, boolean normalQueryMode, Set<JoinNode> keyRestrictedLeftJoins, List<JoinNode> entityFunctions, JoinNode dualNode) {
    if (normalQueryMode && isEmpty(keyRestrictedLeftJoins, COUNT_QUERY_CLAUSE_EXCLUSIONS)) {
        TypedQuery<X> countQuery = em.createQuery(countQueryString, resultType);
        if (isCacheable()) {
            mainQuery.jpaProvider.setCacheable(countQuery);
        }
        parameterManager.parameterizeQuery(countQuery);
        return parameterManager.getCriteriaNameMapping() == null ? countQuery : new TypedQueryWrapper<>(countQuery, parameterManager.getCriteriaNameMapping());
    }
    TypedQuery<X> baseQuery = em.createQuery(countQueryString, resultType);
    Set<String> parameterListNames = parameterManager.getParameterListNames(baseQuery);
    List<String> keyRestrictedLeftJoinAliases = getKeyRestrictedLeftJoinAliases(baseQuery, keyRestrictedLeftJoins, COUNT_QUERY_CLAUSE_EXCLUSIONS);
    List<EntityFunctionNode> entityFunctionNodes;
    if (dualNode == null) {
        entityFunctionNodes = getEntityFunctionNodes(baseQuery, entityFunctions);
    } else {
        entityFunctionNodes = getEntityFunctionNodes(baseQuery, entityFunctions, Collections.<JoinNode>emptyList(), false);
    }
    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();
    if (dualNode == null) {
        parameters = (Collection<Parameter<?>>) (Collection<?>) parameterManager.getParameterImpls();
    } else {
        parameters = new ArrayList<>(parameterManager.getParameters());
        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());
    }
    QuerySpecification querySpecification = new CustomQuerySpecification(this, baseQuery, parameters, parameterListNames, null, null, keyRestrictedLeftJoinAliases, entityFunctionNodes, mainQuery.cteManager.isRecursive(), ctes, shouldRenderCteNodes, mainQuery.getQueryConfiguration().isQueryPlanCacheEnabled(), null);
    CustomSQLTypedQuery<X> countQuery = new CustomSQLTypedQuery<X>(querySpecification, baseQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), valuesParameters, valuesBinders);
    if (dualNode == null) {
        parameterManager.parameterizeQuery(countQuery);
    } else {
        parameterManager.parameterizeQuery(countQuery, dualNode.getAlias());
        countQuery.setParameter(dualNode.getAlias(), Collections.singleton(0L));
    }
    return countQuery;
}
Also used : 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) CustomSQLTypedQuery(com.blazebit.persistence.impl.query.CustomSQLTypedQuery) Parameter(javax.persistence.Parameter) Collection(java.util.Collection)

Aggregations

QuerySpecification (com.blazebit.persistence.impl.query.QuerySpecification)21 CustomReturningSQLTypedQuery (com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)12 TypedQuery (javax.persistence.TypedQuery)11 CTENode (com.blazebit.persistence.impl.query.CTENode)10 Query (javax.persistence.Query)10 EntityFunctionNode (com.blazebit.persistence.impl.query.EntityFunctionNode)9 CustomSQLQuery (com.blazebit.persistence.impl.query.CustomSQLQuery)8 CustomSQLTypedQuery (com.blazebit.persistence.impl.query.CustomSQLTypedQuery)7 CustomQuerySpecification (com.blazebit.persistence.impl.query.CustomQuerySpecification)6 Collection (java.util.Collection)4 Parameter (javax.persistence.Parameter)4 ModificationQuerySpecification (com.blazebit.persistence.impl.query.ModificationQuerySpecification)3 ObjectBuilderTypedQuery (com.blazebit.persistence.impl.query.ObjectBuilderTypedQuery)3 ReturningBuilder (com.blazebit.persistence.ReturningBuilder)2 CollectionDeleteModificationQuerySpecification (com.blazebit.persistence.impl.query.CollectionDeleteModificationQuerySpecification)2 CollectionInsertModificationQuerySpecification (com.blazebit.persistence.impl.query.CollectionInsertModificationQuerySpecification)2 CollectionUpdateModificationQuerySpecification (com.blazebit.persistence.impl.query.CollectionUpdateModificationQuerySpecification)2 DeleteModificationQuerySpecification (com.blazebit.persistence.impl.query.DeleteModificationQuerySpecification)2 QueryWrapper (com.blazebit.persistence.impl.query.QueryWrapper)2 TypedQueryWrapper (com.blazebit.persistence.impl.query.TypedQueryWrapper)2