org.pojava.persistence.util
Class DaoTool

java.lang.Object
  extended by org.pojava.persistence.util.DaoTool

public class DaoTool
extends Object

DaoTool provides a consistent assortment of database operations for your DAO objects to provide. It takes much of the grunt-work out of the typical CRUD operations often copy-pasted and altered from one DAO to another.

Author:
John Pile

Constructor Summary
DaoTool()
           
 
Method Summary
static
<T> int[]
batchInsert(Connection conn, TableMap<T> map, List<T> list)
          Perform a batch insert, throwing an exception if the attempted insert fails.
static
<T> int
countByQuery(Connection conn, TableMap<T> map, SqlQuery query)
          Return a count of rows matching your query.
static
<T> int
delete(Connection conn, TableMap<T> map, T obj)
          Delete an existing record from a table according to its primary key.
static
<T> int
deleteByQuery(Connection conn, TableMap<T> map, SqlQuery query)
          Execute a delete query, returning number of rows.
static
<T> T
find(Connection conn, TableMap<T> map, T obj)
          Retrieve a single record according to its primary key.
static
<T> int
insert(Connection conn, TableMap<T> map, T obj)
          Perform a single insert, throwing an exception if the attempted insert fails.
static
<T> List<T>
listByQuery(Connection conn, TableMap<T> map, SqlQuery query)
          Return a packaged list of objects matching the query.
static
<T> int
passiveInsert(Connection conn, TableMap<T> map, T obj)
          Insert a new record into a table, but only if that record is not already present according to its primary key.
static
<T> int
processByQuery(Connection conn, TableMap<T> map, SqlQuery query, org.pojava.lang.Processor<T> objProcessor)
          Process a list of objects matching the query using the user-defined class extending Processor.
static
<T> int
update(Connection conn, TableMap<T> map, T obj)
          Update an existing record in a table.
static
<T> int
updateInsert(Connection conn, TableMap<T> map, T obj)
          Update an existing object in the table according to the primary key, and insert a new record if one does not already exist.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DaoTool

public DaoTool()
Method Detail

insert

public static final <T> int insert(Connection conn,
                                   TableMap<T> map,
                                   T obj)
Perform a single insert, throwing an exception if the attempted insert fails. This will throw an SQLException if an existing record matches the primary key defined in the TableMap.

Parameters:
conn -
map -
obj -
Returns:
number of rows inserted (should always be 1)

batchInsert

public static final <T> int[] batchInsert(Connection conn,
                                          TableMap<T> map,
                                          List<T> list)
Perform a batch insert, throwing an exception if the attempted insert fails. Pair this with processByQuery for fast high-volume transfers.

Parameters:
conn - Open connection to a databases
map - TableMap describing bean to table mappings
list - List of objects of the class specified in map
Returns:
array of success values in same order as list

updateInsert

public static final <T> int updateInsert(Connection conn,
                                         TableMap<T> map,
                                         T obj)
Update an existing object in the table according to the primary key, and insert a new record if one does not already exist.

Parameters:
conn -
map -
obj -
Returns:
number of rows updated or inserted (should always be 1)

passiveInsert

public static final <T> int passiveInsert(Connection conn,
                                          TableMap<T> map,
                                          T obj)
Insert a new record into a table, but only if that record is not already present according to its primary key.

Parameters:
conn -
map -
obj -
Returns:
Number of records inserted (should be 1 or 0).

update

public static final <T> int update(Connection conn,
                                   TableMap<T> map,
                                   T obj)
Update an existing record in a table. The record is selected by its key fields and its non-key fields are updated. This does not throw an exception if the record does not already exist.

Parameters:
conn -
map -
obj -
Returns:
Number of rows updated.

delete

public static final <T> int delete(Connection conn,
                                   TableMap<T> map,
                                   T obj)
Delete an existing record from a table according to its primary key.

Parameters:
conn -
map -
obj -
Returns:
Number of rows deleted.

find

public static final <T> T find(Connection conn,
                               TableMap<T> map,
                               T obj)
Retrieve a single record according to its primary key.

Parameters:
conn -
map -
obj -
Returns:
the result of the query packaged into a mapped bean.

listByQuery

public static final <T> List<T> listByQuery(Connection conn,
                                            TableMap<T> map,
                                            SqlQuery query)
Return a packaged list of objects matching the query.

Parameters:
conn -
map -
query -
Returns:
a List of objects matching the query.

processByQuery

public static final <T> int processByQuery(Connection conn,
                                           TableMap<T> map,
                                           SqlQuery query,
                                           org.pojava.lang.Processor<T> objProcessor)
Process a list of objects matching the query using the user-defined class extending Processor. Each row is packaged into a bean according to the TableMap, and then individually sent to the "process" method of the provided Processor object. In this way, even very large query results can be processed with very low memory consumption within your Java application.

Parameters:
conn -
map -
query -
objProcessor -
Returns:
number of rows processed

countByQuery

public static final <T> int countByQuery(Connection conn,
                                         TableMap<T> map,
                                         SqlQuery query)
Return a count of rows matching your query.

Parameters:
conn -
query -
Returns:
intValue of first column of first row of ResultSet

deleteByQuery

public static final <T> int deleteByQuery(Connection conn,
                                          TableMap<T> map,
                                          SqlQuery query)
Execute a delete query, returning number of rows.

Parameters:
conn -
query -
Returns:
number of rows deleted


Copyright © 2011. All Rights Reserved.