|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.androwrapee.db.DefaultDAO<T>
T
- the type of the classes manipulated by the DefaultDAO.public class DefaultDAO<T>
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:
DatabaseClass
annotation.DatabaseClass
annotation.DatabaseField
or IdField
annotations.IdField
of recommended type long
(but must be numerical), which will be saved as _id_+field name in the database.ReferenceField
which will not be stored in the database as it is, but it MUST implement the
DatabaseReferenceClass
class, thus providing an 'id' which will be stored in the database
as _rid_
+field name. This field can be used for queries and for making one-to-many
relationships.
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 |
---|
public static final String ID_PREPENDER
public static final String REFERENCE_PREPENDER
public static final String BOOLEAN_TRUE_VALUE
public static final String BOOLEAN_FALSE_VALUE
Constructor Detail |
---|
public DefaultDAO(Class<T> c, DefaultDatabaseHelper dbHelper, ReflectionManager rm, String tableName)
c
- the classdbHelper
- the db helperrm
- the rmtableName
- the table nameMethod Detail |
---|
public DefaultDAO<T> open() throws SQLException
SQLException
- if the database could be neither opened or createdpublic void close()
public long insert(T newObject, boolean generateID)
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.
newObject
- the new objectgenerateID
- whether the database id is generated by the database or the field in the
object is used as an id.
public boolean update(T object, long rowId)
object
- the objectrowId
- the row id
public boolean delete(long rowId)
rowId
- id of the object to delete
public int delete(String whereClause)
whereClause
- the where clause
public T fetch(long rowId)
rowId
- id of object to retrieve
public Cursor fetchCursor(long rowId)
rowId
- the row id
public ArrayList<T> fetchAll(String where)
where
- the where clause; null means it will return all rows
public Cursor fetchCursor(String whereClause)
whereClause
- the where clause
public int countEntries(String whereClause)
whereClause
- the where clause
public long getReferenceId(Cursor cursor, String fieldName)
cursor
- the cursorfieldName
- the field name, without prepender
public SQLiteDatabase getDatabaseReference()
public T buildObject(Cursor cursor) throws IllegalAccessException, InstantiationException, IllegalArgumentException, ParseException, SecurityException, NoSuchMethodException, InvocationTargetException, IllegalClassStructureException
getReferenceId
to get the ids for the referenced entries.
cursor
- the cursor
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
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |