public interface QueryInfo
getSql()
are set by setParameters(java.sql.PreparedStatement)
.
For a simple query, this interface could be implemented as follows:
class LongCodeLookupQuery implements QueryInfo { LongCodeLookupQuery(String shortCode) { this.shortCode = shortCode; } public String getSql() { return "SELECT longCode FROM mappingTable WHERE shortCode = ?"; } public void setParameters(PreparedStatement stmt) throws SQLException { stmt.setString(1, shortCode); } public ExecuteType getExecuteType() { return ExecuteType.QUERY; } public RetryBehaviour getRetryBehaviour() { return RetryBehaviour.TRY_ALL; } private final String shortCode; }For an SQL update, the implementation would be along these lines:
class LongCodeUpdate implements QueryInfo { LongCodeUpdate(String shortCode, String longCode) { this.shortCode = shortCode; this.longCode = longCode; } public String getSql() { return "UPDATE mappings SET longCode = ? WHERE shortCode = ?"; } public void setParameters(PreparedStatement stmt) throws SQLException { stmt.setString(1, longCode); stmt.setString(2, shortCode); } public ExecuteType getExecuteType() { return ExecuteType.UPDATE; } public RetryBehaviour getRetryBehaviour() { return RetryBehaviour.TRY_FIRST_ONLY; } private String shortCode; private String longCode; }For a stored procedure call, this could be an example implementation:
class ExtendLongCodeProcedure implements QueryInfo { ExtendLongCodeProcedure(String shortCode, String prefix) { this.shortCode = shortCode; this.prefix = prefix; } public String getSql() { return "{CALL EXTEND_LONGCODE_WITH_PREFIX(?,?,?)}"; } public void setParameters(PreparedStatement stmt) throws SQLException { CallableStatement cstmt = (CallableStatement) stmt; cstmt.registerOutParameter(1, Types.VARCHAR); cstmt.setString(2, shortCode); cstmt.setString(3, prefix); } public ExecuteType getExecuteType() { return ExecuteType.EXECUTE; } public RetryBehaviour getRetryBehaviour() { return RetryBehaviour.TRY_ALL; } public StatementType getStatementType() { return StatementType.CALLABLE; } private final String shortCode; private final String prefix; }NB: The object passed to the RA must not reference any fields on the enclosing SBB nor invoke any methods on it! It must be completely self-contained (i.e., it must contain a copy of data needed to set the parameters).
Modifier and Type | Interface and Description |
---|---|
static class |
QueryInfo.ExecuteType
The type of execute method to call on the prepared statement.
|
static class |
QueryInfo.RetryBehaviour
Retry behaviour options in the case of multiple data sources.
|
static class |
QueryInfo.StatementType
The type of Statement.
|
Modifier and Type | Method and Description |
---|---|
QueryInfo.ExecuteType |
getExecuteType()
Get the type of execute method to call on the prepared statement.
|
QueryInfo.RetryBehaviour |
getRetryBehaviour()
Get the retry behaviour to use for this query.
|
String |
getSql()
Get the SQL to be passed to
Connection.prepareStatement(String) . |
QueryInfo.StatementType |
getStatementType()
Get the statement type for this query.
|
void |
setParameters(PreparedStatement stmt)
Set the parameters on the PreparedStatement object created with the SQL returned by
getSql() . |
String getSql()
Connection.prepareStatement(String)
.void setParameters(PreparedStatement stmt) throws SQLException
getSql()
.
This method may also call configuration setters, e.g. setMaxRowCount().
The implementation of this method MUST NOT retain a reference to the prepared statement, nor call any methods
other than configuration setters and parameter setters.stmt
- the JDBC prepared statement objectSQLException
QueryInfo.ExecuteType getExecuteType()
QueryInfo.RetryBehaviour getRetryBehaviour()
QueryInfo.StatementType getStatementType()