|
Version 3.8.0
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
Objecthirondelle.web4j.database.Db
public final class Db
Utility class for the most common database tasks.
This class allows many DAO methods to be implemented in one or two lines of simple code.
String
Integer
Boolean
Date
BigDecimal
Float
Long
Id
SafeText (the parameter value is taken from SafeText.getRawString())
For Id objects, the underlying column must be modeled as text, not a number. If
the underlying column is numeric, then the caller must convert an Id into a numeric form
using Id.asInteger() or Id.asLong().
ConvertColumn implementation to convert columns into various building block objects.
In addition, it uses an ordering convention to map ResultSet columns to Model Object constructor arguments. See the package overview for more information on this important point.
Constructing an arbitrary Compound Object can always be performed in multiple steps: first fetch the children, and then construct the parent, by passing the children to the parent's constructor.
For the simplest cases, this can be performed conveniently in a single step, using the fetchCompound and listCompound methods of this class. These methods process a ResultSet in a fundamentally different way : instead of translating a single row into a single Model Object, they can translate groups of 1..N rows into a single Model Object instead.
Here is an illustration. The target Model Object constructor has the form (for example):
public UserRole (String aUserName, ListThat is, the constructor takes a singleaRoles) { ... }
List of Model Objects at
the end of its list of arguments. Here, a List of Id objects appears at the end.
The underlying SELECT statement returns data across a 0..N relation, with data in the first N columns repeating the parent data, and with the remaining M columns containing the child data. For example:
SELECT Name, Role FROM UserRole ORDER BY Rolewhich has a ResultSet of the form :
| Name | Role |
|---|---|
| kenarnold | access-control |
| kenarnold | user-general |
| kenarnold | user-president |
| davidholmes | user-general |
That is, the repeated parent data (Name) comes first and is attached to the parent, while the child data (Role) appears only in the final columns. In addition, changes to the value in the first column must indicate that a new parent has started.
If the above requirements are satisfied, then a List<UserRole> is built using
listCompound(Class, Class, int, SqlId, Object[]), as in:
Db.listCompound(UserRole.class, Id.class, 1, ROLES_LIST_SQL);
| Method Summary | ||
|---|---|---|
static Id |
add(SqlId aSqlId,
Object... aParams)
INSERT operation which returns the database identifier of the added record. |
|
static int |
delete(SqlId aSqlId,
Id aId)
DELETE operation which takes a single Id parameter. |
|
static int |
edit(SqlId aSqlId,
Object... aParams)
INSERT, UPDATE, or DELETE operations which take parameters. |
|
static
|
fetch(Class<T> aClass,
SqlId aSqlId,
Object... aParams)
SELECT operation which returns a single Model Object. |
|
static
|
fetchCompound(Class<T> aClassParent,
Class<?> aClassChild,
int aNumTrailingColsForChildList,
SqlId aSqlId,
Object... aParams)
SELECT operation which typically returns a single item with a 0..N relation. |
|
static
|
fetchValue(Class<T> aSupportedTargetClass,
SqlId aSqlId,
Object... aParams)
SELECT operation which returns a single 'building block' value such as Integer, BigDecimal, and so on. |
|
static
|
list(Class<T> aClass,
SqlId aSqlId,
Object... aParams)
SELECT operation which returns 0..N Model Objects, one per row. |
|
static
|
listCompound(Class<T> aClassParent,
Class<?> aClassChild,
int aNumTrailingColsForChildList,
SqlId aSqlId,
Object... aParams)
SELECT operation which typically returns mutliple items item with a 0..N relation. |
|
static
|
listRange(Class<T> aClass,
SqlId aSqlId,
Integer aStartIndex,
Integer aPageSize,
Object... aParams)
SELECT operation that returns a List of Model Objects "subsetted" to a particular range of rows. |
|
static
|
listValues(Class<T> aSupportedTargetClass,
SqlId aSqlId,
Object... aParams)
SELECT operation which returns a List of 'building block' values such as Integer, BigDecimal, and so on. |
|
static
|
search(Class<T> aClass,
SqlId aSqlId,
DynamicCriteria aSearchCriteria,
Object... aParams)
SELECT operation for listing the result of a user's search with the given DynamicCriteria
and corresponding parameter values. |
|
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <T> T fetch(Class<T> aClass,
SqlId aSqlId,
Object... aParams)
throws DAOException
aClass - class of the returned Model Object.aSqlId - identifies the underlying SQL statement.aParams - parameters for the SQL statement.
DAOException
public static <T> T fetchValue(Class<T> aSupportedTargetClass,
SqlId aSqlId,
Object... aParams)
throws DAOException
aSupportedTargetClass - class supported by the configured
implementation of ConvertColumn.aSqlId - identifies the underlying SQL statement.aParams - parameters for the SQL statement.
DAOException
public static <T> List<T> list(Class<T> aClass,
SqlId aSqlId,
Object... aParams)
throws DAOException
aClass - class of the returned Model Objects.aSqlId - identifies the underlying SQL statement.aParams - parameters for the SQL statement.
List of Model Objects. The list may be empty.
DAOException
public static <T> List<T> listValues(Class<T> aSupportedTargetClass,
SqlId aSqlId,
Object... aParams)
throws DAOException
aSupportedTargetClass - class supported by the configured
implementation of ConvertColumn.aSqlId - identifies the underlying SQL statement.aParams - parameters for the SQL statement.
List of building block objects. The list may be empty.
DAOException
public static <T> List<T> listRange(Class<T> aClass,
SqlId aSqlId,
Integer aStartIndex,
Integer aPageSize,
Object... aParams)
throws DAOException
This method is intended for paging through long listings. When the underlying SELECT returns many pages of items, the records can be "subsetted" by calling this method.
See Pager.
aClass - class of the returned Model Objects.aSqlId - identifies the underlying SQL statement.aStartIndex - 1-based index indentifying the first row to be returned.aPageSize - number of records to be returned.aParams - parameters for the SQL statement.
List of Model Objects. The list may be empty.
DAOException
public static <T> List<T> search(Class<T> aClass,
SqlId aSqlId,
DynamicCriteria aSearchCriteria,
Object... aParams)
throws DAOException
DynamicCriteria
and corresponding parameter values.
This method is called only if the exact underlying criteria are not known beforehand, but are rather
determined dynamically by user selections. See DynamicCriteria for more information.
aClass - class of the returned Model Objects.aSqlId - identifies the underlying SQL statement.aSearchCriteria - criteria for the given search, containing WHERE and ORDER BY clauses.aParams - parameters for the SQL statement, corresponding to the given criteria.
List of Model Objects, corresponding to the input criteria. The list may be empty.
DAOException
public static int edit(SqlId aSqlId,
Object... aParams)
throws DAOException,
DuplicateException
aSqlId - identifies the underlying SQL statement.aParams - parameters for the SQL statement.
DAOException
DuplicateException
public static Id add(SqlId aSqlId,
Object... aParams)
throws DAOException,
DuplicateException
This operation is not supported by all databases. See
Statement for more information.
aSqlId - identifies the underlying SQL statement.aParams - parameters for the SQL statement.
DAOException
DuplicateException
public static int delete(SqlId aSqlId,
Id aId)
throws DAOException
aSqlId - identifies the underlying SQL statement.aId - identifies the item to be deleted.
DAOException
public static <T> T fetchCompound(Class<T> aClassParent,
Class<?> aClassChild,
int aNumTrailingColsForChildList,
SqlId aSqlId,
Object... aParams)
throws DAOException
The ResultSet is parsed into a single parent Model Object having a List of 0..N child Model Objects. See note on compound objects for more information.
aClassParent - class of the parent Model Object.aClassChild - class of the child Model Object.aNumTrailingColsForChildList - number of columns appearing at the end of the ResultSet which
are passed to the child constructor.aSqlId - identifies the underlying SQL statement.aParams - parameters to the underlying SQL statement.
DAOException
public static <T> List<T> listCompound(Class<T> aClassParent,
Class<?> aClassChild,
int aNumTrailingColsForChildList,
SqlId aSqlId,
Object... aParams)
throws DAOException
The ResultSet is parsed into a List of parent Model Objects, each having 0..N child Model Objects. See note on compound objects for more information.
aClassParent - class of the parent Model Object.aClassChild - class of the child Model Object.aNumTrailingColsForChildList - number of columns appearing at the end of the ResultSet which
are passed to the child constructor.aSqlId - identifies the underlying SQL statement.aParams - parameters to the underlying SQL statement.
DAOException
|
Version 3.8.0
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||