Interface QueryInfo

  • All Known Implementing Classes:
    Query, UpdateQuery

    public interface QueryInfo
    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).

    Author:
    OpenCloud
    • Method Detail

      • setParameters

        void setParameters​(PreparedStatement stmt)
                    throws SQLException
        Set the parameters on the PreparedStatement object created with the SQL returned by 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.

        Parameters:
        stmt - the JDBC prepared statement object
        Throws:
        SQLException
      • getExecuteType

        QueryInfo.ExecuteType getExecuteType()
        Get the type of execute method to call on the prepared statement.
        Returns:
        the execute type
        Since:
        1.1
      • getRetryBehaviour

        QueryInfo.RetryBehaviour getRetryBehaviour()
        Get the retry behaviour to use for this query.
        Returns:
        the retry behaviour
        Since:
        1.2
      • getStatementType

        QueryInfo.StatementType getStatementType()
        Get the statement type for this query.
        Returns:
        the statement type
        Since:
        1.3