Search in sources :

Example 1 with CustomSQLQuery

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

the class AbstractCommonQueryBuilder method getCteNodes.

protected List<CTENode> getCteNodes(boolean isSubquery) {
    List<CTENode> cteNodes = new ArrayList<CTENode>();
    // NOTE: Delete statements could cause CTEs to be generated for the cascading deletes
    if (!isMainQuery || isSubquery || !mainQuery.dbmsDialect.supportsWithClause() || !mainQuery.cteManager.hasCtes() && statementType != DbmsStatementType.DELETE || statementType != DbmsStatementType.SELECT && !mainQuery.dbmsDialect.supportsWithClauseInModificationQuery()) {
        return cteNodes;
    }
    StringBuilder sb = new StringBuilder();
    for (CTEInfo cteInfo : mainQuery.cteManager.getCtes()) {
        if (!cteInfo.inline) {
            // Build queries and add as participating queries
            Map<DbmsModificationState, String> modificationStates = cteInfo.nonRecursiveCriteriaBuilder.getModificationStates(explicitVersionEntities);
            Query nonRecursiveQuery = cteInfo.nonRecursiveCriteriaBuilder.getQuery(modificationStates);
            QuerySpecification<?> nonRecursiveQuerySpecification = getQuerySpecification(nonRecursiveQuery);
            Map<String, String> nonRecursiveTableNameRemappings = null;
            if (nonRecursiveQuery instanceof CustomSQLQuery) {
                // EntityAlias -> CteName
                nonRecursiveTableNameRemappings = cteInfo.nonRecursiveCriteriaBuilder.getModificationStateRelatedTableNameRemappings(explicitVersionEntities);
            }
            Query recursiveQuery;
            QuerySpecification<?> recursiveQuerySpecification = null;
            Map<String, String> recursiveTableNameRemappings = null;
            if (cteInfo.recursive) {
                modificationStates = cteInfo.nonRecursiveCriteriaBuilder.getModificationStates(explicitVersionEntities);
                recursiveQuery = cteInfo.recursiveCriteriaBuilder.getQuery(modificationStates);
                if (!mainQuery.dbmsDialect.supportsJoinsInRecursiveCte() && cteInfo.recursiveCriteriaBuilder.joinManager.hasNonEmulatableJoins()) {
                    throw new IllegalStateException("The dbms dialect does not support joins in the recursive part of a CTE!");
                }
                recursiveQuerySpecification = getQuerySpecification(recursiveQuery);
                if (recursiveQuery instanceof CustomSQLQuery) {
                    // EntityAlias -> CteName
                    recursiveTableNameRemappings = cteInfo.recursiveCriteriaBuilder.getModificationStateRelatedTableNameRemappings(explicitVersionEntities);
                }
            }
            String cteName = cteInfo.cteType.getName();
            final List<String> columnNames = cteInfo.columnNames;
            String head;
            String[] aliases;
            if (mainQuery.dbmsDialect.supportsWithClauseHead()) {
                sb.setLength(0);
                sb.append(cteName);
                sb.append('(');
                for (int i = 0; i < columnNames.size(); i++) {
                    String column = columnNames.get(i);
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(column);
                }
                sb.append(')');
                head = sb.toString();
                aliases = null;
            } else {
                sb.setLength(0);
                sb.append(cteName);
                List<String> list = new ArrayList<>(columnNames.size());
                for (int i = 0; i < columnNames.size(); i++) {
                    String[] columns = mainQuery.metamodel.getManagedType(ExtendedManagedType.class, cteInfo.cteType.getJavaType()).getAttribute(columnNames.get(i)).getColumnNames();
                    for (String column : columns) {
                        list.add(column);
                    }
                }
                head = sb.toString();
                aliases = list.toArray(new String[list.size()]);
            }
            String nonRecursiveWithClauseSuffix = null;
            if (!cteInfo.recursive && !mainQuery.dbmsDialect.supportsNonRecursiveWithClause()) {
                sb.setLength(0);
                sb.append(" UNION ALL ");
                sb.append("SELECT ");
                sb.append("NULL");
                for (int i = 1; i < columnNames.size(); i++) {
                    sb.append(", ");
                    sb.append("NULL");
                }
                sb.append(" FROM DUAL WHERE 1=0");
                nonRecursiveWithClauseSuffix = sb.toString();
            }
            cteNodes.add(new CTENode(cteInfo.name, cteInfo.cteType.getName(), head, aliases, cteInfo.unionAll, nonRecursiveQuerySpecification, recursiveQuerySpecification, nonRecursiveTableNameRemappings, recursiveTableNameRemappings, nonRecursiveWithClauseSuffix));
        }
    }
    return cteNodes;
}
Also used : DbmsModificationState(com.blazebit.persistence.spi.DbmsModificationState) CustomSQLTypedQuery(com.blazebit.persistence.impl.query.CustomSQLTypedQuery) AbstractCustomQuery(com.blazebit.persistence.impl.query.AbstractCustomQuery) TypedQuery(javax.persistence.TypedQuery) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery) Query(javax.persistence.Query) ObjectBuilderTypedQuery(com.blazebit.persistence.impl.query.ObjectBuilderTypedQuery) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery) ArrayList(java.util.ArrayList) CTENode(com.blazebit.persistence.impl.query.CTENode)

