|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.j256.ormlite.dao.BaseDaoImpl<T,ID>
public abstract class BaseDaoImpl<T,ID>
Base class for the Database Access Objects that handle the reading and writing a class from the database.
This class is also Iterable
which means you can do a for (T obj : dao)
type of loop code to iterate
through the table of persisted objects. See iterator()
.
NOTE: If you are using the Spring type wiring, initialize()
should be called after all of the set
methods. In Spring XML, init-method="initialize" should be used.
Field Summary | |
---|---|
protected ConnectionSource |
connectionSource
|
protected DatabaseType |
databaseType
|
protected Class<T> |
dataClass
|
protected StatementExecutor<T,ID> |
statementExecutor
|
protected DatabaseTableConfig<T> |
tableConfig
|
protected TableInfo<T,ID> |
tableInfo
|
Constructor Summary | |
---|---|
protected |
BaseDaoImpl(Class<T> dataClass)
Construct our base DAO using Spring type wiring. |
protected |
BaseDaoImpl(ConnectionSource connectionSource,
Class<T> dataClass)
Construct our base DAO class. |
protected |
BaseDaoImpl(ConnectionSource connectionSource,
DatabaseTableConfig<T> tableConfig)
Construct our base DAO class. |
Method Summary | ||
---|---|---|
|
callBatchTasks(Callable<CT> callable)
Call the call-able that will perform a number of batch tasks. |
|
protected void |
checkForInitialized()
|
|
long |
countOf()
Returns the number of rows in the table associated with the data class. |
|
int |
create(T data)
Create a new row in the database from an object. |
|
static
|
createDao(ConnectionSource connectionSource,
Class<T> clazz)
Deprecated. You should be using the DaoManager.createDao(ConnectionSource, Class) |
|
static
|
createDao(ConnectionSource connectionSource,
DatabaseTableConfig<T> tableConfig)
Deprecated. You should be using the DaoManager.createDao(ConnectionSource, DatabaseTableConfig) |
|
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 Dao.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. |
|
int |
executeRaw(String statement,
String... arguments)
Run a raw execute SQL statement to the database.The arguments are optional but can be set with strings to expand ? type of SQL. |
|
ID |
extractId(T data)
Returns the ID from the data argument passed in. |
|
FieldType |
findForeignFieldType(Class<?> clazz)
Returns the class of the DAO. |
|
Class<T> |
getDataClass()
Returns the class of the DAO. |
|
DatabaseTableConfig<T> |
getTableConfig()
Returns the table configuration information associated with the Dao's class or null if none. |
|
TableInfo<T,ID> |
getTableInfo()
Used by internal classes to get the table information structure for the Dao's class. |
|
void |
initialize()
Initialize the various DAO configurations after the various setters have been called. |
|
boolean |
isTableExists()
Returns true if the table already exists otherwise false. |
|
boolean |
isUpdatable()
Returns true if we can call update on this class. |
|
SelectIterator<T,ID> |
iterator()
This satisfies the Iterable interface for the class and allows you to iterate through the objects in the
table using SQL. |
|
SelectIterator<T,ID> |
iterator(PreparedQuery<T> preparedQuery)
Same as Dao.iterator() but with a prepared query parameter. |
|
RawResults |
iteratorRaw(String query)
You should be using Dao.queryRaw(String, String...) ; |
|
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 queryString)
You should be using Dao.queryRaw(String, String...) |
|
List<T> |
queryForEq(String fieldName,
Object value)
Query for the items in the object table that match a simple where with a single field = value type of WHERE clause. |
|
List<T> |
queryForFieldValues(Map<String,Object> fieldValues)
Query for the rows in the database that matches all of the field to value entries from the map passed in. |
|
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. |
|
List<T> |
queryForMatching(T matchObj)
Query for the rows in the database that match the object passed in as an argument. |
|
GenericRawResults<Object[]> |
queryRaw(String query,
DataType[] columnTypes,
String... arguments)
Similar to the Dao.queryRaw(String, String...) but instead of an array of String results being returned by
the iterator, this uses the column-types parameter to return an array of Objects instead. |
|
|
queryRaw(String query,
RawRowMapper<GR> mapper,
String... arguments)
Similar to the Dao.queryRaw(String, String...) but this iterator returns rows that you can map yourself. |
|
GenericRawResults<String[]> |
queryRaw(String query,
String... arguments)
Similar to the Dao.iterator(PreparedQuery) except it returns a RawResults object associated with the SQL
select query argument. |
|
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. |
|
void |
setConnectionSource(ConnectionSource connectionSource)
|
|
void |
setTableConfig(DatabaseTableConfig<T> tableConfig)
Used if you want to configure the class for the Dao by hand or with spring instead of using the DatabaseField annotation in the class. |
|
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 Dao.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. |
|
int |
updateRaw(String statement,
String... arguments)
Run a raw update SQL statement to the database. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected StatementExecutor<T,ID> statementExecutor
protected DatabaseType databaseType
protected final Class<T> dataClass
protected DatabaseTableConfig<T> tableConfig
protected TableInfo<T,ID> tableInfo
protected ConnectionSource connectionSource
Constructor Detail |
---|
protected BaseDaoImpl(Class<T> dataClass) throws SQLException
ConnectionSource
must be set with the
setConnectionSource(com.j256.ormlite.support.ConnectionSource)
method afterwards and then the initialize()
method must be called. The
dataClass provided must have its fields marked with DatabaseField
annotations or the
setTableConfig(com.j256.ormlite.table.DatabaseTableConfig)
method must be called before the initialize()
method is called.
If you are using Spring then your should use: init-method="initialize"
dataClass
- Class associated with this Dao. This must match the T class parameter.
SQLException
protected BaseDaoImpl(ConnectionSource connectionSource, Class<T> dataClass) throws SQLException
DatabaseField
or
javax.persistance annotations.
connectionSource
- Source of our database connections.dataClass
- Class associated with this Dao. This must match the T class parameter.
SQLException
protected BaseDaoImpl(ConnectionSource connectionSource, DatabaseTableConfig<T> tableConfig) throws SQLException
connectionSource
- Source of our database connections.tableConfig
- Hand or Spring wired table configuration information.
SQLException
Method Detail |
---|
public void initialize() throws SQLException
SQLException
public T queryForId(ID id) throws SQLException
Dao
queryForId
in interface Dao<T,ID>
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.public T queryForFirst(PreparedQuery<T> preparedQuery) throws SQLException
Dao
Dao.queryBuilder()
for more information. This can be used to return the object that matches a single unique
column. You should use Dao.queryForId(Object)
if you want to query for the id column.
queryForFirst
in interface Dao<T,ID>
preparedQuery
- Query used to match the objects in the database.
SQLException
- on any SQL problems.public List<T> queryForAll() throws SQLException
Dao
Dao.iterator()
method instead.
queryForAll
in interface Dao<T,ID>
SQLException
- on any SQL problems.public List<T> queryForEq(String fieldName, Object value) throws SQLException
Dao
queryForEq
in interface Dao<T,ID>
SQLException
- on any SQL problems.public QueryBuilder<T,ID> queryBuilder()
Dao
QueryBuilder.prepare()
once you are
ready to build. This returns a PreparedQuery
object which gets passed to Dao.query(PreparedQuery)
or
Dao.iterator(PreparedQuery)
.
queryBuilder
in interface Dao<T,ID>
public UpdateBuilder<T,ID> updateBuilder()
Dao
Dao.queryBuilder()
but allows you to build an UPDATE statement. You can then call call
UpdateBuilder.prepare()
and pass the returned PreparedUpdate
to Dao.update(PreparedUpdate)
.
updateBuilder
in interface Dao<T,ID>
public DeleteBuilder<T,ID> deleteBuilder()
Dao
Dao.queryBuilder()
but allows you to build an DELETE statement. You can then call call
DeleteBuilder.prepare()
and pass the returned PreparedDelete
to Dao.delete(PreparedDelete)
.
deleteBuilder
in interface Dao<T,ID>
public List<T> query(PreparedQuery<T> preparedQuery) throws SQLException
Dao
Dao.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 Dao.iterator(PreparedQuery)
method instead.
query
in interface Dao<T,ID>
preparedQuery
- Query used to match the objects in the database.
SQLException
- on any SQL problems.public RawResults queryForAllRaw(String queryString) throws SQLException
Dao
Dao.queryRaw(String, String...)
queryForAllRaw
in interface Dao<T,ID>
SQLException
public List<T> queryForMatching(T matchObj) throws SQLException
Dao
queryForMatching
in interface Dao<T,ID>
SQLException
public List<T> queryForFieldValues(Map<String,Object> fieldValues) throws SQLException
Dao
queryForFieldValues
in interface Dao<T,ID>
SQLException
public int create(T data) throws SQLException
Dao
create
in interface Dao<T,ID>
data
- The data item that we are creating in the database.
SQLException
public int update(T data) throws SQLException
Dao
Dao.updateId(T, ID)
.
update
in interface Dao<T,ID>
data
- The data item that we are updating in the database.
SQLException
- on any SQL problems.public int updateId(T data, ID newId) throws SQLException
Dao
NOTE: Depending on the database type and the id type, you may be unable to change the id of the field.
updateId
in interface Dao<T,ID>
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.public int update(PreparedUpdate<T> preparedUpdate) throws SQLException
Dao
UpdateBuilder
must have set-columns applied to it using the UpdateBuilder.updateColumnValue(String, Object)
or
UpdateBuilder.updateColumnExpression(String, String)
methods.
update
in interface Dao<T,ID>
preparedUpdate
- A prepared statement to match database rows to be deleted and define the columns to update.
SQLException
- on any SQL problems.public int refresh(T data) throws SQLException
Dao
refresh
in interface Dao<T,ID>
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.public int delete(T data) throws SQLException
Dao
delete
in interface Dao<T,ID>
data
- The data item that we are deleting from the database.
SQLException
- on any SQL problems.public int delete(Collection<T> datas) throws SQLException
Dao
delete
in interface Dao<T,ID>
datas
- A collection of data items to be deleted.
SQLException
- on any SQL problems.public int deleteIds(Collection<ID> ids) throws SQLException
Dao
deleteIds
in interface Dao<T,ID>
ids
- A collection of data ids to be deleted.
SQLException
- on any SQL problems.public int delete(PreparedDelete<T> preparedDelete) throws SQLException
Dao
delete
in interface Dao<T,ID>
preparedDelete
- A prepared statement to match database rows to be deleted.
SQLException
- on any SQL problems.public SelectIterator<T,ID> iterator()
Dao
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 Dao<T,ID>
iterator
in interface Iterable<T>
public SelectIterator<T,ID> iterator(PreparedQuery<T> preparedQuery) throws SQLException
Dao
Dao.iterator()
but with a prepared query parameter. See Dao.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(); }
iterator
in interface Dao<T,ID>
preparedQuery
- Query used to iterate across a sub-set of the items in the database.
SQLException
- on any SQL problems.public RawResults iteratorRaw(String query) throws SQLException
Dao
Dao.queryRaw(String, String...)
;
iteratorRaw
in interface Dao<T,ID>
SQLException
public GenericRawResults<String[]> queryRaw(String query, String... arguments) throws SQLException
Dao
Dao.iterator(PreparedQuery)
except it returns a RawResults object associated with the SQL
select query argument. Although you should use the Dao.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. The arguments are optional but can be set with strings
to expand ? type of SQL.
queryRaw
in interface Dao<T,ID>
SQLException
public <GR> GenericRawResults<GR> queryRaw(String query, RawRowMapper<GR> mapper, String... arguments) throws SQLException
Dao
Dao.queryRaw(String, String...)
but this iterator returns rows that you can map yourself. For
every result that is returned by the database, the RawRowMapper.mapRow(String[], String[])
method is
called so you can convert the result columns into an object to be returned by the iterator. The arguments are
optional but can be set with strings to expand ? type of SQL.
queryRaw
in interface Dao<T,ID>
SQLException
public GenericRawResults<Object[]> queryRaw(String query, DataType[] columnTypes, String... arguments) throws SQLException
Dao
Dao.queryRaw(String, String...)
but instead of an array of String results being returned by
the iterator, this uses the column-types parameter to return an array of Objects instead. The arguments are
optional but can be set with strings to expand ? type of SQL.
queryRaw
in interface Dao<T,ID>
SQLException
public int executeRaw(String statement, String... arguments) throws SQLException
Dao
executeRaw
in interface Dao<T,ID>
SQLException
public int updateRaw(String statement, String... arguments) throws SQLException
Dao
updateRaw
in interface Dao<T,ID>
SQLException
public <CT> CT callBatchTasks(Callable<CT> callable) throws Exception
Dao
NOTE: If neither auto-commit nor transactions are supported by the database type then this may just call the callable.
callBatchTasks
in interface Dao<T,ID>
Exception
public String objectToString(T data)
Dao
objectToString
in interface Dao<T,ID>
data
- The data item for which we are returning the toString information.public boolean objectsEqual(T data1, T data2) throws SQLException
Dao
objectsEqual
in interface Dao<T,ID>
data1
- One of the data items that we are checking for equality.data2
- The other data item that we are checking for equality.
SQLException
public ID extractId(T data) throws SQLException
Dao
extractId
in interface Dao<T,ID>
SQLException
public Class<T> getDataClass()
Dao
getDataClass
in interface Dao<T,ID>
public FieldType findForeignFieldType(Class<?> clazz)
Dao
findForeignFieldType
in interface Dao<T,ID>
public boolean isUpdatable()
Dao
isUpdatable
in interface Dao<T,ID>
public boolean isTableExists() throws SQLException
Dao
isTableExists
in interface Dao<T,ID>
SQLException
public long countOf() throws SQLException
Dao
countOf
in interface Dao<T,ID>
SQLException
public DatabaseTableConfig<T> getTableConfig()
public TableInfo<T,ID> getTableInfo()
public void setConnectionSource(ConnectionSource connectionSource)
public void setTableConfig(DatabaseTableConfig<T> tableConfig)
DatabaseField
annotation in the class. This must be called before initialize()
.
@Deprecated public static <T,ID> Dao<T,ID> createDao(ConnectionSource connectionSource, Class<T> clazz) throws SQLException
DaoManager.createDao(ConnectionSource, Class)
NOTE: You should use DaoManager.createDao(ConnectionSource, DatabaseTableConfig)
instead of this
method so you won't have to create the DAO multiple times.
SQLException
@Deprecated public static <T,ID> Dao<T,ID> createDao(ConnectionSource connectionSource, DatabaseTableConfig<T> tableConfig) throws SQLException
DaoManager.createDao(ConnectionSource, DatabaseTableConfig)
TableInfo
.
NOTE: You should use DaoManager.createDao(ConnectionSource, DatabaseTableConfig)
instead of this
method so you won't have to create the DAO multiple times.
SQLException
protected void checkForInitialized()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |