com.todoroo.andlib.data
Class DatabaseDao<TYPE extends AbstractModel>

java.lang.Object
  extended by com.todoroo.andlib.data.DatabaseDao<TYPE>

public class DatabaseDao<TYPE extends AbstractModel>
extends java.lang.Object

DAO for reading data from an instance of AbstractDatabase. If you are writing an add-on for Astrid, you probably want to be using a subclass of ContentResolverDao instead.

Author:
Tim Su

Nested Class Summary
static interface DatabaseDao.ModelUpdateListener<MTYPE>
           
 
Constructor Summary
DatabaseDao(java.lang.Class<TYPE> modelClass)
           
DatabaseDao(java.lang.Class<TYPE> modelClass, AbstractDatabase database)
           
 
Method Summary
 void addListener(DatabaseDao.ModelUpdateListener<TYPE> listener)
           
 boolean createNew(TYPE item)
          Creates the given item.
 boolean delete(long id)
          Delete the given id
 int deleteWhere(Criterion where)
          Delete all matching a clause
 TYPE fetch(long id, Property<?>... properties)
          Returns object corresponding to the given identifier
 Table getTable()
          Gets table associated with this DAO
 boolean persist(TYPE item)
          Save the given object to the database.
 TodorooCursor<TYPE> query(Query query)
          Construct a query with SQL DSL objects
 TodorooCursor<TYPE> rawQuery(java.lang.String selection, java.lang.String[] selectionArgs, Property<?>... properties)
          Construct a query with raw SQL
 boolean saveExisting(TYPE item)
          Saves the given item.
 void setDatabase(AbstractDatabase database)
          Sets database accessed by this DAO.
 int update(Criterion where, TYPE template)
          Update all matching a clause to have the values set on template object.
 int updateMultiple(android.content.ContentValues values, Criterion criterion)
          Updates multiple rows of the database based on model set values
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseDao

public DatabaseDao(java.lang.Class<TYPE> modelClass)

DatabaseDao

public DatabaseDao(java.lang.Class<TYPE> modelClass,
                   AbstractDatabase database)
Method Detail

getTable

public Table getTable()
Gets table associated with this DAO


setDatabase

public void setDatabase(AbstractDatabase database)
Sets database accessed by this DAO. Used for dependency-injected initialization by child classes and unit tests

Parameters:
database -

addListener

public void addListener(DatabaseDao.ModelUpdateListener<TYPE> listener)

query

public TodorooCursor<TYPE> query(Query query)
Construct a query with SQL DSL objects

Parameters:
query -
Returns:

rawQuery

public TodorooCursor<TYPE> rawQuery(java.lang.String selection,
                                    java.lang.String[] selectionArgs,
                                    Property<?>... properties)
Construct a query with raw SQL

Parameters:
properties -
selection -
selectionArgs -
Returns:

fetch

public TYPE fetch(long id,
                  Property<?>... properties)
Returns object corresponding to the given identifier

Parameters:
database -
table - name of table
properties - properties to read
id - id of item
Returns:
null if no item found

delete

public boolean delete(long id)
Delete the given id

Parameters:
database -
id -
Returns:
true if delete was successful

deleteWhere

public int deleteWhere(Criterion where)
Delete all matching a clause

Parameters:
where - predicate for deletion
Returns:
# of deleted items

update

public int update(Criterion where,
                  TYPE template)
Update all matching a clause to have the values set on template object.

Example (updates "joe" => "bob" in metadata value1): {code} Metadata item = new Metadata(); item.setValue(Metadata.VALUE1, "bob"); update(item, Metadata.VALUE1.eq("joe")); {code}

Parameters:
where - sql criteria
template - set fields on this object in order to set them in the db.
Returns:
# of updated items

persist

public boolean persist(TYPE item)
Save the given object to the database. Creates a new object if model id property has not been set

Returns:
true on success.

createNew

public boolean createNew(TYPE item)
Creates the given item.

Parameters:
database -
table - table name
item - item model
Returns:
returns true on success.

saveExisting

public boolean saveExisting(TYPE item)
Saves the given item. Will not create a new item!

Parameters:
database -
table - table name
item - item model
Returns:
returns true on success.

updateMultiple

public int updateMultiple(android.content.ContentValues values,
                          Criterion criterion)
Updates multiple rows of the database based on model set values

Parameters:
item - item model
criterion -
Returns:
returns true on success.