Search in sources :

Example 1 with QueryWrapper

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

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

the class AbstractModificationCriteriaBuilder method getQuery.

@Override
protected Query getQuery(Map<DbmsModificationState, String> includedModificationStates) {
    Query query;
    // We use this to make these features only available to Hibernate as it is the only provider that supports sql replace yet
    if (hasLimit() || mainQuery.cteManager.hasCtes() || returningAttributeBindingMap.size() > 0) {
        // We need to change the underlying sql when doing a limit with hibernate since it does not support limiting insert ... select statements
        // For CTEs we will also need to change the underlying sql
        query = em.createQuery(getBaseQueryStringWithCheck(null, null));
        Set<String> parameterListNames = parameterManager.getParameterListNames(query);
        boolean isEmbedded = this instanceof ReturningBuilder;
        String[] returningColumns = getReturningColumns();
        boolean shouldRenderCteNodes = renderCteNodes(isEmbedded);
        List<CTENode> ctes = shouldRenderCteNodes ? getCteNodes(isEmbedded) : Collections.EMPTY_LIST;
        QuerySpecification querySpecification = new ModificationQuerySpecification(this, query, getCountExampleQuery(), parameterManager.getParameterImpls(), parameterListNames, mainQuery.cteManager.isRecursive(), ctes, shouldRenderCteNodes, isEmbedded, returningColumns, null, includedModificationStates, returningAttributeBindingMap, mainQuery.getQueryConfiguration().isQueryPlanCacheEnabled());
        query = new CustomSQLQuery(querySpecification, query, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
    } else {
        query = em.createQuery(getBaseQueryStringWithCheck(null, null));
        if (parameterManager.getCriteriaNameMapping() != null) {
            query = new QueryWrapper(query, parameterManager.getCriteriaNameMapping());
        }
    }
    parameterManager.parameterizeQuery(query);
    return query;
}
Also used : ReturningBuilder(com.blazebit.persistence.ReturningBuilder) SimpleReturningBuilder(com.blazebit.persistence.SimpleReturningBuilder) QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) ModificationQuerySpecification(com.blazebit.persistence.impl.query.ModificationQuerySpecification) 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) QueryWrapper(com.blazebit.persistence.impl.query.QueryWrapper) CTENode(com.blazebit.persistence.impl.query.CTENode) ModificationQuerySpecification(com.blazebit.persistence.impl.query.ModificationQuerySpecification)

Aggregations

CustomSQLQuery (com.blazebit.persistence.impl.query.CustomSQLQuery)2 QuerySpecification (com.blazebit.persistence.impl.query.QuerySpecification)2 QueryWrapper (com.blazebit.persistence.impl.query.QueryWrapper)2 Query (javax.persistence.Query)2 ReturningBuilder (com.blazebit.persistence.ReturningBuilder)1 SimpleReturningBuilder (com.blazebit.persistence.SimpleReturningBuilder)1 CTENode (com.blazebit.persistence.impl.query.CTENode)1 CTEQuerySpecification (com.blazebit.persistence.impl.query.CTEQuerySpecification)1 CustomReturningSQLTypedQuery (com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)1 EntityFunctionNode (com.blazebit.persistence.impl.query.EntityFunctionNode)1 ModificationQuerySpecification (com.blazebit.persistence.impl.query.ModificationQuerySpecification)1 TypedQuery (javax.persistence.TypedQuery)1