Search in sources :

Example 6 with QuerySpecification

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

the class AbstractModificationCriteriaBuilder method getExecuteWithReturningQuery.

protected <R> TypedQuery<ReturningResult<R>> getExecuteWithReturningQuery(TypedQuery<Object[]> exampleQuery, Query baseQuery, String[] returningColumns, ReturningObjectBuilder<R> objectBuilder) {
    Set<String> parameterListNames = parameterManager.getParameterListNames(baseQuery);
    boolean shouldRenderCteNodes = renderCteNodes(false);
    List<CTENode> ctes = shouldRenderCteNodes ? getCteNodes(false) : Collections.EMPTY_LIST;
    QuerySpecification querySpecification = new ModificationQuerySpecification(this, baseQuery, exampleQuery, parameterManager.getParameterImpls(), parameterListNames, mainQuery.cteManager.isRecursive(), ctes, shouldRenderCteNodes, false, returningColumns, objectBuilder, null, returningAttributeBindingMap, mainQuery.getQueryConfiguration().isQueryPlanCacheEnabled());
    CustomReturningSQLTypedQuery 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) ModificationQuerySpecification(com.blazebit.persistence.impl.query.ModificationQuerySpecification) CustomReturningSQLTypedQuery(com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery) CTENode(com.blazebit.persistence.impl.query.CTENode) ModificationQuerySpecification(com.blazebit.persistence.impl.query.ModificationQuerySpecification)

Example 7 with QuerySpecification

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

the class AbstractUpdateCollectionCriteriaBuilder method getExecuteWithReturningQuery.

@Override
protected <R> TypedQuery<ReturningResult<R>> getExecuteWithReturningQuery(TypedQuery<Object[]> exampleQuery, Query baseQuery, String[] returningColumns, ReturningObjectBuilder<R> objectBuilder) {
    if (collectionAttribute.getJoinTable() == null) {
        return super.getExecuteWithReturningQuery(exampleQuery, baseQuery, returningColumns, objectBuilder);
    } else {
        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);
        return query;
    }
}
Also used : QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) CollectionUpdateModificationQuerySpecification(com.blazebit.persistence.impl.query.CollectionUpdateModificationQuerySpecification) CustomReturningSQLTypedQuery(com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)

Example 8 with QuerySpecification

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

the class AbstractCTECriteriaBuilder method getQuery.

protected Query getQuery(String baseQueryString) {
    Set<JoinNode> keyRestrictedLeftJoins = getKeyRestrictedLeftJoins();
    Query query;
    if (hasLimit() || joinManager.hasEntityFunctions() || !keyRestrictedLeftJoins.isEmpty()) {
        // We need to change the underlying sql when doing a limit
        query = em.createQuery(baseQueryString);
        Set<String> parameterListNames = parameterManager.getParameterListNames(query);
        String limit = null;
        String offset = null;
        // The main query will handle that separately
        if (!isMainQuery) {
            if (firstResult != 0) {
                query.setFirstResult(firstResult);
                offset = Integer.toString(firstResult);
            }
            if (maxResults != Integer.MAX_VALUE) {
                query.setMaxResults(maxResults);
                limit = Integer.toString(maxResults);
            }
        }
        List<String> keyRestrictedLeftJoinAliases = getKeyRestrictedLeftJoinAliases(query, keyRestrictedLeftJoins, Collections.<ClauseType>emptySet());
        List<EntityFunctionNode> entityFunctionNodes = getEntityFunctionNodes(query);
        QuerySpecification querySpecification = new CTEQuerySpecification(this, query, parameterManager.getParameterImpls(), parameterListNames, limit, offset, keyRestrictedLeftJoinAliases, entityFunctionNodes);
        query = new CustomSQLQuery(querySpecification, query, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
    } else {
        query = em.createQuery(baseQueryString);
        if (parameterManager.getCriteriaNameMapping() != null) {
            query = new QueryWrapper(query, parameterManager.getCriteriaNameMapping());
        }
    }
    parameterManager.parameterizeQuery(query);
    return query;
}
Also used : CTEQuerySpecification(com.blazebit.persistence.impl.query.CTEQuerySpecification) QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) Query(javax.persistence.Query) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery) QueryWrapper(com.blazebit.persistence.impl.query.QueryWrapper) CTEQuerySpecification(com.blazebit.persistence.impl.query.CTEQuerySpecification) EntityFunctionNode(com.blazebit.persistence.impl.query.EntityFunctionNode)

Example 9 with QuerySpecification

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

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

the class BaseUpdateCriteriaBuilderImpl method getQuery.

@Override
protected Query getQuery(Map<DbmsModificationState, String> includedModificationStates) {
    prepareAndCheck();
    JoinNode rootNode = joinManager.getRoots().get(0);
    if (joinManager.getRoots().size() > 1 || rootNode.hasChildNodes()) {
        // Prefer an exists subquery instead of MERGE
        if (mainQuery.dbmsDialect.getUpdateJoinStyle() == UpdateJoinStyle.NONE) {
            return super.getQuery(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);
        return query;
    } else {
        return super.getQuery(includedModificationStates);
    }
}
Also used : QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) UpdateModificationQuerySpecification(com.blazebit.persistence.impl.query.UpdateModificationQuerySpecification) 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)

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