de.greenrobot.dao.query.Query Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of greendao Show documentation
Show all versions of greendao Show documentation
Forked library from the original greendao from greenrobot
The newest version!
/*
* Copyright (C) 2011-2015 Markus Junginger, greenrobot (http://greenrobot.de)
*
* 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.
*/
package de.greenrobot.dao.query;
import android.database.Cursor;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.DaoException;
import java.util.List;
/**
* A repeatable query returning entities.
*
* @param The entity class the query will return results for.
* @author Markus
*/
public class Query extends AbstractQueryWithLimit {
private final static class QueryData extends AbstractQueryData> {
private final int limitPosition;
private final int offsetPosition;
QueryData(AbstractDao dao, String sql, String[] initialValues, int limitPosition, int offsetPosition) {
super(dao, sql, initialValues);
this.limitPosition = limitPosition;
this.offsetPosition = offsetPosition;
}
@Override
protected Query createQuery() {
return new Query(this, dao, sql, initialValues.clone(), limitPosition, offsetPosition);
}
}
/** For internal use by greenDAO only. */
public static Query internalCreate(AbstractDao dao, String sql, Object[] initialValues) {
return create(dao, sql, initialValues, -1, -1);
}
static Query create(AbstractDao dao, String sql, Object[] initialValues, int limitPosition,
int offsetPosition) {
QueryData queryData = new QueryData(dao, sql, toStringArray(initialValues), limitPosition,
offsetPosition);
return queryData.forCurrentThread();
}
private final QueryData queryData;
private Query(QueryData queryData, AbstractDao dao, String sql, String[] initialValues, int limitPosition,
int offsetPosition) {
super(dao, sql, initialValues, limitPosition, offsetPosition);
this.queryData = queryData;
}
public Query forCurrentThread() {
return queryData.forCurrentThread(this);
}
/** Executes the query and returns the result as a list containing all entities loaded into memory. */
public List list() {
checkThread();
Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);
return daoAccess.loadAllAndCloseCursor(cursor);
}
/**
* Executes the query and returns the result as a list that lazy loads the entities on first access. Entities are
* cached, so accessing the same entity more than once will not result in loading an entity from the underlying
* cursor again.Make sure to close it to close the underlying cursor.
*/
public LazyList listLazy() {
checkThread();
Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);
return new LazyList(daoAccess, cursor, true);
}
/**
* Executes the query and returns the result as a list that lazy loads the entities on every access (uncached).
* Make sure to close the list to close the underlying cursor.
*/
public LazyList listLazyUncached() {
checkThread();
Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);
return new LazyList(daoAccess, cursor, false);
}
/**
* Executes the query and returns the result as a list iterator; make sure to close it to close the underlying
* cursor. The cursor is closed once the iterator is fully iterated through.
*/
public CloseableListIterator listIterator() {
return listLazyUncached().listIteratorAutoClose();
}
/**
* Executes the query and returns the unique result or null.
*
* @return Entity or null if no matching entity was found
* @throws DaoException if the result is not unique
*/
public T unique() {
checkThread();
Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);
return daoAccess.loadUniqueAndCloseCursor(cursor);
}
/**
* Executes the query and returns the unique result (never null).
*
* @return Entity
* @throws DaoException if the result is not unique or no entity was found
*/
public T uniqueOrThrow() {
T entity = unique();
if (entity == null) {
throw new DaoException("No entity found for query");
}
return entity;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy