|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.hdpagination.dataaccess.support.QueryBuilder
org.hdpagination.dataaccess.support.jdbc.JdbcQueryCallbackWrapper
public class JdbcQueryCallbackWrapper
This class is used as a convenient wrapper class for JdbcQueryCallback.
It simplifies the way to generate an instance of JdbcQueryCallback based on the searching criteria fields.
The way to use JdbcQueryCallbackWrapper usually follows the following steps:
(1) Call one of its static factory methods getInstance to get an initial instance of JdbcQueryCallbackWrapper.
(2) Call addRestriction methods from the instance.
(3) Call setStatementBeforeOrderBy method if necessary.
(4) Call setOrderBy method if you want to sort the search result.
A sample code is as follows:
JdbcQueryCallbackWrapper callback = JdbcQueryCallbackWrapper.newInstance("select productId,prodNo,price,madeIn,description from product p", ProductVO.class)
.addRestriction( Restrictions.eq("prodNo", crit.getProdNo()) )
.addRestriction( Restrictions.in("madeIn", crit.getMadeIn()) )
.addRestriction( Restrictions.contains("description", crit.getDescription()) )
.addRestriction( Restrictions.range("price", priceFrom, priceTo))
.setOrderBy("p.madeIn");
Behind the scene, the way JdbcQueryCallbackWrapper generates the full SQL query statement can be described as:
'selectFrom' (the first argument from static factory methods newInstance) to construct an initial SQL statement.addRestriction). For each restriction: (1) It appends the keyword 'WHERE' or 'AND' to the SQL statement first.
If it is the first restriction, 'WHERE' is added; otherwise 'AND' is added.
(2) Then it appends the restriction's clause statement (e.g. p.description LIKE ?) to the SQL statement.
statemetnBeforeOrderBy is set, then append its value to the SQL statement.orderBy is set, then append the generated ORDER BY clause to the SQL statement.
| Field Summary |
|---|
| Fields inherited from class org.hdpagination.dataaccess.support.QueryBuilder |
|---|
asending, countStatement, orderBy, selectFrom |
| Constructor Summary | |
|---|---|
JdbcQueryCallbackWrapper(java.lang.String selectFrom)
Please note that the factory methods newInstance are encouraged to use to create an object of JdbcQueryCallbackWrapper rather than instantiating it directly. |
|
| Method Summary | |
|---|---|
JdbcQueryCallbackWrapper |
addRestriction(Restriction restriction)
Add restriction applied to the query SELECT statement. |
JdbcQueryCallbackWrapper |
addRestriction(java.lang.String clause)
Add restriction applied to the query SELECT statement. |
JdbcQueryCallbackWrapper |
addRestriction(java.lang.String clause,
java.util.List params)
Add restriction applied to the query SELECT statement. |
JdbcQueryCallbackWrapper |
addRestriction(java.lang.String clause,
java.lang.Object param)
Add restriction applied to the query SELECT statement. |
JdbcQueryCallbackWrapper |
addRestriction(java.lang.String clause,
java.lang.Object[] params)
Add restriction applied to the query SELECT statement. |
JdbcQueryCallbackWrapper |
addRestriction(java.lang.String clause,
java.lang.Object[] params,
int[] sqlTypes)
Add restriction applied to the query SELECT statement. |
JdbcQueryCallbackWrapper |
addRestriction(java.lang.String clause,
java.lang.Object param,
int sqlType)
Add restriction applied to the query SELECT statement. |
java.lang.String |
getQueryStatement()
Query statement(e.g. |
static JdbcQueryCallbackWrapper |
newInstance(java.lang.String selectFrom)
As a convenient (or shortcut) factory method to get an initial JdbcQueryCallbackWrapper object. |
static JdbcQueryCallbackWrapper |
newInstance(java.lang.String selectFrom,
java.lang.Class rowMappedClass)
As a convenient (or shortcut) factory method to get an initial JdbcQueryCallbackWrapper object. |
static JdbcQueryCallbackWrapper |
newInstance(java.lang.String selectFrom,
RowMapper rowMapper)
As a convenient (or shortcut) factory method to get an initial JdbcQueryCallbackWrapper object. |
java.lang.Object |
processRow(java.sql.ResultSet rs)
Implementations implement this method to fetch each row of data in the ResultSet and map the fetched data to an object of any type you wish and finally return it. |
JdbcQueryCallbackWrapper |
setOrderBy(java.lang.String orderBy)
Set order by |
JdbcQueryCallbackWrapper |
setOrderBy(java.lang.String orderBy,
boolean ascending)
Set order by |
void |
setRowMapper(RowMapper rowMapper)
Set the property (type of RowMapper) for mapping rows of a ResultSet on a per-row basis when query is executed. |
JdbcQueryCallbackWrapper |
setStatementBeforeOrderBy(java.lang.String statementBeforeOrderBy)
Set the statement before "ORDER BY" clause and after "WHERE" clause. |
void |
setValues(java.sql.PreparedStatement ps)
set values on a PreparedStatement provided by JdbcQueryTemplate |
| Methods inherited from class org.hdpagination.dataaccess.support.QueryBuilder |
|---|
getCountRecordsQueryStatement, getQueryOrder, setCountRecordsQueryStatement, setQueryOrder |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface org.hdpagination.core.QueryCallback |
|---|
getCountRecordsQueryStatement, getQueryOrder, setQueryOrder |
| Constructor Detail |
|---|
public JdbcQueryCallbackWrapper(java.lang.String selectFrom)
Please note that the factory methods newInstance are encouraged to use to create an object of JdbcQueryCallbackWrapper rather than instantiating it directly.
selectFrom - the "SELECT ... FROM .." statement for the query. Ideally, it should not contain the "WHERE" clause,
otherwise when restrictions are added later, the full generated query statement will not be correct (it contains more than one "WHERE" key words).
The methods addRestriction are used to specify the "WHERE" clause.
It also should not contain the "ORDER BY" clause, the methods setOrderBy are for this purpose.| Method Detail |
|---|
public JdbcQueryCallbackWrapper setStatementBeforeOrderBy(java.lang.String statementBeforeOrderBy)
"ORDER BY" clause and after "WHERE" clause.
A good example of this is "GROUP BY" clause.
statementBeforeOrderBy -
public void setRowMapper(RowMapper rowMapper)
rowMapper - object that will map one object per rowpublic JdbcQueryCallbackWrapper setOrderBy(java.lang.String orderBy)
orderBy - the column after the key word ORDER BY in 'ORDER BY' clause
public JdbcQueryCallbackWrapper setOrderBy(java.lang.String orderBy,
boolean ascending)
orderBy - the column after the key word ORDER BY in 'ORDER BY' clauseascending - if sorted in ascending order
public JdbcQueryCallbackWrapper addRestriction(Restriction restriction)
SELECT statement.
If the argument 'restriction' is not null, the restriction will be appended to the WHERE
clause of the SELECT statement to filter the search result. Otherwise, the restriction will not be appended.
Typically, this method is used together with one of static factory methods from Restrictions. Restrictions
provides some static factory methods to generate object of Restriction conditionally based on the parameter value.
A general rule for some of its simple factory methods (eq, gt, ge, lt, le, contains) is:
java.lang.String), the method returns null. As a result,the restriction will not
be added.
In a typical search scenario, only if the search criteria field is provided (e.g. its value is not null and empty), the restriction should be applied.
This method particularly suits this scenario. You can avoid coding the annoying IF .. ELSE checking by doing this.
restriction - If the argument is null, the restriction is not applied to the query.
public JdbcQueryCallbackWrapper addRestriction(java.lang.String clause)
SELECT statement. The restriction will be appended to the WHERE clause of the
SELECT statement to filter the search result.
clause - The WHERE clause fragment (after prefixing "WHERE" or "AND" sql keywords) will be appended to the
SELECT statement.
Please note that the SQL key words "WHERE" or "AND" should be excluded from this clause fragment. When generating the full query statement,
the framework knows when to prefix "WHERE" or "AND". The clause fragment should not contain '?' IN parameter placeholder.
This argument can not be null or blank.
public JdbcQueryCallbackWrapper addRestriction(java.lang.String clause,
java.lang.Object param)
SELECT statement. The restriction will be appended to the WHERE clause of the
SELECT statement to filter the search result.
clause - The WHERE clause fragment (after prefixing "WHERE" or "AND" sql keywords) will be appended to the
SELECT statement.
Please note that the SQL key words "WHERE" or "AND" should be excluded from this clause fragment. When generating the full query statement,
the framework knows when to prefix "WHERE" or "AND". The clause fragment should contain ONE and ONLY ONE '?' IN parameter placeholder.
This argument can not be null or blank.param - parameter to bind to the query (leaving it to the java.sql.PreparedStatement to guess the corresponding SQL type);
It is to bind to the '?' IN parameter placeholder in the clause argument. It should be a valid type to call the method
java.sql.PreparedStatement.setObject(int parameterIndex, Object x) as the second argument.
This argument can not be null.
public JdbcQueryCallbackWrapper addRestriction(java.lang.String clause,
java.lang.Object param,
int sqlType)
SELECT statement. The restriction will be appended to the WHERE clause of the
SELECT statement to filter the search result.
clause - The WHERE clause fragment (after prefixing "WHERE" or "AND" sql keywords) will be appended to the
SELECT statement.
Please note that the SQL key words "WHERE" or "AND" should be excluded from this clause fragment. When generating the full query statement,
the framework knows when to prefix "WHERE" or "AND". The clause fragment should contain ONE and ONLY ONE '?' IN parameter placeholder.
This argument can not be null or blank.param - the parameter value binding to the '?' IN parameter placeholder in the clause argument. It should be a valid type to call the method
java.sql.PreparedStatement.setObject(int parameterIndex, Object x, int targetSqlType) as the second argument.
This argument can not be null.sqlType - SQL type of the binding parameter (constants from java.sql.Types)
public JdbcQueryCallbackWrapper addRestriction(java.lang.String clause,
java.lang.Object[] params)
SELECT statement. The restriction will be appended to the WHERE clause of the
SELECT statement to filter the search result.
clause - The WHERE clause fragment (after prefixing "WHERE" or "AND" sql keywords) will be appended to the
SELECT statement.
Please note that the SQL key words "WHERE" or "AND" should be excluded from this clause fragment. When generating the full query statement,
the framework knows when to prefix "WHERE" or "AND".
The clause fragment can contain multiple '?' IN parameter placeholders, and the number of placeholders
must match the number of elements in the params argument.
This argument can not be null or blank.params - the parameter values binding to the '?' IN parameter placeholders in the clause argument
(leaving it to the java.sql.PreparedStatement to guess the corresponding SQL type).
Its elements should be a valid type to call the method java.sql.PreparedStatement.setObject(int parameterIndex, Object x) as the second argument.
This argument can not be null and should contain at least one element.
public JdbcQueryCallbackWrapper addRestriction(java.lang.String clause,
java.util.List params)
SELECT statement. The restriction will be appended to the WHERE clause of the
SELECT statement to filter the search result.
clause - The WHERE clause fragment (after prefixing "WHERE" or "AND" sql keywords) will be appended to the
SELECT statement.
Please note that the SQL key words "WHERE" or "AND" should be excluded from this clause fragment. When generating the full query statement,
the framework knows when to prefix "WHERE" or "AND".
The clause fragment can contain multiple '?' IN parameter placeholders, and the number of placeholders
must match the number of elements in the params argument.
This argument can not be null or blank.params - the parameter values binding to the '?' IN parameter placeholders in the clause argument
(leaving it to the java.sql.PreparedStatement to guess the corresponding SQL type).
Its elements should be a valid type to call the method java.sql.PreparedStatement.setObject(int parameterIndex, Object x) as the second argument.
This argument can not be null and should contain at least one element.
public JdbcQueryCallbackWrapper addRestriction(java.lang.String clause,
java.lang.Object[] params,
int[] sqlTypes)
SELECT statement. The restriction will be appended to the WHERE clause of the
SELECT statement to filter the search result.
clause - The WHERE clause fragment (after prefixing "WHERE" or "AND" sql keywords) will be appended to the
SELECT statement.
Please note that the SQL key words "WHERE" or "AND" should be excluded from this clause fragment. When generating the full query statement,
the framework knows when to prefix "WHERE" or "AND".
The clause fragment can contain multiple '?' IN parameter placeholders, and the number of placeholders
must match the number of elements in the params argument.
This argument can not be null or blank.params - the parameter values binding to the '?' IN parameter placeholders in the clause argument.
Its elements should be a valid type to call the method java.sql.PreparedStatement.setObject(int parameterIndex, Object x, int targetSqlType)
as the second argument.
This argument can not be null and should contain at least one element.sqlTypes - SQL types of the binding parameters (constants from java.sql.Types)
This argument can not be null and should contain at least one element.
public java.lang.String getQueryStatement()
QueryCallback
getQueryStatement in interface QueryCallback
public void setValues(java.sql.PreparedStatement ps)
throws java.sql.SQLException
JdbcQueryCallback
setValues in interface JdbcQueryCallbackjava.sql.SQLException
public java.lang.Object processRow(java.sql.ResultSet rs)
throws java.sql.SQLException
JdbcQueryCallback
processRow in interface JdbcQueryCallbackjava.sql.SQLExceptionpublic static JdbcQueryCallbackWrapper newInstance(java.lang.String selectFrom)
JdbcQueryCallbackWrapper object.
A default implementation ColumnMapRowMapper
of RowMapper is used for mapping rows of a ResultSet on a per-row basis.
As a result, the elements in the search result(java.util.List) will have the type of java.util.Map,
representing all columns as key-value pairs: one entry for each column, with the column name (low case) as the key,
column value as the value.
Please note that the factory methods are encouraged to use to create an object of JdbcQueryCallbackWrapper rather than instantiating it directly.
selectFrom - the "SELECT ... FROM .." statement for the query. Ideally, it should not contain the "WHERE" clause,
otherwise when restrictions are added later, the full generated query statement will not be correct (it contains more than one "WHERE" key words).
The methods addRestriction are used to specify the "WHERE" clause.
It also should not contain the "ORDER BY" clause, the methods setOrderBy are for this purpose.
This argument can not be null or empty.
public static JdbcQueryCallbackWrapper newInstance(java.lang.String selectFrom,
RowMapper rowMapper)
JdbcQueryCallbackWrapper object.
A specified instance of RowMapper is used for mapping rows of a ResultSet on a per-row basis.
Please note that the factory methods are encouraged to use to create an object of JdbcQueryCallbackWrapper rather than instantiating it directly.
selectFrom - the "SELECT ... FROM .." statement for the query. Ideally, it should not contain the "WHERE" clause,
otherwise when restrictions are added later, the full generated query statement will not be correct (it contains more than one "WHERE" key words).
The methods addRestriction are used to specify the "WHERE" clause.
It also should not contain the "ORDER BY" clause, the methods setOrderBy are for this purpose.
This argument can not be null or empty.rowMapper - An instance of RowMapper is used for mapping rows of a ResultSet on a per-row basis.
To customise the rows mapping of a ResultSet, you only need to implement a class of RowMapper in your own way and pass an object of that class.
public static JdbcQueryCallbackWrapper newInstance(java.lang.String selectFrom,
java.lang.Class rowMappedClass)
JdbcQueryCallbackWrapper object.
Internally, the implementation BeanPropertyRowMapper
of RowMapper is used for mapping rows of a ResultSet on a per-row basis.
Briefly, column values are mapped based on matching the column name as obtained from result set metadata to public setters for the corresponding properties
(of the bean with type of the specified class - the second argument 'rowMappedClass').
The names are matched either directly or by transforming a name separating the parts with underscores to the same name using "camel" case.
Please note that the factory methods are encouraged to use to create an object of JdbcQueryCallbackWrapper rather than instantiating it directly.
Also very importantly, because BeanPropertyRowMapper internally uses some classes available only since Spring framework 2.5.2.
So this method can only be used if Spring framework 2.5.2 or later version is used.
selectFrom - the "SELECT ... FROM .." statement for the query. Ideally, it should not contain the "WHERE" clause,
otherwise when restrictions are added later, the full generated query statement will not be correct (it contains more than one "WHERE" key words).
The methods addRestriction are used to specify the "WHERE" clause.
It also should not contain the "ORDER BY" clause, the methods setOrderBy are for this purpose.
This argument can not be null or empty.rowMappedClass - the class is used by the underlying RowMapper implementation
BeanPropertyRowMapper to convert a row into a new instance of the specified mapped target class.
This argument can not be null.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||