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

grails.gorm.rx.DetachedCriteria.groovy Maven / Gradle / Ivy

The newest version!
package grails.gorm.rx

import groovy.transform.CompileStatic
import groovy.transform.InheritConstructors
import org.grails.datastore.gorm.finders.DynamicFinder
import org.grails.datastore.gorm.query.criteria.AbstractDetachedCriteria
import org.grails.datastore.mapping.query.Query
import org.grails.datastore.mapping.query.api.Criteria
import org.grails.datastore.mapping.query.api.ProjectionList
import org.grails.datastore.mapping.query.api.QueryArgumentsAware
import org.grails.datastore.mapping.query.api.QueryableCriteria
import org.grails.datastore.rx.query.RxQuery
import org.grails.gorm.rx.api.RxGormEnhancer
import rx.Observable
import rx.Subscriber
import rx.Subscription

import jakarta.persistence.FetchType

/**
 * Reactive version of {@link grails.gorm.DetachedCriteria}
 *
 * @author Graeme Rocher
 * @since 6.0
 */
@CompileStatic
class DetachedCriteria extends AbstractDetachedCriteria> implements PersistentObservable {
    DetachedCriteria(Class> targetClass, String alias) {
        super(targetClass, alias)
    }

    DetachedCriteria(Class> targetClass) {
        super(targetClass)
    }

    /**
     * Finds a single result matching this criteria. Note that the observable returned will emit each result one by one. If you
     * prefer to receive the entire list of results use {@link #toList()} instead
     *
     * @param args The arguments The arguments
     * @param additionalCriteria Any additional criteria
     *
     * @return An observable
     */
    Observable find(Map args = Collections.emptyMap(), @DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        Query query = prepareQuery(args, additionalCriteria)
        query.max(1)
        return ((RxQuery)query).findAll(args)
    }

    /**
     * Finds all results matching this criteria. Note that the observable returned will emit each result one by one. If you
     * prefer to receive the entire list of results use {@link #toList()} instead
     *
     * @param args The arguments The arguments
     * @param additionalCriteria Any additional criteria
     *
     * @return An observable
     */
    Observable findAll(Map args = Collections.emptyMap(), @DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        Query query = prepareQuery(args, additionalCriteria)
        return ((RxQuery)query).findAll(args)
    }

    /**
     * The same as {@link #find(java.util.Map, groovy.lang.Closure)}
     *
     * @see #find(java.util.Map, groovy.lang.Closure)
     */
    Observable get(Map args, @DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        Query query = prepareQuery(args, additionalCriteria)
        query.max(1)
        return ((RxQuery)query).singleResult(args)
    }

    /**
     * The same as {@link #find(java.util.Map, groovy.lang.Closure)}
     *
     * @see #find(java.util.Map, groovy.lang.Closure)
     */
    Observable get(@DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        Query query = prepareQuery(Collections.emptyMap(), additionalCriteria)
        query.max(1)
        return ((RxQuery)query).singleResult()
    }

    /**
     * Converts the observable to another observable that outputs the complete list. Not for use with large datasets
     *
     * @param args The arguments
     * @param additionalCriteria Any additional criteria
     * @return An observable that emits a list
     */
    Observable> toList(Map args = Collections.emptyMap(), @DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        Query query = prepareQuery(args, additionalCriteria)
        return ((RxQuery)query).findAll(args).toList()
    }

    /**
     * Converts the observable to another observable that outputs the complete list. Not for use with large datasets
     *
     * @param args The arguments
     * @param additionalCriteria Any additional criteria
     * @return An observable that emits a list
     */
    Observable> list(Map args = Collections.emptyMap(), @DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        Query query = prepareQuery(args, additionalCriteria)
        return ((RxQuery)query).findAll(args).toList()
    }

    /**
     * Calculates the total number of matches for the query
     *
     * @param args The arguments
     * @param additionalCriteria Any additional criteria
     * @return The total results
     */
    Observable getCount(Map args = Collections.emptyMap(), @DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        Query query = prepareQuery(args, additionalCriteria)
        query.projections().count()
        return ((RxQuery)query).singleResult(args) as Observable
    }

    /**
     * The same as {@link #getCount(java.util.Map, groovy.lang.Closure)}
     *
     * @see #getCount(java.util.Map, groovy.lang.Closure)
     */
    Observable count(Map args = Collections.emptyMap(), @DelegatesTo(DetachedCriteria) Closure additionalCriteria = null) {
        getCount(args, additionalCriteria)
    }

