A callback interface implemented by SBBs to set the SQL statement and parameters for the query.
It is the responsibility of the service developer to ensure all parameters required by the SQL returned by
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).