org.androwrapee.db
Class DefaultDAO<T>

java.lang.Object
  extended by org.androwrapee.db.DefaultDAO<T>
Type Parameters:
T - the type of the classes manipulated by the DefaultDAO.

public class DefaultDAO<T>
extends Object

Simple database access helper class, for SQLite for Android. Defines the basic CRUD operations for the and gives the ability to insert, get, update or delete objects from the database.

Uses Java Reflection for creating the database entries. This has some implications and restrictions:


Field Summary
static String BOOLEAN_FALSE_VALUE
           
static String BOOLEAN_TRUE_VALUE
           
static String ID_PREPENDER
           
static String REFERENCE_PREPENDER
           
 
Constructor Summary
DefaultDAO(Class<T> c, DefaultDatabaseHelper dbHelper, ReflectionManager rm, String tableName)
          Constructor - takes the context to allow the database to be opened/created.
 
Method Summary
 T buildObject(Cursor cursor)
          Builds the object based on a given cursor.
 void close()
          Close the database.
 int countEntries(String whereClause)
          Count the entries in the database that match a given whereClause.
 boolean delete(long rowId)
          Delete the entry with the given rowId.
 int delete(String whereClause)
          Delete all the entries that match a given whereClause.
 T fetch(long rowId)
          Return the object positioned at the entry that matches the given id.
 ArrayList<T> fetchAll(String where)
          Fetches all the object in the database that match a given where clause.
 Cursor fetchCursor(long rowId)
          Return the cursor positioned at the entry that matches the given id.
 Cursor fetchCursor(String whereClause)
          Return the cursor positioned at the first entry of the results of the query with the provided whereClause.
 SQLiteDatabase getDatabaseReference()
          Gets the database reference that can be used to manually do queries.
 long getReferenceId(Cursor cursor, String fieldName)
          Gets the id for a referenced entry.
 long insert(T newObject, boolean generateID)
          Inserts a new entry in the database for the object provided.
 DefaultDAO<T> open()
          Open the database.
 boolean update(T object, long rowId)
          Update the entry in the database corresponding to the provided object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ID_PREPENDER

public static final String ID_PREPENDER
See Also:
Constant Field Values

REFERENCE_PREPENDER

public static final String REFERENCE_PREPENDER
See Also:
Constant Field Values

BOOLEAN_TRUE_VALUE

public static final String BOOLEAN_TRUE_VALUE
See Also:
Constant Field Values

BOOLEAN_FALSE_VALUE

public static final String BOOLEAN_FALSE_VALUE
See Also:
Constant Field Values
Constructor Detail

DefaultDAO

public DefaultDAO(Class<T> c,
                  DefaultDatabaseHelper dbHelper,
                  ReflectionManager rm,
                  String tableName)
Constructor - takes the context to allow the database to be opened/created.

Parameters:
c - the class
dbHelper - the db helper
rm - the rm
tableName - the table name
Method Detail

open

public DefaultDAO<T> open()
                   throws SQLException
Open the database. If it cannot be opened, try to create a new instance of the database. If it cannot be created, throw an exception to signal the failure

Returns:
this (self reference, allowing this to be chained in an initialization call)
Throws:
SQLException - if the database could be neither opened or created

close

public void close()
Close the database.


insert

public long insert(T newObject,
                   boolean generateID)
Inserts a new entry in the database for the object provided. If the entry is successfully created return the new rowId for that entry, otherwise return a -1 to indicate failure.

Not that, for efficiency reasons, the id field in the object is not set by this method, but it's returned, so it can be set manually as needed.

The generateID parameter sets the way the database id field is filled. If the value is true, the id is generated by the database, otherwise, the value of the id field from the object is used. In the latter case, it is the responsibility of the user to guaranty the uniqueness of the id value.

Parameters:
newObject - the new object
generateID - whether the database id is generated by the database or the field in the object is used as an id.
Returns:
rowId or -1 if failed

update

public boolean update(T object,
                      long rowId)
Update the entry in the database corresponding to the provided object. The row id is given separately.

Parameters:
object - the object
rowId - the row id
Returns:
true if the object was successfully updated, false otherwise

delete

public boolean delete(long rowId)
Delete the entry with the given rowId.

Parameters:
rowId - id of the object to delete
Returns:
true if anything was deleted, false otherwise

delete

public int delete(String whereClause)
Delete all the entries that match a given whereClause.

Parameters:
whereClause - the where clause
Returns:
the count of rows affected, or 0 otherwise.

fetch

public T fetch(long rowId)
Return the object positioned at the entry that matches the given id.

Parameters:
rowId - id of object to retrieve
Returns:
the object fetched from the database

fetchCursor

public Cursor fetchCursor(long rowId)
Return the cursor positioned at the entry that matches the given id.

Parameters:
rowId - the row id
Returns:
the cursor

fetchAll

public ArrayList<T> fetchAll(String where)
Fetches all the object in the database that match a given where clause.

Parameters:
where - the where clause; null means it will return all rows
Returns:
the array list

fetchCursor

public Cursor fetchCursor(String whereClause)
Return the cursor positioned at the first entry of the results of the query with the provided whereClause. The cursor can be used to cycle through all the query results.

Parameters:
whereClause - the where clause
Returns:
the cursor

countEntries

public int countEntries(String whereClause)
Count the entries in the database that match a given whereClause.

Parameters:
whereClause - the where clause
Returns:
the count

getReferenceId

public long getReferenceId(Cursor cursor,
                           String fieldName)
Gets the id for a referenced entry. The fieldName must be given like the object's field name (without the prepender).

Parameters:
cursor - the cursor
fieldName - the field name, without prepender
Returns:
the reference id

getDatabaseReference

public SQLiteDatabase getDatabaseReference()
Gets the database reference that can be used to manually do queries.

Returns:
the database reference

buildObject

public T buildObject(Cursor cursor)
              throws IllegalAccessException,
                     InstantiationException,
                     IllegalArgumentException,
                     ParseException,
                     SecurityException,
                     NoSuchMethodException,
                     InvocationTargetException,
                     IllegalClassStructureException
Builds the object based on a given cursor. Does not fill the reference fields, which remain null. Please use getReferenceId to get the ids for the referenced entries.

Parameters:
cursor - the cursor
Returns:
the object
Throws:
IllegalAccessException - the illegal access exception
InstantiationException - the instantiation exception
IllegalArgumentException - the illegal argument exception
ParseException - the parse exception
SecurityException - the security exception
NoSuchMethodException - the no such method exception
InvocationTargetException - the invocation target exception
IllegalClassStructureException - the illegal class structure exception