    /**
     * Updates all entities matching this criteria
     *
     * @param propertiesMap The property names and values to update
     *
     * @return An observable that returns the total number updated
     */
    Observable updateAll(Map propertiesMap) {
        Query query = prepareQuery(Collections.emptyMap(), null)
        return ((RxQuery)query).updateAll(propertiesMap)
    }

    /**
     * Deletes all entities matching this criteria
     *
     * @return An observable that returns the total number deleted
     */
    Observable deleteAll() {
        Query query = prepareQuery(Collections.emptyMap(), null)
        return ((RxQuery)query).deleteAll()
    }

    /**
     * Convert this {@link DetachedCriteria} to a query implementation
     *
     * @param args The arguments
     * @return The query
     */
    Query toQuery(Map args = Collections.emptyMap()) {
        return prepareQuery(args, null)
    }

    @Override
    DetachedCriteria where(@DelegatesTo(AbstractDetachedCriteria) Closure additionalQuery) {
        return (DetachedCriteria) super.where(additionalQuery)
    }

    @Override
    DetachedCriteria whereLazy(@DelegatesTo(AbstractDetachedCriteria) Closure additionalQuery) {
        return (DetachedCriteria) super.whereLazy(additionalQuery)
    }

    @Override
    DetachedCriteria build(@DelegatesTo(grails.gorm.DetachedCriteria) Closure callable) {
        return (DetachedCriteria)super.build(callable)
    }

    @Override
    DetachedCriteria buildLazy(@DelegatesTo(grails.gorm.DetachedCriteria) Closure callable) {
        return (DetachedCriteria)super.buildLazy(callable)
    }

    @Override
    DetachedCriteria max(int max) {
        return (DetachedCriteria)super.max(max)
    }

    @Override
    DetachedCriteria offset(int offset) {
        return (DetachedCriteria)super.offset(offset)
    }

    @Override
    DetachedCriteria sort(String property) {
        return (DetachedCriteria)super.sort(property)
    }

    @Override
    DetachedCriteria sort(String property, String direction) {
        return (DetachedCriteria)super.sort(property, direction)
    }

    @Override
    DetachedCriteria property(String property) {
        return (DetachedCriteria)super.property(property)
    }

    @Override
    DetachedCriteria id() {
        return (DetachedCriteria)super.id()
    }

    @Override
    DetachedCriteria avg(String property) {
        return (DetachedCriteria)super.avg(property)
    }

    @Override
    DetachedCriteria sum(String property) {
        return (DetachedCriteria)super.sum(property)
    }

    @Override
    DetachedCriteria min(String property) {
        return (DetachedCriteria)super.min(property)
    }

    @Override
    DetachedCriteria max(String property) {
        return (DetachedCriteria)super.max(property)
    }

    @Override
    def propertyMissing(String name) {
        return super.propertyMissing(name)
    }

    @Override
    DetachedCriteria distinct(String property) {
        return (DetachedCriteria)super.distinct(property)
    }

    @Override
    protected QueryableCriteria buildQueryableCriteria(Closure queryClosure) {
        return (QueryableCriteria)new DetachedCriteria(targetClass).build(queryClosure)
    }

    @Override
    protected DetachedCriteria clone() {
        return (DetachedCriteria)super.clone()
    }

    @Override
    protected DetachedCriteria newInstance() {
        new DetachedCriteria(targetClass, alias)
    }

    @Override
    DetachedCriteria join(String property) {
        return (DetachedCriteria)super.join(property)
    }

    @Override
    DetachedCriteria select(String property) {
        return (DetachedCriteria)super.select(property)
    }

    @Override
    DetachedCriteria projections(@DelegatesTo(ProjectionList) Closure callable) {
        return (DetachedCriteria)super.projections(callable)
    }

    @Override
    DetachedCriteria and(@DelegatesTo(AbstractDetachedCriteria) Closure callable) {
        return (DetachedCriteria)super.and(callable)
    }

    @Override
    DetachedCriteria or(@DelegatesTo(AbstractDetachedCriteria) Closure callable) {
        return (DetachedCriteria)super.or(callable)
    }

    @Override
    DetachedCriteria not(@DelegatesTo(AbstractDetachedCriteria) Closure callable) {
        return (DetachedCriteria)super.not(callable)
    }

