All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dellroad.querystream.jpa.FromStreamImpl Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2018 Archie L. Cobbs. All rights reserved.
 */

package org.dellroad.querystream.jpa;

import jakarta.persistence.EntityManager;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Parameter;
import jakarta.persistence.TemporalType;
import jakarta.persistence.criteria.AbstractQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.Order;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Selection;
import jakarta.persistence.metamodel.PluralAttribute;
import jakarta.persistence.metamodel.SingularAttribute;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

import org.dellroad.querystream.jpa.querytype.SearchType;

class FromStreamImpl> extends PathStreamImpl implements FromStream {

// Constructors

    FromStreamImpl(EntityManager entityManager, SearchType queryType,
      QueryConfigurer, X, ? extends S> configurer, QueryInfo queryInfo) {
        super(entityManager, queryType, configurer, queryInfo);
    }

/*

// Plural Joins

    public  SearchStream flatMap(CollectionAttribute attribute) {
        if (attribute == null)
            throw new IllegalArgumentException("null attribute");
        return this.flatMap(attribute.getElementType().getJavaType(), from -> from.join(attribute));
    }

    public  SearchStream flatMap(ListAttribute attribute) {
        if (attribute == null)
            throw new IllegalArgumentException("null attribute");
        return this.flatMap(attribute.getElementType().getJavaType(), from -> from.join(attribute));
    }

    public  SearchStream flatMapValues(MapAttribute attribute) {
        if (attribute == null)
            throw new IllegalArgumentException("null attribute");
        return this.flatMap(attribute.getElementType().getJavaType(), from -> from.join(attribute));
    }

    public  SearchStream flatMapKeys(MapAttribute attribute) {
        if (attribute == null)
            throw new IllegalArgumentException("null attribute");
        return this.flatMap(attribute.getKeyJavaType(), from -> from.join(attribute).key());
    }

    public  SearchStream flatMap(SetAttribute attribute) {
        if (attribute == null)
            throw new IllegalArgumentException("null attribute");
        return this.flatMap(attribute.getElementType().getJavaType(), from -> from.join(attribute));
    }

    // This is used for plural joins, where limit and offset may need to be "recalculated" (which we can't do; see below)
    private  SearchStream flatMap(Class joinedType, Function, ? extends Expression> joiner) {

        // If there is an offset or limit configured, we need to do a sub-query, so that the offset & limit apply to the
        // owners of the collection, not the collection elements themselves. Oops! The Criteria API doesn't support setting
        // a row offset or row count limit on a subquery (why not?):
        //  https://stackoverflow.com/questions/37187193/criteria-api-limit-results-in-subquery
        if (this.offset != 0 || this.limit != -1) {
            throw new UnsupportedOperationException("sorry, can't perform plural join after skip() or limit() because"
              + " the JPA Criteria API does not support setting a row offset or row count limit in subqueries");
        }

        // Apply "flat map" via join
        return new FromStream(this.entityManager, new SearchType<>(joinedType),
          (builder, query) -> joiner.apply(this.configurer.configureFrom(builder, query)));
    }
*/

// Narrowing overrides (SearchStreamImpl)

    @Override
    FromStream create(EntityManager entityManager, SearchType queryType,
      QueryConfigurer, X, ? extends S> configurer, QueryInfo queryInfo) {
        return new FromStreamImpl<>(entityManager, queryType, configurer, queryInfo);
    }

    @Override
    FromStream withConfig(QueryConfigurer, X, ? extends S> configurer) {
        return (FromStream)super.withConfig(configurer);
    }

    @Override
    FromValue toValue() {
        return this.toValue(false);
    }

    @Override
    FromValue toValue(boolean forceLimit) {
        return new FromValueImpl<>(this.entityManager, this.queryType,
          this.configurer, forceLimit ? this.queryInfo.withMaxResults(1) : this.queryInfo);
    }

    @Override
    public FromStream distinct() {
        return (FromStream)super.distinct();
    }

    @Override
    public FromStream orderBy(Ref> ref, boolean asc) {
        return (FromStream)super.orderBy(ref, asc);
    }

    @Override
    public FromStream orderBy(SingularAttribute attribute, boolean asc) {
        return (FromStream)super.orderBy(attribute, asc);
    }

    @Override
    public FromStream orderBy(SingularAttribute attribute1, boolean asc1,
      SingularAttribute attribute2, boolean asc2) {
        return (FromStream)super.orderBy(attribute1, asc1, attribute2, asc2);
    }

    @Override
    public FromStream orderBy(SingularAttribute attribute1, boolean asc1,
      SingularAttribute attribute2, boolean asc2, SingularAttribute attribute3, boolean asc3) {
        return (FromStream)super.orderBy(attribute1, asc1, attribute2, asc2, attribute3, asc3);
    }

    @Override
    public FromStream orderBy(Function> orderExprFunction, boolean asc) {
        return (FromStream)super.orderBy(orderExprFunction, asc);
    }

    @Override
    public FromStream orderBy(Order... orders) {
        return (FromStream)super.orderBy(orders);
    }

