public class QueryPreparer extends Object
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 ... }
Modifier and Type | Class and Description |
---|---|
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.ListPlaceHolder
A SqlObject which outputs 0 or more '?' separated by commas, and records
the current indexes at the times the
appendTo method is
called. |
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.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_START_INDEX
the default first index that will be assigned to a placeholder
|
Constructor and Description |
---|
QueryPreparer() |
QueryPreparer(int startIndex)
Creates a QueryPreparer with a different startIndex from the default.
|
public static final int DEFAULT_START_INDEX
public QueryPreparer()
public QueryPreparer(int startIndex)
public QueryPreparer.PlaceHolder getNewPlaceHolder()
public QueryPreparer.MultiPlaceHolder getNewMultiPlaceHolder()
public QueryPreparer.ListPlaceHolder getNewListPlaceHolder()
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
Copyright © 2006–2021 OpenHMS. All rights reserved.