Search in sources :

Example 1 with CorrelatedSubqueryEmbeddingViewJpqlMacro

use of com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryEmbeddingViewJpqlMacro in project blaze-persistence by Blazebit.

the class AbstractCorrelatedBatchTupleListTransformer method applyAndGetCorrelationRoot.

private String applyAndGetCorrelationRoot(BatchCorrelationMode batchCorrelationMode) {
    Class<?> viewRootEntityClass = viewRootType.getEntityClass();
    Class<?> embeddingViewEntityClass = embeddingViewType.getEntityClass();
    String viewRootIdAttributePath = getEntityIdName(viewRootEntityClass);
    String embeddingViewIdAttributePath = getEntityIdName(embeddingViewEntityClass);
    FullQueryBuilder<?, ?> queryBuilder = entityViewConfiguration.getCriteriaBuilder();
    Map<String, Object> optionalParameters = entityViewConfiguration.getOptionalParameters();
    Class<?> correlationBasisEntityType;
    String viewRootExpression;
    String embeddingViewExpression;
    boolean batchedIdValues = false;
    if (batchCorrelationMode == BatchCorrelationMode.VALUES) {
        correlationBasisEntityType = correlationBasisEntity;
        viewRootExpression = null;
        batchedIdValues = correlatesThis && correlationBasisEntity == null;
        embeddingViewExpression = correlatesThis ? CORRELATION_KEY_ALIAS : null;
    } else if (batchCorrelationMode == BatchCorrelationMode.VIEW_ROOTS) {
        correlationBasisEntityType = viewRootEntityClass;
        viewRootExpression = CORRELATION_KEY_ALIAS;
        embeddingViewExpression = null;
    } else {
        correlationBasisEntityType = embeddingViewEntityClass;
        viewRootExpression = null;
        embeddingViewExpression = CORRELATION_KEY_ALIAS;
    }
    this.criteriaBuilder = queryBuilder.getCriteriaBuilderFactory().create(queryBuilder.getEntityManager(), Object[].class);
    if (queryBuilder instanceof CTEBuilder<?>) {
        this.criteriaBuilder.withCtesFrom((CTEBuilder<?>) queryBuilder);
    }
    this.viewRootJpqlMacro = new CorrelatedSubqueryViewRootJpqlMacro(criteriaBuilder, optionalParameters, viewRootExpression != null, viewRootEntityClass, viewRootIdAttributePath, viewRootExpression);
    this.embeddingViewJpqlMacro = new CorrelatedSubqueryEmbeddingViewJpqlMacro(criteriaBuilder, optionalParameters, embeddingViewExpression != null, embeddingViewEntityClass, embeddingViewIdAttributePath, embeddingViewExpression, batchedIdValues, viewRootJpqlMacro);
    this.criteriaBuilder.registerMacro("view", new MutableViewJpqlMacro(correlationResult));
    this.criteriaBuilder.registerMacro("view_root", viewRootJpqlMacro);
    this.criteriaBuilder.registerMacro("embedding_view", embeddingViewJpqlMacro);
    String joinBase = CORRELATION_KEY_ALIAS;
    SubqueryCorrelationBuilder correlationBuilder = new SubqueryCorrelationBuilder(queryBuilder, optionalParameters, criteriaBuilder, correlationAlias, correlationExternalAlias, correlationResult, correlationBasisType, correlationBasisEntityType, joinBase, attributePath, batchSize, limiter, false);
    CorrelationProvider provider = correlationProviderFactory.create(entityViewConfiguration.getCriteriaBuilder(), entityViewConfiguration.getOptionalParameters());
    String correlationKeyExpression;
    if (batchSize > 1) {
        if (batchCorrelationMode == BatchCorrelationMode.VALUES) {
            this.correlationParamName = CORRELATION_KEY_ALIAS;
        // TODO: when using EMBEDDING_VIEW, we could make use of correlationBasis instead of binding parameters separately
        } else {
            this.correlationParamName = generateCorrelationParamName();
        }
        if (correlationBasisEntityType != null) {
            correlationKeyExpression = CORRELATION_KEY_ALIAS;
            if (batchCorrelationMode == BatchCorrelationMode.VALUES) {
                correlationSelectExpression = CORRELATION_KEY_ALIAS + '.' + getEntityIdName(correlationBasisEntityType);
            } else {
                correlationSelectExpression = CORRELATION_KEY_ALIAS + '.' + viewRootIdAttributePath;
            }
        } else {
            // The correlation key is basic type
            correlationSelectExpression = correlationKeyExpression = CORRELATION_KEY_ALIAS;
        }
    } else {
        this.correlationParamName = generateCorrelationParamName();
        this.correlationSelectExpression = correlationKeyExpression = null;
    }
    int originalFirstResult = criteriaBuilder.getFirstResult();
    int originalMaxResults = criteriaBuilder.getMaxResults();
    if (batchSize > 1 && batchCorrelationMode == BatchCorrelationMode.VALUES) {
        provider.applyCorrelation(correlationBuilder, correlationKeyExpression);
    } else {
        provider.applyCorrelation(correlationBuilder, ':' + correlationParamName);
    }
    if (batchSize > 1 && (originalFirstResult != criteriaBuilder.getFirstResult() || originalMaxResults != criteriaBuilder.getMaxResults())) {
        throw new IllegalArgumentException("Correlation provider '" + provider + "' wrongly uses setFirstResult() or setMaxResults() on the query builder which might lead to wrong results. Use SELECT fetching with batch size 1 or reformulate the correlation provider to use the limit/offset in a subquery!");
    }
    if (fetches.length != 0) {
        for (int i = 0; i < fetches.length; i++) {
            criteriaBuilder.fetch(fetches[i]);
        }
    }
    if (indexFetches.length != 0) {
        for (int i = 0; i < indexFetches.length; i++) {
            criteriaBuilder.fetch(indexFetches[i]);
        }
    }
    return correlationBuilder.getCorrelationRoot();
}
Also used : CorrelatedSubqueryViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryViewRootJpqlMacro) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider) CorrelatedSubqueryEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryEmbeddingViewJpqlMacro) CTEBuilder(com.blazebit.persistence.CTEBuilder)

Aggregations

CTEBuilder (com.blazebit.persistence.CTEBuilder)1 CorrelationProvider (com.blazebit.persistence.view.CorrelationProvider)1 CorrelatedSubqueryEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryEmbeddingViewJpqlMacro)1 CorrelatedSubqueryViewRootJpqlMacro (com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryViewRootJpqlMacro)1 MutableViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro)1