act.db.ebean2.EbeanQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of act-ebean2 Show documentation
Show all versions of act-ebean2 Show documentation
Provides SQL database access through Ebean ORM library (Java 8+)
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