    @Override
    DetachedCriteria "in"(String propertyName, Collection values) {
        return (DetachedCriteria)super.in(propertyName, values)
    }

    @Override
    DetachedCriteria "in"(String propertyName, QueryableCriteria subquery) {
        return (DetachedCriteria)super.in(propertyName, subquery)
    }

    @Override
    DetachedCriteria inList(String propertyName, QueryableCriteria subquery) {
        return (DetachedCriteria)super.inList(propertyName, subquery)
    }

    @Override
    DetachedCriteria "in"(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure subquery) {
        return (DetachedCriteria)super.in(propertyName, subquery)
    }

    @Override
    DetachedCriteria inList(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure subquery) {
        return (DetachedCriteria)super.inList(propertyName, subquery)
    }

    @Override
    DetachedCriteria "in"(String propertyName, Object[] values) {
        return (DetachedCriteria)super.in(propertyName, values)
    }

    @Override
    DetachedCriteria notIn(String propertyName, QueryableCriteria subquery) {
        return (DetachedCriteria)super.notIn(propertyName, subquery)
    }

    @Override
    DetachedCriteria notIn(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure subquery) {
        return (DetachedCriteria)super.notIn(propertyName, subquery)
    }

    @Override
    DetachedCriteria order(String propertyName) {
        return (DetachedCriteria)super.order(propertyName)
    }

    @Override
    DetachedCriteria order(Query.Order o) {
        return (DetachedCriteria)super.order(o)
    }

    @Override
    DetachedCriteria order(String propertyName, String direction) {
        return (DetachedCriteria)super.order(propertyName, direction)
    }

    @Override
    DetachedCriteria inList(String propertyName, Collection values) {
        return (DetachedCriteria)super.inList(propertyName, values)
    }

    @Override
    protected List convertArgumentList(Collection argList) {
        return super.convertArgumentList(argList)
    }

    @Override
    DetachedCriteria inList(String propertyName, Object[] values) {
        return (DetachedCriteria)super.inList(propertyName, values)
    }

    @Override
    DetachedCriteria sizeEq(String propertyName, int size) {
        return (DetachedCriteria)super.sizeEq(propertyName, size)
    }

    @Override
    DetachedCriteria sizeGt(String propertyName, int size) {
        return (DetachedCriteria)super.sizeGt(propertyName, size)
    }

    @Override
    DetachedCriteria sizeGe(String propertyName, int size) {
        return (DetachedCriteria)super.sizeGe(propertyName, size)
    }

    @Override
    DetachedCriteria sizeLe(String propertyName, int size) {
        return (DetachedCriteria)super.sizeLe(propertyName, size)
    }

    @Override
    DetachedCriteria sizeLt(String propertyName, int size) {
        return (DetachedCriteria)super.sizeLt(propertyName, size)
    }

    @Override
    DetachedCriteria sizeNe(String propertyName, int size) {
        return (DetachedCriteria)super.sizeNe(propertyName, size)
    }

    @Override
    DetachedCriteria eqProperty(String propertyName, String otherPropertyName) {
        return (DetachedCriteria)super.eqProperty(propertyName, otherPropertyName)
    }

    @Override
    DetachedCriteria neProperty(String propertyName, String otherPropertyName) {
        return (DetachedCriteria)super.neProperty(propertyName, otherPropertyName)
    }

    @Override
    DetachedCriteria allEq(Map propertyValues) {
        return (DetachedCriteria)super.allEq(propertyValues)
    }

    @Override
    DetachedCriteria gtProperty(String propertyName, String otherPropertyName) {
        return (DetachedCriteria)super.gtProperty(propertyName, otherPropertyName)
    }

    @Override
    DetachedCriteria geProperty(String propertyName, String otherPropertyName) {
        return (DetachedCriteria)super.geProperty(propertyName, otherPropertyName)
    }

    @Override
    DetachedCriteria ltProperty(String propertyName, String otherPropertyName) {
        return (DetachedCriteria)super.ltProperty(propertyName, otherPropertyName)
    }

    @Override
    DetachedCriteria leProperty(String propertyName, String otherPropertyName) {
        return (DetachedCriteria)super.leProperty(propertyName, otherPropertyName)
    }

    @Override
    DetachedCriteria idEquals(Object value) {
        return (DetachedCriteria)super.idEquals(value)
    }

    @Override
    DetachedCriteria exists(QueryableCriteria subquery) {
        return (DetachedCriteria)super.exists(subquery)
    }