    @Override
    public FromStream orderByMulti(Function> orderListFunction) {
        return (FromStream)super.orderByMulti(orderListFunction);
    }

    @Override
    public FromStream thenOrderBy(Ref> ref, boolean asc) {
        return (FromStream)super.thenOrderBy(ref, asc);
    }

    @Override
    public FromStream thenOrderBy(Order... orders) {
        return (FromStream)super.thenOrderBy(orders);
    }

    @Override
    public FromStream thenOrderBy(SingularAttribute attribute, boolean asc) {
        return (FromStream)super.thenOrderBy(attribute, asc);
    }

    @Override
    public FromStream thenOrderBy(Function> orderExprFunction, boolean asc) {
        return (FromStream)super.thenOrderBy(orderExprFunction, asc);
    }

    @Override
    public FromStream groupBy(Ref> ref) {
        return (FromStream)super.groupBy(ref);
    }

    @Override
    public FromStream groupBy(SingularAttribute attribute) {
        return (FromStream)super.groupBy(attribute);
    }

    @Override
    public FromStream groupBy(Function> groupFunction) {
        return (FromStream)super.groupBy(groupFunction);
    }

    @Override
    public FromStream groupByMulti(Function>> groupFunction) {
        return (FromStream)super.groupByMulti(groupFunction);
    }

    @Override
    public FromStream having(Function> havingFunction) {
        return (FromStream)super.having(havingFunction);
    }

    @Override
    public FromValue findAny() {
        return (FromValue)super.findAny();
    }

    @Override
    public FromValue findFirst() {
        return (FromValue)super.findFirst();
    }

    @Override
    public FromValue findSingle() {
        return (FromValue)super.findSingle();
    }

    @Override
    public  FromStream addRoot(Ref> ref, Class type) {
        return (FromStream)super.addRoot(ref, type);
    }

// Fetches

    @Override
    public FromStream fetch(SingularAttribute attribute) {
        return this.fetch(attribute, JoinType.INNER);
    }

    @Override
    public FromStream fetch(SingularAttribute attribute, JoinType joinType) {
        if (attribute == null)
            throw new IllegalArgumentException("null attribute");
        if (joinType == null)
            throw new IllegalArgumentException("null joinType");
        return this.withConfig((builder, query) -> {
            final S selection = this.configure(builder, query);
            selection.fetch(attribute, joinType);
            return selection;
        });
    }

    @Override
    public FromStream fetch(PluralAttribute attribute) {
        return this.fetch(attribute, JoinType.INNER);
    }

    @Override
    public FromStream fetch(PluralAttribute attribute, JoinType joinType) {
        if (attribute == null)
            throw new IllegalArgumentException("null attribute");
        if (joinType == null)
            throw new IllegalArgumentException("null joinType");
        QueryStreamImpl.checkOffsetLimit(this, "plural fetch()");
        return this.withConfig((builder, query) -> {
            final S selection = this.configure(builder, query);
            selection.fetch(attribute, joinType);
            return selection;
        });
    }

// Narrowing overrides (QueryStreamImpl)

    @Override
    public FromStream bind(Ref ref) {
        return (FromStream)super.bind(ref);
    }

    @Override
    public FromStream peek(Consumer peeker) {
        return (FromStream)super.peek(peeker);
    }

    @Override
    public > FromStream bind(
      Ref ref, Function refFunction) {
        return (FromStream)super.bind(ref, refFunction);
    }

    @Override
    public FromStream filter(SingularAttribute attribute) {
        return (FromStream)super.filter(attribute);
    }

    @Override
    public FromStream filter(Function> predicateBuilder) {
        return (FromStream)super.filter(predicateBuilder);
    }

    @Override
    public FromStream limit(int limit) {
        return (FromStream)super.limit(limit);
    }

    @Override
    public FromStream skip(int skip) {
        return (FromStream)super.skip(skip);
    }

    @Override
    public FromStream withFlushMode(FlushModeType flushMode) {
        return (FromStream)super.withFlushMode(flushMode);
    }

    @Override
    public FromStream withLockMode(LockModeType lockMode) {
        return (FromStream)super.withLockMode(lockMode);
    }

    @Override
    public FromStream withHint(String name, Object value) {
        return (FromStream)super.withHint(name, value);
    }

    @Override
    public FromStream withHints(Map hints) {
        return (FromStream)super.withHints(hints);
    }

    @Override
    public  FromStream withParam(Parameter parameter, T value) {
        return (FromStream)super.withParam(parameter, value);
    }

    @Override
    public FromStream withParam(Parameter parameter, Date value, TemporalType temporalType) {
        return (FromStream)super.withParam(parameter, value, temporalType);
    }

    @Override
    public FromStream withParam(Parameter parameter, Calendar value, TemporalType temporalType) {
        return (FromStream)super.withParam(parameter, value, temporalType);
    }

    @Override
    public FromStream withParams(Iterable> params) {
        return (FromStream)super.withParams(params);
    }

    @Override
    public FromStream withLoadGraph(String name) {
        return (FromStream)super.withLoadGraph(name);
    }

    @Override
    public FromStream withFetchGraph(String name) {
        return (FromStream)super.withFetchGraph(name);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy