|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Dao<T,ID>
The definition of the Database Access Objects that handle the reading and writing a class from the database. Kudos to Robert A. for the general concept of this hierarchy.
Method Summary | ||
---|---|---|
|
callBatchTasks(Callable<CT> callable)
Call the call-able that will perform a number of batch tasks. |
|
int |
create(T data)
Create a new row in the database from an object. |
|
int |
delete(Collection<T> datas)
Delete a collection of objects from the database using an IN SQL clause. |
|
int |
delete(PreparedDelete<T> preparedDelete)
Delete the objects that match the prepared statement argument. |
|
int |
delete(T data)
Delete an object from the database. |
|
DeleteBuilder<T,ID> |
deleteBuilder()
Like queryBuilder() but allows you to build an DELETE statement. |
|
int |
deleteIds(Collection<ID> ids)
Delete the objects that match the collection of ids from the database using an IN SQL clause. |
|
CloseableIterator<T> |
iterator()
This satisfies the Iterable interface for the class and allows you to iterate through the objects in the
table using SQL. |
|
CloseableIterator<T> |
iterator(PreparedQuery<T> preparedQuery)
Same as iterator() but with a prepared query parameter. |
|
RawResults |
iteratorRaw(String query)
Same as iterator(PreparedQuery) except it returns a RawResults object associated with the SQL select
query argument. |
|
boolean |
objectsEqual(T data1,
T data2)
Return true if the two arguments are equal. |
|
String |
objectToString(T data)
Return the string version of the object with each of the known field values shown. |
|
List<T> |
query(PreparedQuery<T> preparedQuery)
Query for the items in the object table which match the prepared query. |
|
QueryBuilder<T,ID> |
queryBuilder()
Create and return a new query builder object which allows you to build a custom SELECT statement. |
|
List<T> |
queryForAll()
Query for all of the items in the object table. |
|
RawResults |
queryForAllRaw(String query)
Query for all of the items in the object table that match the SQL select query argument. |
|
T |
queryForFirst(PreparedQuery<T> preparedQuery)
Query for and return the first item in the object table which matches the PreparedQuery. |
|
T |
queryForId(ID id)
Retrieves an object associated with a specific ID. |
|
int |
refresh(T data)
Does a query for the object's id and copies in each of the field values from the database to refresh the data parameter. |
|
int |
update(PreparedUpdate<T> preparedUpdate)
Update all rows in the table according to the prepared statement argument. |
|
int |
update(T data)
Save the fields from an object to the database. |
|
UpdateBuilder<T,ID> |
updateBuilder()
Like queryBuilder() but allows you to build an UPDATE statement. |
|
int |
updateId(T data,
ID newId)
Update an object in the database to change its id to the newId parameter. |
Method Detail |
---|
T queryForId(ID id) throws SQLException
id
- Identifier that matches a specific row in the database to find and return.
SQLException
- on any SQL problems or if more than 1 item with the id are found in the database.T queryForFirst(PreparedQuery<T> preparedQuery) throws SQLException
queryBuilder()
for more information. This can be used to return the object that matches a single unique
column. You should use queryForId(ID)
if you want to query for the id column.
preparedQuery
- Query used to match the objects in the database.
SQLException
- on any SQL problems.List<T> queryForAll() throws SQLException
iterator()
method instead.
SQLException
- on any SQL problems.RawResults queryForAllRaw(String query) throws SQLException
StatementBuilder
for most queries, this method allows you to do special queries that aren't supported
otherwise. For medium sized or large tables, this may load a lot of objects into memory so you should consider
using the iteratorRaw(String)
method instead.
SQLException
- on any SQL problems.QueryBuilder<T,ID> queryBuilder()
QueryBuilder.prepare()
once you are
ready to build. This returns a PreparedQuery
object which gets passed to query(PreparedQuery)
or
iterator(PreparedQuery)
.
UpdateBuilder<T,ID> updateBuilder()
queryBuilder()
but allows you to build an UPDATE statement. You can then call call
UpdateBuilder.prepare()
and pass the returned PreparedUpdate
to update(PreparedUpdate)
.
DeleteBuilder<T,ID> deleteBuilder()
queryBuilder()
but allows you to build an DELETE statement. You can then call call
DeleteBuilder.prepare()
and pass the returned PreparedDelete
to delete(PreparedDelete)
.
List<T> query(PreparedQuery<T> preparedQuery) throws SQLException
queryBuilder()
for more
information.
NOTE: For medium sized or large tables, this may load a lot of objects into memory so you should consider
using the iterator(PreparedQuery)
method instead.
preparedQuery
- Query used to match the objects in the database.
SQLException
- on any SQL problems.int create(T data) throws SQLException
data
- The data item that we are creating in the database.
SQLException
int update(T data) throws SQLException
updateId(T, ID)
.
data
- The data item that we are updating in the database.
SQLException
- on any SQL problems.
IllegalArgumentException
- If there is only an ID field in the object. See the updateId(T, ID)
method.int updateId(T data, ID newId) throws SQLException
NOTE: Depending on the database type and the id type, you may be unable to change the id of the field.
data
- The data item that we are updating in the database with the current id.newId
- The new id that you want to update the data with.
SQLException
- on any SQL problems.int update(PreparedUpdate<T> preparedUpdate) throws SQLException
UpdateBuilder
must have set-columns applied to it using the UpdateBuilder.updateColumnValue(String, Object)
or
UpdateBuilder.updateColumnExpression(String, String)
methods.
preparedUpdate
- A prepared statement to match database rows to be deleted and define the columns to update.
SQLException
- on any SQL problems.
IllegalArgumentException
- If there is only an ID field in the object. See the updateId(T, ID)
method.int refresh(T data) throws SQLException
data
- The data item that we are refreshing with fields from the database.
SQLException
- on any SQL problems or if the data item is not found in the table or if more than 1 item is found
with data's id.int delete(T data) throws SQLException
data
- The data item that we are deleting from the database.
SQLException
- on any SQL problems.int delete(Collection<T> datas) throws SQLException
datas
- A collection of data items to be deleted.
SQLException
- on any SQL problems.int deleteIds(Collection<ID> ids) throws SQLException
ids
- A collection of data ids to be deleted.
SQLException
- on any SQL problems.int delete(PreparedDelete<T> preparedDelete) throws SQLException
preparedDelete
- A prepared statement to match database rows to be deleted.
SQLException
- on any SQL problems.CloseableIterator<T> iterator()
Iterable
interface for the class and allows you to iterate through the objects in the
table using SQL. You can use code similar to the following:
for (Account account : accountDao) { ... }
WARNING: because the Iterator.hasNext()
, Iterator.next()
, etc. methods can only throw
RuntimeException
, the code has to wrap any SQLException
with IllegalStateException
. Make
sure to catch IllegalStateException
and look for a SQLException
cause. See the
SelectIterator
source code for more details.
WARNING: The underlying results object will only be closed if you page all the way to the end of the
iterator using the for() loop or if you call SelectIterator.close()
directly. It is also closed when it
is garbage collected but this is considered bad form.
iterator
in interface CloseableIterable<T>
iterator
in interface Iterable<T>
IllegalStateException
- When it encounters a SQLException or in other cases.CloseableIterator<T> iterator(PreparedQuery<T> preparedQuery) throws SQLException
iterator()
but with a prepared query parameter. See queryBuilder()
for more information.
You use it like the following:
QueryBuilder<Account, String> qb = accountDao.queryBuilder(); ... custom query builder methods SelectIterator<Account> iterator = partialDao.iterator(qb.prepare()); try { while (iterator.hasNext()) { Account account = iterator.next(); ... } } finish { iterator.close(); }
preparedQuery
- Query used to iterate across a sub-set of the items in the database.
SQLException
- on any SQL problems.RawResults iteratorRaw(String query) throws SQLException
iterator(PreparedQuery)
except it returns a RawResults object associated with the SQL select
query argument. Although you should use the iterator()
for most queries, this method allows you to do
special queries that aren't supported otherwise. Like the above iterator methods, you must call close on the
returned RawResults object once you are done with it.
SQLException
<CT> CT callBatchTasks(Callable<CT> callable) throws Exception
NOTE: This is only supported by databases that support auto-commit. Android, for example, does not support
auto-commit although using the TransactionManager
and performing actions within a transaction seems to
have the same batch performance implications.
Exception
String objectToString(T data)
data
- The data item for which we are returning the toString information.boolean objectsEqual(T data1, T data2) throws SQLException
data1
- One of the data items that we are checking for equality.data2
- The other data item that we are checking for equality.
SQLException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |