Example 7: Simple Procedure that Invokes Another Procedure

This custom procedure invokes another procedure.

package proc;
import com.compositesw.extension.*;
import java.sql.*;
public class SimpleProcInvoke
  implements CustomProcedure
{
  private ExecutionEnvironment qenv;
  private ProcedureReference proc;
  public SimpleProcInvoke() { }
  /**
   * This is called once just after constructing the class. The
   * environment contains methods used to interact with the server.
   */
  public void initialize(ExecutionEnvironment qenv) {
    this.qenv = qenv;
  }
  /**
   * Called during introspection to get the description of the input
   * and output parameters. Should not return null.
   */
  public ParameterInfo[] getParameterInfo() {
    return new ParameterInfo[] {
      new ParameterInfo("arg1", Types.INTEGER, DIRECTION_IN),
      new ParameterInfo("operator", Types.VARCHAR, DIRECTION_IN),
      new ParameterInfo("arg2", Types.INTEGER, DIRECTION_IN),
      new ParameterInfo("result", Types.INTEGER, DIRECTION_OUT),
    };
  }
  /**
   * Called to invoke the stored procedure. Will only be called a
   * single time per instance. Can throw CustomProcedureException or
   * SQLException if there is an error during invoke.
   */
  public void invoke(Object[] inputValues)
    throws CustomProcedureException, SQLException
  {
    proc = qenv.lookupProcedure("/services/databases/tutorial/expr");
    proc.invoke(inputValues);
  }
  /**
   * Called to retrieve the number of rows that were inserted,
   * updated, or deleted during the execution of the procedure. A
   * return value of -1 indicates that the number of affected rows is
   * unknown. Can throw CustomProcedureException or SQLException if
   * there is an error when getting the number of affected rows.
   */
  public int getNumAffectedRows() {
    return 0;
  }
  /**
   * Called to retrieve the output values. The returned objects
   * should obey the Java to SQL typing conventions as defined in the
   * table above. Output cursors can be returned as either
   * CustomCursor or java.sql.ResultSet. Can throw
   * CustomProcedureException or SQLException if there is an error
   * when getting the output values. Should not return null.
   */
  public Object[] getOutputValues()
    throws CustomProcedureException, SQLException
  {
    return proc.getOutputValues();
  }
  /**
   * Called when the procedure reference is no longer needed. Close
   * can be called without retrieving any of the output values (such
   * as cursors) or even invoking, so this needs to do any remaining
   * cleanup. Close can be called concurrently with any other call
   * such as "invoke" or "getOutputValues". In this case, any pending
   * methods should immediately throw a CustomProcedureException.
   */
  public void close()
    throws CustomProcedureException, SQLException
  {
    if (proc != null)
      proc.close();
  }
  //
  // Introspection methods
  //
  /**
   * Called during introspection to get the short name of the stored
   * procedure. This name can be overridden during configuration.
   * Should not return null.
   */
  public String getName() {
    return "SimpleProcInvoke";
  }
  /**
   * Called during introspection to get the description of the stored
   * procedure. Should not return null.
   */
  public String getDescription() {
    return "This procedure invokes another procedure.";
  }
  //
  // Transaction methods
  //
  /**
   * Returns true if the custom procedure uses transactions. If this
   * method returns false then commit and rollback will not be called.
   */
  public boolean canCommit() {
    return false;
  }
  /**
   * Commit any open transactions.
   */
  public void commit() { }
  /**
   * Rollback any open transactions.
   */
  public void rollback() { }
  /**
   * Returns true if the transaction can be compensated.
   */
  public boolean canCompensate() {
    return false;
  }
  /**
   * Compensate any committed transactions (if supported).
   */
  public void compensate(ExecutionEnvironment qenv) { }
}