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

act.db.ebean2.EbeanQuery Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
package act.db.ebean2;

/*-
 * #%L
 * ACT AAA Plugin
 * %%
 * Copyright (C) 2015 - 2017 ActFramework
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import act.db.Dao;
import io.ebean.*;
import org.osgl.$;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.Generics;
import org.osgl.util.S;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class EbeanQuery implements Query, Dao.Query> {

    protected Class modelType;

    Query q;
    EbeanDao dao;

    public EbeanQuery() {
        List typeParams = Generics.typeParamImplementations(getClass(), EbeanQuery.class);
        int sz = typeParams.size();
        if (sz > 1) {
            dao = $.cast(typeParams.get(1));
        }
        if (sz > 0) {
            modelType = $.cast(typeParams.get(0));
        }
    }

    public EbeanQuery(EbeanDao dao, Class modelType) {
        this.modelType = modelType;
        EbeanServer ebean = dao.ebean();
        E.NPE(ebean);
        q = ebean.createQuery(modelType);
        this.dao = dao;
    }

    public Query rawQuery() {
        return q;
    }

    @Override
    public EbeanQuery asOf(Timestamp timestamp) {
        q.asOf(timestamp);
        return this;
    }

    @Override
    public  A findSingleAttribute() {
        return q.findSingleAttribute();
    }

    @Override
    public List> findVersions() {
        return q.findVersions();
    }

    @Override
    public List> findVersionsBetween(Timestamp timestamp, Timestamp timestamp1) {
        return q.findVersionsBetween(timestamp, timestamp1);
    }

    @Override
    public EbeanQuery apply(FetchPath fetchPath) {
        q.apply(fetchPath);
        return this;
    }

    @Override
    public ExpressionList text() {
        return q.text();
    }

    @Override
    public EbeanQuery setUseDocStore(boolean b) {
        q.setUseDocStore(b);
        return this;
    }

    @Override
    public Query setBeanCacheMode(CacheMode beanCacheMode) {
        q.setBeanCacheMode(beanCacheMode);
        return this;
    }

    @Override
    public Query setProfileLocation(ProfileLocation profileLocation) {
        q.setProfileLocation(profileLocation);
        return this;
    }

    @Override
    public int update() {
        return q.update();
    }

    @Override
    public int delete() {
        return q.delete();
    }

    @Override
    public Object getId() {
        return q.getId();
    }

    @Override
    public Class getBeanType() {
        return q.getBeanType();
    }

    @Override
    public EbeanQuery setDisableLazyLoading(boolean b) {
        q.setDisableLazyLoading(b);
        return this;
    }

    @Override
    public EbeanQuery offset(int pos) {
        q.setFirstRow(pos);
        return this;
    }

    @Override
    public EbeanQuery limit(int limit) {
        q.setMaxRows(limit);
        return this;
    }

    @Override
    public EbeanQuery orderBy(String... fieldList) {
        E.illegalArgumentIf(fieldList.length == 0);
        q.order(S.join(" ", fieldList));
        return this;
    }

    @Override
    public MODEL_TYPE first() {
        return q.findOne();
    }

    @Override
    public EbeanQuery fetchQuery(String path, String fetchProperties) {
        q.fetchQuery(path, fetchProperties);
        return this;
    }

    @Override
    public EbeanQuery fetchLazy(String path, String fetchProperties) {
        q.fetchLazy(path, fetchProperties);
        return this;
    }

    @Override
    public EbeanQuery fetchQuery(String path) {
        q.fetchQuery(path);
        return this;
    }

    @Override
    public EbeanQuery fetchLazy(String path) {
        q.fetchLazy(path);
        return this;
    }

    @Override
    public Iterable fetch() {
        C.List list = C.newList();
        QueryIterator qi = findIterate();
        while (qi.hasNext()) {
            list.add(qi.next());
        }
        qi.close();
        return list;
// we need to close the query iterable right now otherwise it hold the data connection forever
//        return new Iterable() {
//            @Override
//            public Iterator iterator() {
//                return findIterate();
//            }
//        };
    }

    @Override
    public long count() {
        return q.findCount();
    }

    // --- Ebean Query methods: delegate to q


    @Override
    public EbeanQuery setIncludeSoftDeletes() {
        q.setIncludeSoftDeletes();
        return this;
    }

    @Override
    public EbeanQuery asDraft() {
        q.asDraft();
        return this;
    }

    @Override
    public boolean isAutoTuned() {
        return q.isAutoTuned();
    }

    @Override
    public EbeanQuery setAutoTune(boolean b) {
        q.setAutoTune(b);
        return this;
    }

    @Override
    public EbeanQuery setDisableReadAuditing() {
        q.setDisableReadAuditing();
        return this;
    }

    @Override
    public EbeanQuery setUseQueryCache(CacheMode useQueryCache) {
        q.setUseQueryCache(useQueryCache);
        return this;
    }

    @Override
    public EbeanQuery setProfileId(int i) {
        q.setProfileId(i);
        return this;
    }

    @Override
    public EbeanQuery setRawSql(RawSql rawSql) {
        q.setRawSql(rawSql);
        return this;
    }

    @Override
    public EbeanQuery setDocIndexName(String indexName) {
        q.setDocIndexName(indexName);
        return this;
    }

    @Override
    public void findEach(Consumer consumer) {
        q.findEach(consumer);
    }

    @Override
    public void findEachWhile(Predicate consumer) {
        q.findEachWhile(consumer);
    }

    @Override
    public EbeanQuery forUpdate() {
        q.forUpdate();
        return this;
    }

    @Override
    public EbeanQuery forUpdateNoWait() {
        q.forUpdateNoWait();
        return this;
    }

    @Override
    public EbeanQuery forUpdateSkipLocked() {
        q.forUpdateSkipLocked();
        return this;
    }

    @Override
    public ForUpdate getForUpdateMode() {
        return q.getForUpdateMode();
    }

    @Override
    public void cancel() {
        q.cancel();
    }

    @Override
    public EbeanQuery copy() {
        q.copy();
        return this;
    }

    @Override
    public EbeanQuery setPersistenceContextScope(PersistenceContextScope scope) {
        q.setPersistenceContextScope(scope);
        return this;
    }

    @Override
    public ExpressionFactory getExpressionFactory() {
        return q.getExpressionFactory();
    }

    @Override
    public EbeanQuery setLazyLoadBatchSize(int lazyLoadBatchSize) {
        q.setLazyLoadBatchSize(lazyLoadBatchSize);
        return this;
    }

    @Override
    public EbeanQuery select(String fetchProperties) {
        q.select(fetchProperties);
        return this;
    }

    @Override
    public EbeanQuery fetch(String path, String fetchProperties) {
        q.fetch(path, fetchProperties);
        return this;
    }

    @Override
    public EbeanQuery fetch(String assocProperty, String fetchProperties, FetchConfig fetchConfig) {
        q.fetch(assocProperty, fetchProperties, fetchConfig);
        return this;
    }

    @Override
    public EbeanQuery fetch(String path) {
        q.fetch(path);
        return this;
    }

    @Override
    public EbeanQuery fetch(String path, FetchConfig joinConfig) {
        q.fetch(path, joinConfig);
        return this;
    }

    @Override
    public List findIds() {
        return q.findIds();
    }

    @Override
    public QueryIterator findIterate() {
        QueryIterator i = q.findIterate();
        dao.registerQueryIterator(i);
        return i;
    }

    public void consume($.Visitor visitor) {
        QueryIterator i = q.findIterate();
        try {
            while (i.hasNext()) {
                MODEL_TYPE entity = i.next();
                visitor.visit(entity);
            }
        } finally {
            i.close();
        }
    }

    @Override
    public List findList() {
        return q.findList();
    }

    @Override
    public Set findSet() {
        return q.findSet();
    }

    @Override
    public  Map findMap() {
        return q.findMap();
    }

    @Override
    public  List findSingleAttributeList() {
        return q.findSingleAttributeList();
    }

    @Override
    public int findCount() {
        return q.findCount();
    }

    @Override
    public MODEL_TYPE findOne() {
        return q.findOne();
    }

    @Override
    public Optional findOneOrEmpty() {
        return q.findOneOrEmpty();
    }

    @Override
    public FutureRowCount findFutureCount() {
        return q.findFutureCount();
    }

    @Override
    public FutureIds findFutureIds() {
        return q.findFutureIds();
    }

    @Override
    public FutureList findFutureList() {
        return q.findFutureList();
    }

    @Override
    public EbeanQuery setParameter(String name, Object value) {
        q.setParameter(name, value);
        return this;
    }

    @Override
    public EbeanQuery setParameter(int position, Object value) {
        q.setParameter(position, value);
        return this;
    }

    @Override
    public EbeanQuery setId(Object id) {
        q.setId(id);
        return this;
    }

    @Override
    public EbeanQuery where(Expression expression) {
        q.where(expression);
        return this;
    }

    @Override
    public ExpressionList where() {
        return q.where();
    }

    @Override
    public ExpressionList filterMany(String propertyName) {
        return q.filterMany(propertyName);
    }

    @Override
    public ExpressionList having() {
        return q.having();
    }

    @Override
    public EbeanQuery having(Expression addExpressionToHaving) {
        q.having(addExpressionToHaving);
        return this;
    }

    @Override
    public EbeanQuery orderBy(String orderByClause) {
        q.orderBy(orderByClause);
        return this;
    }

    @Override
    public EbeanQuery order(String orderByClause) {
        q.order(orderByClause);
        return this;
    }

    @Override
    public OrderBy order() {
        return q.order();
    }

    @Override
    public OrderBy orderBy() {
        return q.orderBy();
    }

    @Override
    public EbeanQuery setOrder(OrderBy orderBy) {
        q.setOrder(orderBy);
        return this;
    }

    @Override
    public EbeanQuery setOrderBy(OrderBy orderBy) {
        q.setOrderBy(orderBy);
        return this;
    }

    @Override
    public EbeanQuery setDistinct(boolean isDistinct) {
        q.setDistinct(isDistinct);
        return this;
    }

    @Override
    public int getFirstRow() {
        return q.getFirstRow();
    }

    @Override
    public EbeanQuery setFirstRow(int firstRow) {
        q.setFirstRow(firstRow);
        return this;
    }

    @Override
    public int getMaxRows() {
        return q.getMaxRows();
    }

    @Override
    public EbeanQuery setMaxRows(int maxRows) {
        q.setMaxRows(maxRows);
        return this;
    }

    @Override
    public EbeanQuery setMapKey(String mapKey) {
        q.setMapKey(mapKey);
        return this;
    }

    @Override
    public EbeanQuery setUseCache(boolean useBeanCache) {
        q.setUseCache(useBeanCache);
        return this;
    }

    @Override
    public EbeanQuery setUseQueryCache(boolean useQueryCache) {
        q.setUseQueryCache(useQueryCache);
        return this;
    }

    @Override
    public EbeanQuery setReadOnly(boolean readOnly) {
        q.setReadOnly(readOnly);
        return this;
    }

    @Override
    public EbeanQuery setLoadBeanCache(boolean loadBeanCache) {
        q.setLoadBeanCache(loadBeanCache);
        return this;
    }

    @Override
    public EbeanQuery setTimeout(int secs) {
        q.setTimeout(secs);
        return this;
    }

    @Override
    public EbeanQuery setBufferFetchSizeHint(int fetchSize) {
        q.setBufferFetchSizeHint(fetchSize);
        return this;
    }

    @Override
    public String getGeneratedSql() {
        return q.getGeneratedSql();
    }

    @Override
    public EbeanQuery setForUpdate(boolean forUpdate) {
        q.setForUpdate(forUpdate);
        return this;
    }

    @Override
    public boolean isForUpdate() {
        return q.isForUpdate();
    }

    @Override
    public EbeanQuery alias(String alias) {
        q.alias(alias);
        return this;
    }

    @Override
    public PagedList findPagedList() {
        return q.findPagedList();
    }

    @Override
    public Set validate() {
        return q.validate();
    }
}