    @Override
    DetachedCriteria notExists(QueryableCriteria subquery) {
        return (DetachedCriteria)super.notExists(subquery)
    }

    @Override
    DetachedCriteria isEmpty(String propertyName) {
        return (DetachedCriteria)super.isEmpty(propertyName)
    }

    @Override
    DetachedCriteria isNotEmpty(String propertyName) {
        return (DetachedCriteria)super.isNotEmpty(propertyName)
    }

    @Override
    DetachedCriteria isNull(String propertyName) {
        return (DetachedCriteria)super.isNull(propertyName)
    }

    @Override
    DetachedCriteria isNotNull(String propertyName) {
        return (DetachedCriteria)super.isNotNull(propertyName)
    }

    @Override
    DetachedCriteria eq(String propertyName, Object propertyValue) {
        return (DetachedCriteria)super.eq(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria idEq(Object propertyValue) {
        return (DetachedCriteria)super.idEq(propertyValue)
    }

    @Override
    DetachedCriteria ne(String propertyName, Object propertyValue) {
        return (DetachedCriteria)super.ne(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria between(String propertyName, Object start, Object finish) {
        return (DetachedCriteria)super.between(propertyName, start, finish)
    }

    @Override
    DetachedCriteria gte(String property, Object value) {
        return (DetachedCriteria)super.gte(property, value)
    }

    @Override
    DetachedCriteria ge(String property, Object value) {
        return (DetachedCriteria)super.ge(property, value)
    }

    @Override
    DetachedCriteria gt(String property, Object value) {
        return (DetachedCriteria)super.gt(property, value)
    }

    @Override
    DetachedCriteria lte(String property, Object value) {
        return (DetachedCriteria)super.lte(property, value)
    }

    @Override
    DetachedCriteria le(String property, Object value) {
        return (DetachedCriteria)super.le(property, value)
    }

    @Override
    DetachedCriteria lt(String property, Object value) {
        return (DetachedCriteria)super.lt(property, value)
    }

    @Override
    DetachedCriteria like(String propertyName, Object propertyValue) {
        return (DetachedCriteria)super.like(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria ilike(String propertyName, Object propertyValue) {
        return (DetachedCriteria)super.ilike(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria rlike(String propertyName, Object propertyValue) {
        return (DetachedCriteria)super.rlike(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria eqAll(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.eqAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria gtAll(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.gtAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria ltAll(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.ltAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria geAll(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.geAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria leAll(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.leAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria eqAll(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.eqAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria gtAll(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.gtAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria gtSome(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.gtSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria gtSome(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.gtSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria geSome(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.geSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria geSome(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.geSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria ltSome(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.ltSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria ltSome(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.ltSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria leSome(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.leSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria leSome(String propertyName, @DelegatesTo(AbstractDetachedCriteria) Closure propertyValue) {
        return (DetachedCriteria)super.leSome(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria ltAll(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.ltAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria geAll(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.geAll(propertyName, propertyValue)
    }

    @Override
    DetachedCriteria leAll(String propertyName, QueryableCriteria propertyValue) {
        return (DetachedCriteria)super.leAll(propertyName, propertyValue)
    }

    protected Query prepareQuery(Map args, Closure additionalCriteria) {
        def staticApi = RxGormEnhancer.findStaticApi(targetClass)
        applyLazyCriteria()
        def query = staticApi.datastoreClient.createQuery(targetClass, args)

        if (defaultMax != null) {
            query.max(defaultMax)
        }
        if (defaultOffset != null) {
            query.offset(defaultOffset)
        }
        DynamicFinder.applyDetachedCriteria(query, this)

        for(entry in fetchStrategies) {
            switch(entry.value) {
                case FetchType.EAGER:
                    query.join(entry.key)
                break
                default:
                    query.select(entry.key)
            }
        }

        if (query instanceof QueryArgumentsAware) {
            query.arguments = args
        }

        if (additionalCriteria != null) {
            def additionalDetached = new DetachedCriteria(targetClass).build(additionalCriteria)
            DynamicFinder.applyDetachedCriteria(query, additionalDetached)
        }

        DynamicFinder.populateArgumentsForCriteria(targetClass, query, args)
        query
    }

    @Override
    Observable toObservable() {
        findAll()
    }

    @Override
    Subscription subscribe(Subscriber subscriber) {
        findAll().subscribe(subscriber)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy