package org.litepal.crud;

import java.util.List;

import android.database.sqlite.SQLiteDatabase;

/**
 * This is a component under DataSupport. It deals with query stuff as primary
 * task.
 * 
 * @author Tony Green
 * @since 1.1
 */
class QueryHandler extends DataHandler {

	/**
	 * Initialize {@link DataHandler#mDatabase} for operating database. Do not
	 * allow to create instance of QueryHandler out of CRUD package.
	 * 
	 * @param db
	 *            The instance of SQLiteDatabase.
	 */
	QueryHandler(SQLiteDatabase db) {
		mDatabase = db;
	}

	/**
	 * The open interface for other classes in CRUD package to query a record
	 * based on id. If the result set is empty, gives null back.
	 * 
	 * @param modelClass
	 *            Which table to query and the object type to return.
	 * @param id
	 *            Which record to query.
	 * @return An object with founded data from database, or null.
	 */
	<T> T onFind(Class<T> modelClass, long id) {
		List<T> dataList = query(modelClass, null, "id = ?", new String[] { String.valueOf(id) },
				null, null, null, null);
		if (dataList.size() > 0) {
			return dataList.get(0);
		}
		return null;
	}

	/**
	 * The open interface for other classes in CRUD package to query the first
	 * record in a table. If the result set is empty, gives null back.
	 * 
	 * @param modelClass
	 *            Which table to query and the object type to return.
	 * @return An object with data of first row, or null.
	 */
	<T> T onFindFirst(Class<T> modelClass) {
		List<T> dataList = query(modelClass, null, null, null, null, null, "id", "1");
		if (dataList.size() > 0) {
			return dataList.get(0);
		}
		return null;
	}

	/**
	 * The open interface for other classes in CRUD package to query the last
	 * record in a table. If the result set is empty, gives null back.
	 * 
	 * @param modelClass
	 *            Which table to query and the object type to return.
	 * @return An object with data of last row, or null.
	 */
	<T> T onFindLast(Class<T> modelClass) {
		List<T> dataList = query(modelClass, null, null, null, null, null, "id desc", "1");
		if (dataList.size() > 0) {
			return dataList.get(0);
		}
		return null;
	}

	/**
	 * The open interface for other classes in CRUD package to query multiple
	 * records by an id array. Pass no ids means query all rows.
	 * 
	 * @param modelClass
	 *            Which table to query and the object type to return as a list.
	 * @param ids
	 *            Which records to query. Or do not pass it to find all records.
	 * @return An object list with founded data from database, or an empty list.
	 */
	<T> List<T> onFindAll(Class<T> modelClass, long... ids) {
		List<T> dataList;
		if (isAffectAllLines(ids)) {
			dataList = query(modelClass, null, null, null, null, null, "id", null);
		} else {
			dataList = query(modelClass, null, getWhereOfIdsWithOr(ids), null, null, null, "id",
					null);
		}
		return dataList;
	}

	<T> List<T> onFind(Class<T> modelClass, String[] columns, String[] conditions, String orderBy,
			String limit) {
		checkConditionsCorrect(conditions);
		List<T> dataList = query(modelClass, columns, getWhereClause(conditions),
				getWhereArgs(conditions), null, null, orderBy, limit);
		return dataList;
	}

}
@@ -2905,16 +2905,853 @@
 st;%0A%09%7D%0A%0A
+%09/**%0A%09 * The open interface for other classes in CRUD package to query multiple%0A%09 * records by parameters.%0A%09 * %0A%09 * @param modelClass%0A%09 *            Which table to query and the object type to return as a list.%0A%09 * @param columns%0A%09 *            A String array of which columns to return. Passing null will%0A%09 *            return all columns.%0A%09 * @param conditions%0A%09 *            A filter declaring which rows to return, formatted as an SQL%0A%09 *            WHERE clause. Passing null will return all rows.%0A%09 * @param orderBy%0A%09 *            How to order the rows, formatted as an SQL ORDER BY clause.%0A%09 *            Passing null will use the default sort order, which may be%0A%09 *            unordered.%0A%09 * @param limit%0A%09 *            Limits the number of rows returned by the query, formatted as%0A%09 *            LIMIT clause.%0A%09 * @return%0A%09 */%0A
 %09%3CT%3E Lis

Add annotations.