public class QueryHandler extends LitePalBase
| 限定符和类型 | 字段和说明 |
|---|---|
static java.lang.String |
TAG |
| 构造器和说明 |
|---|
QueryHandler(android.database.sqlite.SQLiteDatabase db)
Initialize
DataHandler.mDatabase for operating database. |
| 限定符和类型 | 方法和说明 |
|---|---|
protected void |
analyzeAssociatedModels(LitePalSupport baseObj,
java.util.Collection<AssociationsInfo> associationInfos)
Find all the associated models of currently model.
|
protected java.lang.Object |
createInstanceFromClass(java.lang.Class<?> modelClass)
Creates an instance from the passed in class.
|
protected java.lang.Object |
decryptValue(java.lang.String algorithm,
java.lang.Object fieldValue)
Decrypt the field value with targeted algorithm.
|
protected java.lang.Object |
encryptValue(java.lang.String algorithm,
java.lang.Object fieldValue)
Encrypt the field value with targeted algorithm.
|
protected java.lang.reflect.Constructor<?> |
findBestSuitConstructor(java.lang.Class<?> modelClass)
Finds the best suit constructor for creating an instance of a class.
|
protected LitePalSupport |
getAssociatedModel(LitePalSupport baseObj,
AssociationsInfo associationInfo)
Get the associated model.
|
protected java.util.Collection<LitePalSupport> |
getAssociatedModels(LitePalSupport baseObj,
AssociationsInfo associationInfo)
Get the associated models collection.
|
protected java.lang.Object[] |
getConstructorParams(java.lang.Class<?> modelClass,
java.lang.reflect.Constructor<?> constructor)
Depends on the passed in constructor, creating a parameters array with
initialized values for the constructor.
|
protected LitePalSupport |
getEmptyModel(LitePalSupport baseObj)
Create an empty instance of baseObj if it hasn't created one yet.
|
protected java.lang.Object |
getFieldValue(LitePalSupport dataSupport,
java.lang.reflect.Field field)
Get the field value for model.
|
protected java.util.List<AssociationsInfo> |
getForeignKeyAssociations(java.lang.String className,
boolean isEager)
Get the foreign key associations of the specified class.
|
protected java.lang.String |
getIntermediateTableName(LitePalSupport baseObj,
java.lang.String associatedTableName)
Get the name of intermediate join table.
|
protected java.lang.Class<?>[] |
getParameterTypes(java.lang.reflect.Field field,
java.lang.Object fieldValue,
java.lang.Object[] parameters)
Get the types of parameters for
ContentValues.put(java.lang.String, java.lang.String). |
protected java.lang.String |
getTableName(java.lang.Class<?> modelClass)
Get the simple name of modelClass.
|
protected java.lang.String[] |
getWhereArgs(java.lang.String... conditions)
Get the WHERE arguments to fill into where clause when updating or
deleting multiple rows.
|
protected java.lang.String |
getWhereClause(java.lang.String... conditions)
Get the WHERE clause to apply when updating or deleting multiple rows.
|
protected java.lang.String |
getWhereOfIdsWithOr(java.util.Collection<java.lang.Long> ids)
Get the where clause by the passed in id collection to apply multiple
rows.
|
protected java.lang.String |
getWhereOfIdsWithOr(long... ids)
Get the where clause by the passed in id array to apply multiple rows.
|
protected void |
giveBaseObjIdValue(LitePalSupport baseObj,
long id)
Assign the generated id value to
LitePalSupport.baseObjId. |
protected boolean |
isAffectAllLines(java.lang.Object... conditions)
Check the passing conditions represent to affect all lines or not.
|
protected java.lang.String |
makeGetterMethodName(java.lang.reflect.Field field)
Generate the getter method name by field, following the Android Studio rule.
|
protected java.lang.String |
makeSetterMethodName(java.lang.reflect.Field field)
Generate the setter method name by field, following the Android Studio rule.
|
protected <T> T |
mathQuery(java.lang.String tableName,
java.lang.String[] columns,
java.lang.String[] conditions,
java.lang.Class<T> type)
Handles the math query of the given table.
|
double |
onAverage(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions)
The open interface for other classes in CRUD package to calculate the
average value on a given column.
|
int |
onCount(java.lang.String tableName,
java.lang.String[] conditions)
The open interface for other classes in CRUD package to Count the
records.
|
<T> T |
onFind(java.lang.Class<T> modelClass,
long id,
boolean isEager)
The open interface for other classes in CRUD package to query a record
based on id.
|
<T> java.util.List<T> |
onFind(java.lang.Class<T> modelClass,
java.lang.String[] columns,
java.lang.String[] conditions,
java.lang.String orderBy,
java.lang.String limit,
boolean isEager)
The open interface for other classes in CRUD package to query multiple
records by parameters.
|
<T> java.util.List<T> |
onFindAll(java.lang.Class<T> modelClass,
boolean isEager,
long... ids)
The open interface for other classes in CRUD package to query multiple
records by an id array.
|
<T> T |
onFindFirst(java.lang.Class<T> modelClass,
boolean isEager)
The open interface for other classes in CRUD package to query the first
record in a table.
|
<T> T |
onFindLast(java.lang.Class<T> modelClass,
boolean isEager)
The open interface for other classes in CRUD package to query the last
record in a table.
|
<T> T |
onMax(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions,
java.lang.Class<T> type)
The open interface for other classes in CRUD package to calculate the
maximum value on a given column.
|
<T> T |
onMin(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions,
java.lang.Class<T> type)
The open interface for other classes in CRUD package to calculate the
minimum value on a given column.
|
<T> T |
onSum(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions,
java.lang.Class<T> type)
The open interface for other classes in CRUD package to calculate the sum
of values on a given column.
|
protected void |
putContentValuesForSave(LitePalSupport baseObj,
java.lang.reflect.Field field,
android.content.ContentValues values)
This method deals with the putting values job into ContentValues.
|
protected void |
putContentValuesForUpdate(LitePalSupport baseObj,
java.lang.reflect.Field field,
android.content.ContentValues values)
putContentValuesForUpdate operation is almost same with putContentValuesForSave, except allowing put null fieldValue into database,
which is made for
LitePalSupport.setToDefault(java.lang.String) function. |
protected void |
putFieldsValue(LitePalSupport baseObj,
java.util.List<java.lang.reflect.Field> supportedFields,
android.content.ContentValues values)
Iterate all the fields passed in.
|
protected <T> java.util.List<T> |
query(java.lang.Class<T> modelClass,
java.lang.String[] columns,
java.lang.String selection,
java.lang.String[] selectionArgs,
java.lang.String groupBy,
java.lang.String having,
java.lang.String orderBy,
java.lang.String limit,
java.util.List<AssociationsInfo> foreignKeyAssociations)
Query the table of the given model, returning a model list over the
result set.
|
protected void |
setFieldValue(LitePalSupport dataSupport,
java.lang.reflect.Field field,
java.lang.Object parameter)
Set the field value for model.
|
protected void |
setGenericValueToModel(LitePalSupport baseObj,
java.util.List<java.lang.reflect.Field> supportedGenericFields,
java.util.Map<java.lang.reflect.Field,GenericModel> genericModelMap)
Get generic value from generic tables, then set the value into the baseObj.
|
protected void |
setValueToModel(java.lang.Object modelInstance,
java.util.List<java.lang.reflect.Field> supportedFields,
java.util.List<AssociationsInfo> foreignKeyAssociations,
android.database.Cursor cursor,
android.util.SparseArray<org.litepal.crud.DataHandler.QueryInfoCache> sparseArray)
Get value from database by cursor, then set the value into modelInstance.
|
protected boolean |
shouldGetOrSet(LitePalSupport dataSupport,
java.lang.reflect.Field field)
When executing
getFieldValue(LitePalSupport, Field) or
setFieldValue(LitePalSupport, Field, Object), the
dataSupport and field passed in should be protected from null value. |
public static final java.lang.String TAG
public QueryHandler(android.database.sqlite.SQLiteDatabase db)
DataHandler.mDatabase for operating database. Do not
allow to create instance of QueryHandler out of CRUD package.db - The instance of SQLiteDatabase.public <T> T onFind(java.lang.Class<T> modelClass,
long id,
boolean isEager)
modelClass - Which table to query and the object type to return.id - Which record to query.isEager - True to load the associated models, false not.public <T> T onFindFirst(java.lang.Class<T> modelClass,
boolean isEager)
modelClass - Which table to query and the object type to return.isEager - True to load the associated models, false not.public <T> T onFindLast(java.lang.Class<T> modelClass,
boolean isEager)
modelClass - Which table to query and the object type to return.isEager - True to load the associated models, false not.public <T> java.util.List<T> onFindAll(java.lang.Class<T> modelClass,
boolean isEager,
long... ids)
modelClass - Which table to query and the object type to return as a list.isEager - True to load the associated models, false not.ids - Which records to query. Or do not pass it to find all records.public <T> java.util.List<T> onFind(java.lang.Class<T> modelClass,
java.lang.String[] columns,
java.lang.String[] conditions,
java.lang.String orderBy,
java.lang.String limit,
boolean isEager)
modelClass - Which table to query and the object type to return as a list.columns - A String array of which columns to return. Passing null will
return all columns.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.orderBy - How to order the rows, formatted as an SQL ORDER BY clause.
Passing null will use the default sort order, which may be
unordered.limit - Limits the number of rows returned by the query, formatted as
LIMIT clause.isEager - True to load the associated models, false not.public int onCount(java.lang.String tableName,
java.lang.String[] conditions)
tableName - Which table to query from.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.public double onAverage(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions)
tableName - Which table to query from.column - The based on column to calculate.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.public <T> T onMax(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions,
java.lang.Class<T> type)
tableName - Which table to query from.column - The based on column to calculate.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.type - The type of the based on column.public <T> T onMin(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions,
java.lang.Class<T> type)
tableName - Which table to query from.column - The based on column to calculate.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.type - The type of the based on column.public <T> T onSum(java.lang.String tableName,
java.lang.String column,
java.lang.String[] conditions,
java.lang.Class<T> type)
tableName - Which table to query from.column - The based on column to calculate.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.type - The type of the based on column.protected <T> java.util.List<T> query(java.lang.Class<T> modelClass,
java.lang.String[] columns,
java.lang.String selection,
java.lang.String[] selectionArgs,
java.lang.String groupBy,
java.lang.String having,
java.lang.String orderBy,
java.lang.String limit,
java.util.List<AssociationsInfo> foreignKeyAssociations)
modelClass - The model to compile the query against.columns - A list of which columns to return. Passing null will return
all columns, which is discouraged to prevent reading data from
storage that isn't going to be used.selection - A filter declaring which rows to return, formatted as an SQL
WHERE clause (excluding the WHERE itself). Passing null will
return all rows for the given table.selectionArgs - You may include ?s in selection, which will be replaced by the
values from selectionArgs, in order that they appear in the
selection. The values will be bound as Strings.groupBy - A filter declaring how to group rows, formatted as an SQL
GROUP BY clause (excluding the GROUP BY itself). Passing null
will cause the rows to not be grouped.having - A filter declare which row groups to include in the cursor, if
row grouping is being used, formatted as an SQL HAVING clause
(excluding the HAVING itself). Passing null will cause all row
groups to be included, and is required when row grouping is
not being used.orderBy - How to order the rows, formatted as an SQL ORDER BY clause
(excluding the ORDER BY itself). Passing null will use the
default sort order, which may be unordered.limit - Limits the number of rows returned by the query, formatted as
LIMIT clause. Passing null denotes no LIMIT clause.foreignKeyAssociations - Associated classes which have foreign keys in the current
model's table.protected <T> T mathQuery(java.lang.String tableName,
java.lang.String[] columns,
java.lang.String[] conditions,
java.lang.Class<T> type)
tableName - Which table to query from.columns - A list of which columns to return. Passing null will return
all columns, which is discouraged to prevent reading data from
storage that isn't going to be used.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.type - The type of the based on column.protected void giveBaseObjIdValue(LitePalSupport baseObj, long id) throws java.lang.SecurityException, java.lang.NoSuchFieldException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
LitePalSupport.baseObjId. This
value will be used as identify of this model for system use.baseObj - The class of base object.id - The value of id.java.lang.SecurityExceptionjava.lang.NoSuchFieldExceptionjava.lang.IllegalArgumentExceptionjava.lang.IllegalAccessExceptionprotected void putFieldsValue(LitePalSupport baseObj, java.util.List<java.lang.reflect.Field> supportedFields, android.content.ContentValues values) throws java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
putFieldsValueDependsOnSaveOrUpdate(LitePalSupport, java.lang.reflect.Field, android.content.ContentValues)
if it's not id field.baseObj - Current model to persist or update.supportedFields - List of all supported fields.values - To store data of current model for persisting or updating.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.NoSuchMethodExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionprotected void putContentValuesForSave(LitePalSupport baseObj, java.lang.reflect.Field field, android.content.ContentValues values) throws java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
baseObj - The class of base object.field - Field to put into ContentValues.values - To store data of current model for persisting or updating.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionprotected void putContentValuesForUpdate(LitePalSupport baseObj, java.lang.reflect.Field field, android.content.ContentValues values) throws java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
LitePalSupport.setToDefault(java.lang.String) function.baseObj - The class of base object.field - Field to put into ContentValues.values - To store data of current model for persisting or updating.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionprotected java.lang.Object encryptValue(java.lang.String algorithm,
java.lang.Object fieldValue)
algorithm - The algorithm to encrypt value.fieldValue - Field value to encrypt.protected java.lang.Object getFieldValue(LitePalSupport dataSupport, java.lang.reflect.Field field) throws java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
dataSupport - The model to get method from.field - Use to generate getter method name.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.IllegalAccessExceptionprotected void setFieldValue(LitePalSupport dataSupport, java.lang.reflect.Field field, java.lang.Object parameter) throws java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
dataSupport - The model to set method to.field - Use to generate setter method name.parameter - The parameter to invoke setter method.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.IllegalAccessExceptionprotected void analyzeAssociatedModels(LitePalSupport baseObj, java.util.Collection<AssociationsInfo> associationInfos)
baseObj - The class of base object.protected LitePalSupport getAssociatedModel(LitePalSupport baseObj, AssociationsInfo associationInfo) throws java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
baseObj - The instance of self model.associationInfo - To get the associated model.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.IllegalAccessExceptionprotected java.util.Collection<LitePalSupport> getAssociatedModels(LitePalSupport baseObj, AssociationsInfo associationInfo) throws java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
baseObj - The instance of self model.associationInfo - To get the associated models collection.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.IllegalAccessExceptionprotected LitePalSupport getEmptyModel(LitePalSupport baseObj)
tempEmptyModel, no
need to create a new one.baseObj - Current model to update.protected java.lang.String getWhereClause(java.lang.String... conditions)
conditions - A string array representing the WHERE part of an SQL
statement.protected java.lang.String[] getWhereArgs(java.lang.String... conditions)
conditions - A string array representing the WHERE part of an SQL
statement.protected boolean isAffectAllLines(java.lang.Object... conditions)
conditions - An array representing the WHERE part of an SQL statement.protected java.lang.String getWhereOfIdsWithOr(java.util.Collection<java.lang.Long> ids)
ids - The id collection.protected java.lang.String getWhereOfIdsWithOr(long... ids)
ids - The id collection.protected boolean shouldGetOrSet(LitePalSupport dataSupport, java.lang.reflect.Field field)
getFieldValue(LitePalSupport, Field) or
setFieldValue(LitePalSupport, Field, Object), the
dataSupport and field passed in should be protected from null value.dataSupport - The object to execute set or get method.field - The field of generating set and get methods.protected java.lang.String getIntermediateTableName(LitePalSupport baseObj, java.lang.String associatedTableName)
baseObj - Current model.associatedTableName - The name of associated table.protected java.lang.String getTableName(java.lang.Class<?> modelClass)
modelClass - Class of model to get table name from.protected java.lang.Object createInstanceFromClass(java.lang.Class<?> modelClass)
findBestSuitConstructor(Class) method.modelClass - The class to create instance.protected java.lang.reflect.Constructor<?> findBestSuitConstructor(java.lang.Class<?> modelClass)
modelClass - To get constructors from.protected java.lang.Object[] getConstructorParams(java.lang.Class<?> modelClass,
java.lang.reflect.Constructor<?> constructor)
modelClass - The original class the this constructor belongs to.constructor - The constructor to get parameters for it.protected void setValueToModel(java.lang.Object modelInstance,
java.util.List<java.lang.reflect.Field> supportedFields,
java.util.List<AssociationsInfo> foreignKeyAssociations,
android.database.Cursor cursor,
android.util.SparseArray<org.litepal.crud.DataHandler.QueryInfoCache> sparseArray)
throws java.lang.SecurityException,
java.lang.IllegalArgumentException,
java.lang.NoSuchMethodException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
modelInstance - The model to set into.supportedFields - Corresponding to each column in database.foreignKeyAssociations - Associated classes which have foreign keys in the current
model's table.cursor - Use to get value from database.sparseArray - Use SparseArray to cache the query information at first loop. Then the rest loop
can get query information directly to speed up.java.lang.SecurityExceptionjava.lang.IllegalArgumentExceptionjava.lang.NoSuchMethodExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionprotected void setGenericValueToModel(LitePalSupport baseObj, java.util.List<java.lang.reflect.Field> supportedGenericFields, java.util.Map<java.lang.reflect.Field,GenericModel> genericModelMap) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
baseObj - The model to set into.supportedGenericFields - List of all supported generic fields.genericModelMap - Use HashMap to cache the query information at first loop. Then the rest loop can
get query information directly to speed up.java.lang.NoSuchMethodExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionprotected java.util.List<AssociationsInfo> getForeignKeyAssociations(java.lang.String className, boolean isEager)
className - The full class name.isEager - True to load the associated models, false not.protected java.lang.Class<?>[] getParameterTypes(java.lang.reflect.Field field,
java.lang.Object fieldValue,
java.lang.Object[] parameters)
ContentValues.put(java.lang.String, java.lang.String). Need two
parameters. First is String type for key. Second is depend on field for
value.field - The field to get parameter type.fieldValue - Value of the field. Only used to convert to String when the
field is char.parameters - If the field is char, convert the value to String at index 1.ContentValues.put(java.lang.String, java.lang.String).protected java.lang.String makeGetterMethodName(java.lang.reflect.Field field)
field - The field to generate getter method from.protected java.lang.String makeSetterMethodName(java.lang.reflect.Field field)
field - The field to generate setter method from.protected java.lang.Object decryptValue(java.lang.String algorithm,
java.lang.Object fieldValue)
algorithm - The algorithm to decrypt value.fieldValue - Field value to decrypt.