Example 2 with CustomSQLQuery

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

the class AbstractDeleteCollectionCriteriaBuilder method getQuery.

@Override
protected Query getQuery(Map<DbmsModificationState, String> includedModificationStates) {
    if (collectionAttribute.getJoinTable() == null) {
        return super.getQuery(includedModificationStates);
    } else {
        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;
    }
}
Also used : QuerySpecification(com.blazebit.persistence.impl.query.QuerySpecification) CollectionDeleteModificationQuerySpecification(com.blazebit.persistence.impl.query.CollectionDeleteModificationQuerySpecification) 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 3 with CustomSQLQuery

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

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

Example 5 with CustomSQLQuery

use of com.blazebit.persistence.impl.query.CustomSQLQuery 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)9 Query (javax.persistence.Query)9 QuerySpecification (com.blazebit.persistence.impl.query.QuerySpecification)8 TypedQuery (javax.persistence.TypedQuery)7 CustomReturningSQLTypedQuery (com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)6 CTENode (com.blazebit.persistence.impl.query.CTENode)3 ReturningBuilder (com.blazebit.persistence.ReturningBuilder)2 EntityFunctionNode (com.blazebit.persistence.impl.query.EntityFunctionNode)2 ModificationQuerySpecification (com.blazebit.persistence.impl.query.ModificationQuerySpecification)2 QueryWrapper (com.blazebit.persistence.impl.query.QueryWrapper)2 SimpleReturningBuilder (com.blazebit.persistence.SimpleReturningBuilder)1 AbstractCustomQuery (com.blazebit.persistence.impl.query.AbstractCustomQuery)1 CTEQuerySpecification (com.blazebit.persistence.impl.query.CTEQuerySpecification)1 CollectionDeleteModificationQuerySpecification (com.blazebit.persistence.impl.query.CollectionDeleteModificationQuerySpecification)1 CollectionInsertModificationQuerySpecification (com.blazebit.persistence.impl.query.CollectionInsertModificationQuerySpecification)1 CollectionUpdateModificationQuerySpecification (com.blazebit.persistence.impl.query.CollectionUpdateModificationQuerySpecification)1 CustomSQLTypedQuery (com.blazebit.persistence.impl.query.CustomSQLTypedQuery)1 DeleteModificationQuerySpecification (com.blazebit.persistence.impl.query.DeleteModificationQuerySpecification)1 ObjectBuilderTypedQuery (com.blazebit.persistence.impl.query.ObjectBuilderTypedQuery)1 UpdateModificationQuerySpecification (com.blazebit.persistence.impl.query.UpdateModificationQuerySpecification)1