public class DataSupport
extends java.lang.Object
public class Person extends DataSupport {
private int id;
private String name;
private int age;
}
The Person class is automatically mapped to the table named "person",
which might look like this:
CREATE TABLE person (
id integer primary key autoincrement,
age integer,
name text
);
| 限定符 | 构造器和说明 |
|---|---|
protected |
DataSupport()
Disable developers to create instance of DataSupport directly.
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
assignBaseObjId(int baseObjId)
Assigns value to baseObjId.
|
static double |
average(java.lang.Class<?> modelClass,
java.lang.String column)
Calculates the average value on a given column.
|
static double |
average(java.lang.String tableName,
java.lang.String column)
Calculates the average value on a given column.
|
void |
clearSavedState()
It model is saved, clear the saved state and model becomes unsaved.
|
static int |
count(java.lang.Class<?> modelClass)
Count the records.
|
static int |
count(java.lang.String tableName)
Count the records.
|
int |
delete()
Deletes the record in the database.
|
static int |
delete(java.lang.Class<?> modelClass,
long id)
Deletes the record in the database by id.
|
static int |
deleteAll(java.lang.Class<?> modelClass,
java.lang.String... conditions)
Deletes all records with details given if they match a set of conditions
supplied.
|
static int |
deleteAll(java.lang.String tableName,
java.lang.String... conditions)
Deletes all records with details given if they match a set of conditions
supplied.
|
static <T> T |
find(java.lang.Class<T> modelClass,
long id)
Finds the record by a specific id.
|
static <T> T |
find(java.lang.Class<T> modelClass,
long id,
boolean isEager)
It is mostly same as
find(Class, long) but an isEager
parameter. |
static <T> java.util.List<T> |
findAll(java.lang.Class<T> modelClass,
boolean isEager,
long... ids)
It is mostly same as
findAll(Class, long...) but an
isEager parameter. |
static <T> java.util.List<T> |
findAll(java.lang.Class<T> modelClass,
long... ids)
Finds multiple records by an id array.
|
static android.database.Cursor |
findBySQL(java.lang.String... sql)
Runs the provided SQL and returns a Cursor over the result set.
|
static <T> T |
findFirst(java.lang.Class<T> modelClass)
Finds the first record of a single table.
|
static <T> T |
findFirst(java.lang.Class<T> modelClass,
boolean isEager)
It is mostly same as
findFirst(Class) but an isEager
parameter. |
static <T> T |
findLast(java.lang.Class<T> modelClass)
Finds the last record of a single table.
|
static <T> T |
findLast(java.lang.Class<T> modelClass,
boolean isEager)
It is mostly same as
findLast(Class) but an isEager
parameter. |
protected long |
getBaseObjId()
Get the baseObjId of this model if it's useful for developers.
|
protected java.lang.String |
getClassName()
Get the full class name of self.
|
protected java.lang.String |
getTableName()
Get the corresponding table name of current model.
|
static <T> boolean |
isExist(java.lang.Class<T> modelClass,
java.lang.String... conditions)
Check if the specified conditions data already exists in the table.
|
boolean |
isSaved()
Current model is saved or not.
|
static ClusterQuery |
limit(int value)
Limits the number of rows returned by the query.
|
static <T extends DataSupport> |
markAsDeleted(java.util.Collection<T> collection)
Provide a way to mark all models in collection as deleted.
|
static <T> T |
max(java.lang.Class<?> modelClass,
java.lang.String columnName,
java.lang.Class<T> columnType)
Calculates the maximum value on a given column.
|
static <T> T |
max(java.lang.String tableName,
java.lang.String columnName,
java.lang.Class<T> columnType)
Calculates the maximum value on a given column.
|
static <T> T |
min(java.lang.Class<?> modelClass,
java.lang.String columnName,
java.lang.Class<T> columnType)
Calculates the minimum value on a given column.
|
static <T> T |
min(java.lang.String tableName,
java.lang.String columnName,
java.lang.Class<T> columnType)
Calculates the minimum value on a given column.
|
static ClusterQuery |
offset(int value)
Declaring the offset of rows returned by the query.
|
static ClusterQuery |
order(java.lang.String column)
Declaring how to order the rows queried from table.
|
boolean |
save()
Saves the model.
|
static <T extends DataSupport> |
saveAll(java.util.Collection<T> collection)
Saves the collection into database.
|
boolean |
saveFast()
Saves the model ignore associations, so that the saving process will be faster.
|
boolean |
saveIfNotExist(java.lang.String... conditions)
Saves the model only when the conditions data not exist.
|
void |
saveThrows()
Saves the model.
|
static ClusterQuery |
select(java.lang.String... columns)
Declaring to query which columns in table.
|
void |
setToDefault(java.lang.String fieldName)
When updating database with
update(long), you must
use this method to update a field into default value. |
static <T> T |
sum(java.lang.Class<?> modelClass,
java.lang.String columnName,
java.lang.Class<T> columnType)
Calculates the sum of values on a given column.
|
static <T> T |
sum(java.lang.String tableName,
java.lang.String columnName,
java.lang.Class<T> columnType)
Calculates the sum of values on a given column.
|
static int |
update(java.lang.Class<?> modelClass,
android.content.ContentValues values,
long id)
Updates the corresponding record by id with ContentValues.
|
int |
update(long id)
Updates the corresponding record by id.
|
static int |
updateAll(java.lang.Class<?> modelClass,
android.content.ContentValues values,
java.lang.String... conditions)
Updates all records with details given if they match a set of conditions
supplied.
|
int |
updateAll(java.lang.String... conditions)
Updates all records with details given if they match a set of conditions
supplied.
|
static int |
updateAll(java.lang.String tableName,
android.content.ContentValues values,
java.lang.String... conditions)
Updates all records with details given if they match a set of conditions
supplied.
|
static ClusterQuery |
where(java.lang.String... conditions)
Declaring to query which rows in table.
|
protected DataSupport()
public static ClusterQuery select(java.lang.String... columns)
DataSupport.select("name", "age").find(Person.class);
This will find all rows with name and age columns in Person table.columns - A String array of which columns to return. Passing null will
return all columns.public static ClusterQuery where(java.lang.String... conditions)
DataSupport.where("name = ? or age > ?", "Tom", "14").find(Person.class);
This will find rows which name is Tom or age greater than 14 in Person
table.conditions - A filter declaring which rows to return, formatted as an SQL
WHERE clause. Passing null will return all rows.public static ClusterQuery order(java.lang.String column)
DataSupport.order("name desc").find(Person.class);
This will find all rows in Person table sorted by name with inverted
order.column - How to order the rows, formatted as an SQL ORDER BY clause.
Passing null will use the default sort order, which may be
unordered.public static ClusterQuery limit(int value)
DataSupport.limit(2).find(Person.class);This will find the top 2 rows in Person table.
value - Limits the number of rows returned by the query, formatted as
LIMIT clause.public static ClusterQuery offset(int value)
limit(int), or nothing will return.
DataSupport.limit(1).offset(2).find(Person.class);This will find the third row in Person table.
value - The offset amount of rows returned by the query.public static int count(java.lang.Class<?> modelClass)
DataSupport.count(Person.class);This will count all rows in person table.
DataSupport.where("age > ?", "15").count(Person.class);
modelClass - Which table to query from by class.public static int count(java.lang.String tableName)
DataSupport.count("person");
This will count all rows in person table.
DataSupport.where("age > ?", "15").count("person");
tableName - Which table to query from.public static double average(java.lang.Class<?> modelClass,
java.lang.String column)
DataSupport.average(Person.class, "age");You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").average(Person.class, "age");
modelClass - Which table to query from by class.column - The based on column to calculate.public static double average(java.lang.String tableName,
java.lang.String column)
DataSupport.average("person", "age");
You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").average("person", "age");
tableName - Which table to query from.column - The based on column to calculate.public static <T> T max(java.lang.Class<?> modelClass,
java.lang.String columnName,
java.lang.Class<T> columnType)
DataSupport.max(Person.class, "age", int.class);You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").max(Person.class, "age", Integer.TYPE);
modelClass - Which table to query from by class.columnName - The based on column to calculate.columnType - The type of the based on column.public static <T> T max(java.lang.String tableName,
java.lang.String columnName,
java.lang.Class<T> columnType)
DataSupport.max("person", "age", int.class);
You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").max("person", "age", Integer.TYPE);
tableName - Which table to query from.columnName - The based on column to calculate.columnType - The type of the based on column.public static <T> T min(java.lang.Class<?> modelClass,
java.lang.String columnName,
java.lang.Class<T> columnType)
DataSupport.min(Person.class, "age", int.class);You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").min(Person.class, "age", Integer.TYPE);
modelClass - Which table to query from by class.columnName - The based on column to calculate.columnType - The type of the based on column.public static <T> T min(java.lang.String tableName,
java.lang.String columnName,
java.lang.Class<T> columnType)
DataSupport.min("person", "age", int.class);
You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").min("person", "age", Integer.TYPE);
tableName - Which table to query from.columnName - The based on column to calculate.columnType - The type of the based on column.public static <T> T sum(java.lang.Class<?> modelClass,
java.lang.String columnName,
java.lang.Class<T> columnType)
DataSupport.sum(Person.class, "age", int.class);You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").sum(Person.class, "age", Integer.TYPE);
modelClass - Which table to query from by class.columnName - The based on column to calculate.columnType - The type of the based on column.public static <T> T sum(java.lang.String tableName,
java.lang.String columnName,
java.lang.Class<T> columnType)
DataSupport.sum("person", "age", int.class);
You can also specify a where clause when calculating.
DataSupport.where("age > ?", "15").sum("person", "age", Integer.TYPE);
tableName - Which table to query from.columnName - The based on column to calculate.columnType - The type of the based on column.public static <T> T find(java.lang.Class<T> modelClass,
long id)
Person p = DataSupport.find(Person.class, 1);The modelClass determines which table to query and the object type to return. If no record can be found, then return null.
find(Class, long, boolean).modelClass - Which table to query and the object type to return.id - Which record to query.public static <T> T find(java.lang.Class<T> modelClass,
long id,
boolean isEager)
find(Class, long) but an isEager
parameter. If set true the associated models will be loaded as well.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 static <T> T findFirst(java.lang.Class<T> modelClass)
Person p = DataSupport.findFirst(Person.class);Note that the associated models won't be loaded by default considering the efficiency, but you can do that by using
findFirst(Class, boolean).modelClass - Which table to query and the object type to return.public static <T> T findFirst(java.lang.Class<T> modelClass,
boolean isEager)
findFirst(Class) but an isEager
parameter. If set true the associated models will be loaded as well.modelClass - Which table to query and the object type to return.isEager - True to load the associated models, false not.public static <T> T findLast(java.lang.Class<T> modelClass)
Person p = DataSupport.findLast(Person.class);Note that the associated models won't be loaded by default considering the efficiency, but you can do that by using
findLast(Class, boolean).modelClass - Which table to query and the object type to return.public static <T> T findLast(java.lang.Class<T> modelClass,
boolean isEager)
findLast(Class) but an isEager
parameter. If set true the associated models will be loaded as well.modelClass - Which table to query and the object type to return.isEager - True to load the associated models, false not.public static <T> java.util.List<T> findAll(java.lang.Class<T> modelClass,
long... ids)
List<Person> people = DataSupport.findAll(Person.class, 1, 2, 3);
long[] bookIds = { 10, 18 };
List<Book> books = DataSupport.findAll(Book.class, bookIds);
Of course you can find all records by passing nothing to the ids
parameter.
List<Book> allBooks = DataSupport.findAll(Book.class);Note that the associated models won't be loaded by default considering the efficiency, but you can do that by using
findAll(Class, boolean, long...).
The modelClass determines which table to query and the object type to
return.modelClass - Which table to query and the object type to return as a list.ids - Which records to query. Or do not pass it to find all records.public static <T> java.util.List<T> findAll(java.lang.Class<T> modelClass,
boolean isEager,
long... ids)
findAll(Class, long...) but an
isEager parameter. If set true the associated models will be loaded as
well.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 static android.database.Cursor findBySQL(java.lang.String... sql)
Cursor cursor = DataSupport.findBySQL("select * from person where name=? and age=?", "Tom", "14");
sql - First parameter is the SQL clause to apply. Second to the last
parameters will replace the place holders.public static int delete(java.lang.Class<?> modelClass,
long id)
DataSupport.delete(Person.class, 1);This means that the record 1 in person table will be removed.
modelClass - Which table to delete from by class.id - Which record to delete.public static int deleteAll(java.lang.Class<?> modelClass,
java.lang.String... conditions)
DataSupport.deleteAll(Person.class, "name = ? and age = ?", "Tom", "14");This means that all the records which name is Tom and age is 14 will be removed.
modelClass - Which table to delete from by class.conditions - A string array representing the WHERE part of an SQL
statement. First parameter is the WHERE clause to apply when
deleting. The way of specifying place holders is to insert one
or more question marks in the SQL. The first question mark is
replaced by the second element of the array, the next question
mark by the third, and so on. Passing empty string will update
all rows.public static int deleteAll(java.lang.String tableName,
java.lang.String... conditions)
DataSupport.deleteAll("person", "name = ? and age = ?", "Tom", "14");
This means that all the records which name is Tom and age is 14 will be
removed.tableName - Which table to delete from.conditions - A string array representing the WHERE part of an SQL
statement. First parameter is the WHERE clause to apply when
deleting. The way of specifying place holders is to insert one
or more question marks in the SQL. The first question mark is
replaced by the second element of the array, the next question
mark by the third, and so on. Passing empty string will update
all rows.public static int update(java.lang.Class<?> modelClass,
android.content.ContentValues values,
long id)
ContentValues cv = new ContentValues();
cv.put("name", "Jim");
DataSupport.update(Person.class, cv, 1);
This means that the name of record 1 will be updated into Jim.modelClass - Which table to update by class.values - A map from column names to new column values. null is a valid
value that will be translated to NULL.id - Which record to update.public static int updateAll(java.lang.Class<?> modelClass,
android.content.ContentValues values,
java.lang.String... conditions)
ContentValues cv = new ContentValues();
cv.put("name", "Jim");
DataSupport.update(Person.class, cv, "name = ?", "Tom");
This means that all the records which name is Tom will be updated into
Jim.modelClass - Which table to update by class.values - A map from column names to new column values. null is a valid
value that will be translated to NULL.conditions - A string array representing the WHERE part of an SQL
statement. First parameter is the WHERE clause to apply when
updating. The way of specifying place holders is to insert one
or more question marks in the SQL. The first question mark is
replaced by the second element of the array, the next question
mark by the third, and so on. Passing empty string will update
all rows.public static int updateAll(java.lang.String tableName,
android.content.ContentValues values,
java.lang.String... conditions)
ContentValues cv = new ContentValues();
cv.put("name", "Jim");
DataSupport.update("person", cv, "name = ?", "Tom");
This means that all the records which name is Tom will be updated into
Jim.tableName - Which table to update.values - A map from column names to new column values. null is a valid
value that will be translated to NULL.conditions - A string array representing the WHERE part of an SQL
statement. First parameter is the WHERE clause to apply when
updating. The way of specifying place holders is to insert one
or more question marks in the SQL. The first question mark is
replaced by the second element of the array, the next question
mark by the third, and so on. Passing empty string will update
all rows.public static <T extends DataSupport> void saveAll(java.util.Collection<T> collection)
DataSupport.saveAll(people);If the model in collection is a new record gets created in the database, otherwise the existing record gets updated.
for (Person person : people) {
person.save();
}
So when your collection holds huge of models,
saveAll(java.util.Collection) is the better choice.collection - Holds all models to save.public static <T extends DataSupport> void markAsDeleted(java.util.Collection<T> collection)
collection - Collection of models which want to mark as deleted and clear their save state.public static <T> boolean isExist(java.lang.Class<T> modelClass,
java.lang.String... conditions)
modelClass - Which table to check by class.conditions - A filter declaring which data to check. Exactly same use as
where(String...), except null conditions will result in false.public int delete()
Person person;
....
if (person.isSaved()) {
person.delete();
}
public int update(long id)
Person person = new Person();
person.setName("Jim");
person.update(1);
This means that the name of record 1 will be updated into Jim.setToDefault(String) to update
columns into default value. 2. This method couldn't update foreign key in
database. So do not use setXxx to set associations between models.id - Which record to update.public int updateAll(java.lang.String... conditions)
Person person = new Person();
person.setName("Jim");
person.updateAll("name = ?", "Tom");
This means that all the records which name is Tom will be updated into
Jim.setToDefault(String) to update
columns into default value. 2. This method couldn't update foreign key in
database. So do not use setXxx to set associations between models.conditions - A string array representing the WHERE part of an SQL
statement. First parameter is the WHERE clause to apply when
updating. The way of specifying place holders is to insert one
or more question marks in the SQL. The first question mark is
replaced by the second element of the array, the next question
mark by the third, and so on. Passing empty string will update
all rows.public boolean save()
Person person = new Person();
person.setName("Tom");
person.setAge(22);
person.save();
If the model is a new record gets created in the database, otherwise the
existing record gets updated.public void saveThrows()
Person person = new Person();
person.setName("Tom");
person.setAge(22);
person.saveThrows();
If the model is a new record gets created in the database, otherwise the
existing record gets updated.DataSupportExceptionpublic boolean saveIfNotExist(java.lang.String... conditions)
Person person = new Person();
person.setName("Tom");
person.setAge(22);
person.saveIfNotExists("name = ?", "Tom");
If person table doesn't have a name with Tom, a new record gets created in the database,
otherwise the saving operation will be ignored.public boolean saveFast()
Person person = new Person();
person.setName("Tom");
person.setAge(22);
person.save();
If the model is a new record gets created in the database, otherwise the
existing record gets updated.public boolean isSaved()
public void clearSavedState()
public void setToDefault(java.lang.String fieldName)
update(long), you must
use this method to update a field into default value. Use setXxx with
default value of the model won't update anything. fieldName - The name of field to update into default value.public void assignBaseObjId(int baseObjId)
baseObjId - protected long getBaseObjId()
protected java.lang.String getClassName()
protected java.lang.String getTableName()