| | | | SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
|
|
com.newstep.solid.persistence.db
public abstract class DbKeeper < T >
Implements a generic database object. Uses reflection and subclassed parameter
arrays to derive the appropriate SQL for create, add, get, update, and deletion
Abstract.
Links:
Kernel Association Association Link
Associates type:
association
Client cardinality:
1
Client navigability:
NOT NAVIGABLE
Client visibility:
private
Supplier cardinality:
1
Supplier navigability:
NAVIGABLE
Supplier visibility:
protected
| Template Parameters |
| Class20 |
T |
| Nested Class Summary |
public class |
QueryConjunctive |
Final class |
|
| Field Summary |
HashMap |
|
HashMap |
|
HashMap |
|
Method |
|
HashMap |
|
Method |
|
public final static int |
|
private HashMap |
|
private HashMap |
|
static String |
|
public final static int |
|
private HashMap |
|
private HashMap |
|
private HashMap |
|
|
|
private Method |
|
private static Logger |
|
QueryConjunctive |
AND |
QueryConjunctive |
OR |
private Method |
|
public final static int |
|
| Constructor Summary |
public void |
|
| Method Summary |
protected boolean |
|
|
|
|
|
protected Object |
|
protected StringBuffer |
|
private String |
|
private String |
|
private String |
|
private String |
buildSelect(String selectCols, String whereCols, Collection likeCols, Collection nullCols, String sortAttrs, QueryConjunctive conjunctive) |
private String |
buildSelect(String selectCols, String whereCols, Collection likeCols, Collection nullCols, String sortAttrs) |
private String |
buildSelect(String selectCols, String whereCols, String sortAttrs) |
private String |
|
private String |
|
private String |
buildSelectForUpdate(String selectCols, String whereCols, String sortAttrs, QueryConjunctive conjunctive, QueryLock lock) |
private String |
|
private String |
|
protected String |
|
protected void |
|
protected void |
|
protected void |
|
protected HashMap |
|
private int |
|
public HashMap |
|
public HashMap |
|
public HashMap |
|
public HashMap |
|
protected Object |
|
public Method |
|
protected int |
|
private Collection |
|
private Collection |
|
|
|
|
|
|
|
protected int |
|
protected Collection |
|
protected Collection |
|
protected Collection |
|
protected Collection |
|
protected Collection |
|
protected Collection |
|
protected Collection |
|
protected int |
|
protected int |
|
protected abstract String |
|
protected abstract String |
|
protected abstract String |
|
public Method |
|
protected abstract String |
|
protected abstract String |
|
protected void |
|
private Method |
|
private Method |
|
protected Collection |
|
private void |
prepareSQLField(Integer index, Object obj, String type, PreparedStatement pstmt) |
private PreparedStatement |
|
protected PreparedStatement |
|
private String |
|
protected Collection |
|
private void |
|
protected void |
|
|
unpack(ResultSet rs, String unpackCols) |
protected boolean |
|
protected void |
|
protected void |
|
| Template Parameter Detail |
| Template Parameter Summary |
Parameter Metaclass:
Class20
Final.
Custom properties
templateParameterString:
T
public final static int ATTR
Final.
Multiplicity:
1
Static.
private HashMap attrColMap
Multiplicity:
1
private HashMap attrIndexMap
Multiplicity:
1
HashMap attrIndexMap
static String className
Multiplicity:
1
Static.
public final static int COL
Final.
Multiplicity:
1
Static.
HashMap colAttrMap
private HashMap colAttrMap
Multiplicity:
1
HashMap colIndexMap
private HashMap colIndexMap
Multiplicity:
1
HashMap colSetterMap
private HashMap colSetterMap
Multiplicity:
1
Multiplicity:
1
Method getters
private Method getters
Multiplicity:
1
private static Logger logger
Multiplicity:
1
Static.
Method setters
private Method setters
Multiplicity:
1
public final static int TYPE
Final.
Multiplicity:
1
Static.
public void DbKeeper ()
Stereotype:
create
protected boolean addBatch (Collection objCol, String insertCols, PersistenceContext context)
Batch object creation.
TODO: check that batch processing is supported, and throw if not.
Parameters:
objCol
insertCols
context
Throws:
PersistenceException
Add all fields of the specified object to the database
Parameters:
obj the object to persist
context the persistence context in which to perform the add
Exceptions:
PersistenceException if an error occurs persisting the object
Return:
the object with primary key and default values
Throws:
PersistenceException
Adds the specified object fields to the database
Parameters:
obj the object instance to add
insertCols array of column names specifying which cols to include in insert statement
context the PersistenceContext for the add action
Exceptions:
PersistenceException if an error occurs inserting the object record
Return:
the added object (with defaulted values and valid primary key)
Throws:
PersistenceException
Adds the specified object fields to the database without generating a unique ID from a sequence.
Parameters:
obj the object instance to add
insertCols array of column names specifying which cols to include in insert statement
context the PersistenceContext for the add action
Exceptions:
PersistenceException if an error occurs inserting the object record
Return:
the added object (with defaulted values and valid primary key)
Throws:
PersistenceException
protected StringBuffer buildCountQuery (Map searchCriteria)
Builds an sql query to perform a row count based on the specified search criteria.
Parameters:
searchCriteria column name-value pairs to be used as filtering criteria in the query
Custom properties
return A:
StringBuffer containing the sql query for the count.
private String buildDelete (String whereCols)
Builds an appropriate Delete SQL statment for the object based on the
specified columns
DELETE FROM table WHERE whereCol[0] = ? AND ... AND whereCol[N]
Parameters:
whereCols column names of the columns to use for deletion criteria
Exceptions:
PersistenceException if an error occurs building the delete statement
Return:
the SQL statement
Throws:
PersistenceException
private String buildInsert (String insertCols)
Constructs an appropriate insert statement for the object based on the
specified columns. The created statement contains only specified columns.
If the array of columns is null it creates an insert statement for ALL
the object fields
INSERT INTO table (insertCols[0], ..., insertCols[N]) VALUES(?,...?)
Parameters:
insertCols an array of indexes of the columns to be include in the
insert statement. If null than ALL attributes are inserted.
This mechanism is used to enable
attribute value defaulting via the database
Exceptions:
PersistenceException Description of the Exception
Return:
a string containing the appropriate insert statement
Throws:
PersistenceException
private String buildSelect (String selectCols, String whereCols)
Builds an appropritate select statment based on the specified
select and where columns. If the select columns is null the
builder defaults to ALL while the where defaults to the primary
key column. e.g.
SELECT selectCols[0], .. , selectCols[N] FROM table WHERE whereCols[0] = ? AND .... AND whereCols[N] = ?
Parameters:
selectCols array of column names to be selected from database
whereCols array of columns to use in WHERE clause
Exceptions:
PersistenceException Description of the Exception
Return:
Appropriate SQL statement
Throws:
PersistenceException
private String buildSelect (String selectCols, String whereCols, Collection likeCols, Collection nullCols, String sortAttrs, QueryConjunctive conjunctive)
Throws:
PersistenceException
private String buildSelect (String selectCols, String whereCols, Collection likeCols, Collection nullCols, String sortAttrs)
Throws:
PersistenceException
private String buildSelect (String selectCols, String whereCols, String sortAttrs)
Builds an appropritate select statment based on the specified
select, where and sort columns. If the select columns is null the
builder defaults to ALL while the where defaults to the primary
key column. e.g.
SELECT selectCols[0], .. , selectCols[N] FROM table WHERE whereCols[0] = ? AND .... AND whereCols[N] = ? ORDER BY sortAttrs{0] ... sortAttrs[N]
Parameters:
selectCols array of column names to be selected from database
whereCols array of columns to use in WHERE clause
sortAttrs array of attribute to use in ORDER BY clause
Exceptions:
PersistenceException Description of the Exception
Return:
Appropriate SQL statement
Throws:
PersistenceException
private String buildSelectCount (String whereCols, Collection likeCols, Collection nullCols)
Throws:
PersistenceException
private String buildSelectCount (String whereCols)
Builds an appropritate select statment based on the specified
where column. e.g.
SELECT count(*) FROM table WHERE whereCols[0] = ? AND .... AND whereCols[N] = ?
Parameters:
whereCols array of columns to use in WHERE clause
Exceptions:
PersistenceException Description of the Exception
Return:
Appropriate SQL statement
Throws:
PersistenceException
private String buildSelectForUpdate (String selectCols, String whereCols, String sortAttrs, QueryConjunctive conjunctive, QueryLock lock)
Throws:
PersistenceException
private String buildUpdate (String updateCols, String whereCols)
Builds the appropriate UPDATE SQL statement based on the updates
columns array and where columns array.
UPDATE table SET updateCols[0] = ?, ..., updateCols[N] = ? WHERE whereCols[0] = ? AND ... AND whereCols[N] = ?
Parameters:
updateCols Description of the Parameter
whereCols Description of the Parameter
Exceptions:
PersistenceException Description of the Exception
Return:
Description of the Return Value
Throws:
PersistenceException
private String capitalize (String str)
Capitalizes the first letter of the specified String
Parameters:
str the String to capitalize
Return:
String with first letter capitalized
protected String convertBeanAttrToCol (String attrNames)
Deletes the specified object record
Parameters:
context the PersistenceContext for the delete
obj Description of the Parameter
whereCols Description of the Parameter
Exceptions:
PersistenceException if an error occurs deleting the record
Throws:
PersistenceException
Deletes objects using the specified key column
Parameters:
id the value of the deletion key
colName the name of the deletion key column
context the PersistenceContext in which to delete
Exceptions:
PersistenceException if an error occurs deleting the object
Throws:
PersistenceException
Delete the specified object record
Parameters:
id the unique id of the object to delete
context the PersistenceContext in which to perform the delete
Exceptions:
PersistenceException if an error occurs deleting the object
Throws:
PersistenceException
protected HashMap getAttrColMap ()
private int getAttrIndex (String attrName)
Retrieve the index of the specified atttribure information into the multi dimensional Schema array
Parameters:
attrName the name of the attribute who's index is requested
Exceptions:
PersistenceException if the specified attribute does not exist
Return:
the index of the attribute in the schema array
Throws:
PersistenceException
public HashMap getAttrIndexMap ()
public HashMap getColAttrMap ()
public HashMap getColIndexMap ()
public HashMap getColSetterMap ()
Extracts default values for all columns in the table that have defaults defined.
Parameters:
context the PersistenceContext on which the query is performed
Exceptions:
PersistenceException if an error occurs during retrieval from database
Return:
an empty object with default values
Throws:
PersistenceException
public Method getGetters ()
protected int getIndex (String colName)
Retrieve the index of the specified column information into the multi dimensional Schema array
Parameters:
colName the name of the column who's index is requested
Exceptions:
PersistenceException if the specified column does not exist
Return:
the index of the column in the schema array
Throws:
PersistenceException
private Collection getLikeCols (Object obj, String whereCols)
Search for "%" in the values of a given array of columns. This result is used
to build a select statement using "LIKE" rather than " = " as its condition.
Parameters:
obj an object instance containing key column values
whereCols an array of key column names
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
an array of the indices of any column whose value contains "%", null if "%" not found in any column
Throws:
PersistenceException
private Collection getNullCols (Object obj, String whereCols)
Search for null in the values of a given array of columns. This result is used
to build a select statement using "IS NULL" rather than " = " as its condition.
Parameters:
obj an object instance containing key column values
whereCols an array of key column names
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
an array of the indices of any column whose value is null, null if null value is not found in any column
Throws:
PersistenceException
Retrieves the specified object's columns based on the specified column values
Parameters:
obj the object containing the key column values
selectCols an array of the column names to populate
whereCols an array of the key column names
context the PersistenceContext associated with the get action
Exceptions:
PersistenceException if an error occurs retrieving the object
Return:
the specified object
Throws:
PersistenceException
Retrieves the specified obj from the database. Loads all attribute
values based on the specified key column
Parameters:
id the unique id of the object to load
colName the column name of the id field
context the PersistenceContext in which to get the object
Exceptions:
PersistenceException if an error occurs loading the specified object
Return:
the specified object
Throws:
PersistenceException
Gets the obj attribute of the DbKeeper object
Parameters:
id Description of the Parameter
context Description of the Parameter
Exceptions:
PersistenceException Description of the Exception
Return:
The obj value
Throws:
PersistenceException
Returns a count of the total number of rows in a table matching the specified search criteria.
Parameters:
searchCriteria column name-value pairs to be used as filtering criteria in the query
context the PersistenceContext for the select query
Return.
Throws:
PersistenceException
protected Collection getObjList (Object obj, String selectCols, String whereCols, PersistenceContext context)
Retrieves a list of objects by using the specified select column list
and where column key list
Parameters:
obj an object instance containing key column values, % is wild card and null matches NULL in column
selectCols an array of column names to select
whereCols an array of key column names
context the PersistenceContext for the select
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
a Collection of objects associated with the specified mnss
Throws:
PersistenceException
protected Collection getObjList (Object obj, String selectCols, String whereCols, String sortAttrs, Integer maxRows, PersistenceContext context)
Retrieves a list of objects by using the specified select column list
and where column key list. The query result is sorted using the sort column list.
The sorting criteria is ordered by the order of the sort column list.
Parameters:
obj an object instance containing key column values, % is wild card and null matches NULL in column
selectCols an array of column names to select
whereCols an array of key column names
sortAttrs an array of attribute name for sorting results
context the PersistenceContext for the select
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
a Collection of objects associated with the specified mnss
Throws:
PersistenceException
Retrieves a list of objects with the specified key
Parameters:
id the identifier key of the objects to get
colName the name of the column to use as the lookup key
context the PersistenceContext in which to retrieve the list
Exceptions:
PersistenceException if an error occurs retrieving the object list
Return:
the list of corresponding objects
Throws:
PersistenceException
protected Collection getObjList (Integer id, String colName, String sortAttrs, Integer maxRows, PersistenceContext context)
Retrieves a list of objects with the specified key
Parameters:
id the identifier key of the objects to get
colName the name of the column to use as the lookup key
sortAttrs an array of attribute name for sorting results
context the PersistenceContext in which to retrieve the list
Exceptions:
PersistenceException if an error occurs retrieving the object list
Return:
the list of corresponding objects
Throws:
PersistenceException
Retrieves a list of objects with the specified key
Parameters:
id the identifier key of the objects to get
context the PersistenceContext in which to retrieve the list
Exceptions:
PersistenceException if an error occurs retrieving the object list
Return:
the list of corresponding objects
Throws:
PersistenceException
protected Collection getObjListByPage (Object obj, String whereCols, String selectCols, String sortAttrs, PagedQuery page, PersistenceContext context)
Retrieves a list of objects using paging.
The goal is to retrieve a big list of objects in chunks, the size of the chunk (page) determines
how many records are fetched from the database. In this manner, a query will never load more objects
in memory than the page size.
Each entry in the result list contains the attributes specified by the "select" columns; the
filtering criteria for data retrieval is specified by the "where" columns.
The query result is sorted using the specified "sort" attributes.
The query searches with a default AND(ed) conjunctive.
Parameters:
obj a temporary object holding the data to be used as filtering criteria
whereCols an array of column names to use as filtering criteria
selectCols an array of column names to select, null for select all
sortAttrs an array of attribute names for sorting results
pageSize an int defining the number of rows in a page (records to be fetched)
pageIndex an int defining which page (group of records) to retrieve
context the PersistenceContext for the select
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
a Collection of objects associated with the specified page index or
an empty Collection if the page index exceeds the number of pages in the set
Throws:
PersistenceException
protected Collection getObjListByPage (Object obj, String whereCols, String selectCols, String sortAttrs, Integer pageIndex, Integer pageSize, PersistenceContext context)
Retrieves a list of objects using paging.
The goal is to retrieve a big list of objects in chunks, the size of the chunk (page) determines
how many records are fetched from the database. In this manner, a query will never load more objects
in memory than the page size.
Each entry in the result list contains the attributes specified by the "select" columns; the
filtering criteria for data retrieval is specified by the "where" columns.
The query result is sorted using the specified "sort" attributes.
Parameters:
obj a temporary object holding the data to be used as filtering criteria
whereCols an array of column names to use as filtering criteria
selectCols an array of column names to select, null for select all
sortAttrs an array of attribute names for sorting results
pageSize an int defining the number of rows in a page (records to be fetched)
pageIndex an int defining which page (group of records) to retrieve
context the PersistenceContext for the select
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
a Collection of objects associated with the specified page index or
an empty Collection if the page index exceeds the number of pages in the set
Throws:
PersistenceException
Gets the count of objects with the specified key
Parameters:
id the identifier key of the objects to get
colName the name of the column to use as the lookup key
context the PersistenceContext in which to retrieve the list
Exceptions:
PersistenceException if an error occurs retrieving the object list
Return:
the list of corresponding objects
Throws:
PersistenceException
Gets the total number of objects by using the specified select column list
and where column key list.
Parameters:
obj an object instance containing key column values, % is wild card and null matches NULL in column
whereCols an array of key column names
context the PersistenceContext for the select
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
a Collection of objects associated with the specified mnss
Throws:
PersistenceException
protected abstract String getPrimaryKeyCol ()
Gets the index of the primary key column attribute in the cols array
Return:
the index of the primary key column attribute in the cols array
Abstract.
protected abstract String getSchema ()
Gets an array of schema mappings containing the cols, attrs, types
Return:
an array of column names reflecting the db schema
Abstract.
protected abstract String getSequence ()
Gets the name of the sequence associated with the object
Return:
the name of the sequence associated with the object
Abstract.
public Method getSetters ()
protected abstract String getTable ()
Gets the name of the table associatd with the value object
Return:
the name of the table associatd with the value object
Abstract.
protected abstract String getVOClassName ()
Gets the class name of the value object
Return:
The value object class name
Abstract.
protected void init ()
Initializes the set/get arrays based on the object associated
with the return of getVOClassName() and the array of attributes.
Exceptions:
PersistenceException Description of the Exception
Throws:
PersistenceException
private Method initGetters ()
Initializes an array of member access methods (get) for the
value object based on the attrs array and the types array
Exceptions:
PersistenceException Description of the Exception
Return:
an array of get methods for the objects attributes
Throws:
PersistenceException
private Method initSetters ()
Initializes an array of member access methods (set) for the
value object based on the attrs array and the types array
Exceptions:
PersistenceException Description of the Exception
Return:
an array of set methods for the objects attributes
Throws:
PersistenceException
protected Collection page (PagedQuery paging, ResultSet rs, String selectCols)
Throws:
PersistenceException,SQLException
private void prepareSQLField (Integer index, Object obj, String type, PreparedStatement pstmt)
Format and substitute value into the prepared statement based on the specified type
Parameters:
index the index into the prepared statement
obj the object value to format and substitute
type the name of the type to use for object formatting
pstmt the prepared statement to substitute the value into
Exceptions:
SQLException if an error occurs substituting a value into the statement
Throws:
PersistenceException,SQLException
prepareSQLSelectStatement
private PreparedStatement prepareSQLSelectStatement (Object obj, Integer offset, String prepCols, PreparedStatement pstmt)
Prepares a select statement by substituting the attribute values from
the obj object into the PreparedStatement created by the build f(n).
Insert, delete and update statements should use prepareSQLStatement().
This method is different from prepareSQLStatement() in that it does not
call prepareSQLField() if a where column value is null. This method must
be used in combination with and after buildSelect() becuase buildSelect()
treats null where column value in a way consistent with this method.
If the array of prep columns is null the function will attemt to substitute
ALL the attribute values in order.
e.g. SELECT COL1, ..., COLN FROM table WHERE PK = ? --> becomes:
SELECT COL1, ..., COLN FROM table WHERE PK = prepCols[0]
Parameters:
obj the object who's attribute values are used to prepare the statement
offset the offset from which the substitution should start
prepCols the columns that should be prepared by the function
pstmt the statement object
Exceptions:
PersistenceException if an error occurs preparing the specified statement
Return:
the prepared PreparedStatement object
Throws:
PersistenceException
protected PreparedStatement prepareSQLStatement (Object obj, Integer offset, String prepCols, PreparedStatement pstmt)
Prepares an SQL statement by substituting the attribute values from
the obj object into the PreparedStatement created by the build f(n).
Select statements should use prepareSQLSelectStatement().
This method is different from prepareSQLSelect Statement() in that it
calls prepareSQLField() regardless of the value of any where column value.
This method must not be used in combination with and after buildSelect()
becuase buildSelect() treats null where column value in a way inconsistent
with this method.
If the array of prep columns is null the function will attemt to substitute
ALL the attribute values in order.
e.g. SELECT COL1, ..., COLN FROM table WHERE PK = ? --> becomes:
SELECT COL1, ..., COLN FROM table WHERE PK = prepCols[0]
Parameters:
obj the object who's attribute values are used to prepare the statement
offset the offset from which the substitution should start
prepCols the columns that should be prepared by the function
pstmt the statement object
Exceptions:
PersistenceException if an error occurs preparing the specified statement
Return:
the prepared PreparedStatement object
Throws:
PersistenceException
private String prepareWhereCols (Object obj, String whereCols)
Removes any entry in the where columns whose value is the String "%"
because the search criterion from such column "LIKE %" does not specify
anything but slows down query.
Throws:
PersistenceException
protected Collection query (Object obj, String whereCols, String selectCols, String sortAttrs, PagedQuery page, QueryConjunctive conjunctive, PersistenceContext context)
See previous method description
Parameters:
obj a temporary object holding the data to be used as filtering criteria
whereCols an array of column names to use as filtering criteria
selectCols an array of column names to select, null for select all
sortAttrs an array of attribute names for sorting results
pageSize an int defining the number of rows in a page (records to be fetched)
pageIndex an int defining which page (group of records) to retrieve
conjunctive AND OR conjunctive connecting criteria
context the PersistenceContext for the select
Exceptions:
PersistenceException if an error occurs retrieving the list of objects
Return:
a Collection of objects associated with the specified page index or
an empty Collection if the page index exceeds the number of pages in the set
Throws:
PersistenceException
private void rollbackUid (Object obj, Integer oldId)
Throws:
PersistenceException
protected void setUniqueId (Object obj, Integer pk)
Throws:
PersistenceException
protected T unpack (ResultSet rs, String unpackCols)
Un-marshals the object attribute values from the result set and populates
a new object instance based on the unpackCols array
Parameters:
rs the result set to unpack
unpackCols the columns to unpack from the results
Exceptions:
PersistenceException if an error occurs upacking the results
Return:
a value object populated with the result set
Throws:
PersistenceException
protected boolean updateBatch (Collection objCol, String updateCols, String whereCols, PersistenceContext context)
TODO: check that batch processing is supported, and throw if not.
Parameters:
objCol
updateCols
whereCols
context
Return.
Throws:
PersistenceException
Updates the specified object values
Parameters:
obj the object instance to update
context the PersistenceContext in which to update the object
Exceptions:
PersistenceContext if an error occurs persisting the updates
Throws:
PersistenceException
protected void updateObj (Object obj, String updateCols, String whereCols, PersistenceContext context)
Update the object record's specified attributes based on the supplied
update column names and where-clause column names
Parameters:
obj the object from which to update
updateCols an array of the attribute columns to update
whereCols an array of key column names
context the PersistenceContext for the action
Exceptions:
PersistenceException if an error occurs retrieving the object
Throws:
PersistenceException
| | | | SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
|
|