|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.healthmarketscience.sqlbuilder.QueryPreparer
public class QueryPreparer
Helper class which keeps track of '?' positions in dynamically generated
prepared statements so that the query user can easily set the parameters
correctly, especially useful where the code which generates the prepared
query is separate from the code which uses the prepared query.
A QueryPreparer may only be used for a single query generation. Also, the
PlaceHolders will not have a valid position until the query is actually
converted to a string. After that, the PlaceHolders will never change
their stored index(es), so reuse in a new query is impossible. Likewise,
the state of the QueryPreparer is altered by the query generation, so, it
cannot be used in a new query either. However, the QueryPreparer utility
is designed so that it is not modified after query generation, so it can
safely be used concurrently with the original query string as long as
desired (so it can safely be used in a static context along with the
associated query string).
There are two main types of placeholders supported normal (dynamic) and
static. Normal placeholders consist of two sub-types (the
QueryPreparer.PlaceHolder and QueryPreparer.MultiPlaceHolder classes) and should be
used for values which are going to change for each invocation of the
PreparedStatement. A PlaceHolder may only be used in one place in a query,
while a MultiPlaceHolder can be re-used multiple times in the same query.
Static placeholders (subclasses of the
QueryPreparer.StaticPlaceHolder class) are a convenience placeholder which can be
used for values which will not change across invocations of a
PreparedStatement (and can also only be used in one place in a query, like
a PlaceHolder).
Examples:
// example using placeholders where another class is generating
// the actual query
QueryPreparer preparer = new QueryPreparer();
QueryPreparer.PlaceHolder param1PH = preparer.getNewPlaceHolder();
QueryPreparer.PlaceHolder param2PH = preparer.getNewPlaceHolder();
String prepQueryStr = otherObj.createQuery(param1PH, param2PH);
PreparedStatement ps = conn.prepareStatement(prepQueryStr);
for(Param param : paramList) {
param1PH.setLong(param.getValue1(), ps);
param2PH.setString(param.getValue2(), ps);
ResultSet rs = ps.executeQuery();
// ... parse results ...
}
// example using static placeholders and regular placeholders where
// this class is generating the query
Long param1 = 13;
String param2 = "foo";
QueryPreparer preparer = new QueryPreparer();
QueryPreparer.PlaceHolder param3PH = preparer.getNewPlaceHolder();
String prepQueryStr = new SelectQuery()
.addAllColumns()
.addCustomTable(table)
.setCondition(
ComboCondition.and(
BinaryCondition.eq(col1,
preparer.addStaticPlaceHolder(param1)),
BinaryCondition.eq(col2,
preparer.addStaticPlaceHolder(param2)),
BinaryCondition.eq(col3, param3PH)))
.validate.toString();
PreparedStatement ps = conn.prepareStatement(prepQueryStr);
for(String param3 : param3List) {
preparer.setStaticValues(ps); // sets param1, param2
param3PH.setString(param3, ps);
ResultSet rs = ps.executeQuery();
// ... parse results ...
}
| Nested Class Summary | |
|---|---|
static class |
QueryPreparer.BooleanStaticPlaceHolder
StaticPlaceHolder which calls setInt on the PreparedStatement with the saved value. |
static class |
QueryPreparer.IntegerStaticPlaceHolder
StaticPlaceHolder which calls setInt on the PreparedStatement with the saved value. |
static class |
QueryPreparer.LongStaticPlaceHolder
StaticPlaceHolder which calls setLong on the PreparedStatement with the saved value. |
static class |
QueryPreparer.MultiPlaceHolder
A SqlObject which outputs a '?', and records the current index at the time(s) the appendTo method is called. |
static class |
QueryPreparer.NullStaticPlaceHolder
StaticPlaceHolder which always calls setNull on the PreparedStatement with the saved sql type. |
static class |
QueryPreparer.ObjectStaticPlaceHolder<ObjType>
StaticPlaceHolder which calls setObject on the PreparedStatement with the saved value. |
static class |
QueryPreparer.PlaceHolder
A SqlObject which outputs a '?', and records the current index at the time the appendTo method is called. |
static class |
QueryPreparer.StaticPlaceHolder
Convenience PlaceHolder which also maintains a value which will always be inserted into the PreparedStatement when setValue is called. |
static class |
QueryPreparer.StringStaticPlaceHolder
StaticPlaceHolder which calls setString on the PreparedStatement with the saved value. |
static class |
QueryPreparer.TypedStaticPlaceHolder
StaticPlaceHolder which calls setObject on the PreparedStatement with the saved value and the saved sql type. |
| Field Summary | |
|---|---|
static int |
DEFAULT_START_INDEX
the default first index that will be assigned to a placeholder |
| Constructor Summary | |
|---|---|
QueryPreparer()
|
|
QueryPreparer(int startIndex)
Creates a QueryPreparer with a different startIndex from the default. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int DEFAULT_START_INDEX
| Constructor Detail |
|---|
public QueryPreparer()
public QueryPreparer(int startIndex)
| Method Detail |
|---|
public String toString()
toString in class Objectpublic QueryPreparer.PlaceHolder getNewPlaceHolder()
public QueryPreparer.MultiPlaceHolder getNewMultiPlaceHolder()
public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(String val)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(Long val)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(Integer val)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(Boolean val)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(long val)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(int val)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(boolean val)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(Object obj)
setStaticValues(java.sql.PreparedStatement)
public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(Object obj,
int sqlType)
setStaticValues(java.sql.PreparedStatement)public QueryPreparer.StaticPlaceHolder addStaticPlaceHolder(QueryPreparer.StaticPlaceHolder ph)
setStaticValues(java.sql.PreparedStatement)
public void setStaticValues(PreparedStatement ps)
throws SQLException
QueryPreparer.StaticPlaceHolder.setValue(java.sql.PreparedStatement) on all the StaticPlaceHolders
held by this class with the given PreparedStatement.
SQLException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||