Search in sources :

Example 1 with CTEBuilder

use of com.blazebit.persistence.CTEBuilder 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)

Example 2 with CTEBuilder

use of com.blazebit.persistence.CTEBuilder in project blaze-persistence by Blazebit.

the class ManagedViewTypeImpl method renderSecondaryMappings.

@Override
public void renderSecondaryMappings(String viewPath, BaseQueryBuilder<?, ?> baseQueryBuilder, Map<String, Object> optionalParameters, boolean renderFetches) {
    if (baseQueryBuilder instanceof CTEBuilder) {
        CTEBuilder<?> cteBuilder = (CTEBuilder<?>) baseQueryBuilder;
        for (CTEProvider cteProvider : getCteProviders()) {
            cteProvider.applyCtes(cteBuilder, optionalParameters);
        }
    }
    for (ViewRoot viewRoot : viewRoots) {
        String entityViewRootName = viewRoot.getName();
        CorrelationProvider correlationProvider = viewRoot.getCorrelationProviderFactory().create(baseQueryBuilder, optionalParameters);
        ExpressionFactory expressionFactory = baseQueryBuilder.getService(ExpressionFactory.class);
        Limiter limiter = createLimiter(expressionFactory, viewPath, viewRoot.getLimitExpression(), viewRoot.getOffsetExpression(), viewRoot.getOrderByItems());
        String correlationAlias;
        if (limiter == null) {
            correlationAlias = entityViewRootName;
        } else {
            correlationAlias = "_sub_" + entityViewRootName;
        }
        JoinCorrelationBuilder correlationBuilder = new JoinCorrelationBuilder(baseQueryBuilder, optionalParameters, baseQueryBuilder, viewPath, correlationAlias, entityViewRootName, null, viewRoot.getJoinType(), limiter);
        correlationProvider.applyCorrelation(correlationBuilder, viewPath);
        correlationBuilder.finish();
        if (renderFetches && baseQueryBuilder instanceof FetchBuilder<?>) {
            ((FetchBuilder<?>) baseQueryBuilder).fetch(viewRoot.getFetches());
        }
    }
}
Also used : CTEProvider(com.blazebit.persistence.view.CTEProvider) JoinCorrelationBuilder(com.blazebit.persistence.view.impl.objectbuilder.transformer.correlation.JoinCorrelationBuilder) FetchBuilder(com.blazebit.persistence.FetchBuilder) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) ViewRoot(com.blazebit.persistence.view.metamodel.ViewRoot) StaticCorrelationProvider(com.blazebit.persistence.view.impl.StaticCorrelationProvider) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider) StaticPathCorrelationProvider(com.blazebit.persistence.view.impl.StaticPathCorrelationProvider) CTEBuilder(com.blazebit.persistence.CTEBuilder) Limiter(com.blazebit.persistence.view.impl.objectbuilder.Limiter)

Aggregations

CTEBuilder (com.blazebit.persistence.CTEBuilder)2 CorrelationProvider (com.blazebit.persistence.view.CorrelationProvider)2 FetchBuilder (com.blazebit.persistence.FetchBuilder)1 ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)1 CTEProvider (com.blazebit.persistence.view.CTEProvider)1 StaticCorrelationProvider (com.blazebit.persistence.view.impl.StaticCorrelationProvider)1 StaticPathCorrelationProvider (com.blazebit.persistence.view.impl.StaticPathCorrelationProvider)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 Limiter (com.blazebit.persistence.view.impl.objectbuilder.Limiter)1 JoinCorrelationBuilder (com.blazebit.persistence.view.impl.objectbuilder.transformer.correlation.JoinCorrelationBuilder)1 ViewRoot (com.blazebit.persistence.view.metamodel.ViewRoot)1