Ns_PdDbSpExec

Name

Ns_PdDbSpExec -- Execute stored procedure

Syntax

void Ns_PdDbSpExec (void *handle)

Description

This function executes a stored procedure that has been initialized with Ns_PdDbSpStart and Ns_PdDbSpSetParam.

Pseudo-code Example

void
Ns_PdDbSpExec(void *handle) {
  DBMSState   *state = (DBMSState *)handle;
  int         retcode;
  int         morep = 1;
  int         restype;
  int         status = NS_ERROR;
  char        *name, *value;
  retcode = DBMSSend(state->cmd);
  if (retcode != DBMS_OK) {
    Ns_PdLog(Error, "DbSpExec: DBMSSend() failed.");
    Ns_PdSendException(state->exceptionCode, state->exceptionMsg);
    return;
  }
  while (morep &&
   (retcode = DBMSResults(state->cmd, &res_type)) == DBMS_OK) {
    switch (res_type) {
      case DBMS_ROW_RESULT:
        /*
         * Rows were returned.
         */
        morep = 0;
        status = DB_ROWS;
        state->fetchingRows = 1;
        break;
      case DBMS_CMD_FAIL:
        if (DBMSCancel(state->cmd) != DBMS_OK) {
          Ns_PdLog(Error,
            "DbSpExec: DBMSCancel after DBMS_CMD_FAIL failed.");
          morep = 0;
        }
        break;
      case DBMS_STATUS_RESULT:
        /*
         * Process a stored procedure return code and go on.
         */
        state->returnCode = DBMSGetReturnCode(state->cmd)
        break;
      case DBMS_PARAM_RESULT:
        /*
         * Output parameters were returned.
         */
        while (DBMSGetOutputParameter(&name, &value) == DBMS_OK) {
          SaveOutputParameter(state, name, value);
        }
        break;
      case DBMS_CMD_SUCCEED:
        /*
         * It succeeded and returned nothing.
         */
        status = DB_DML;
        break;
      default:
        Ns_PdLog(Error, "DbSpExec: "
          "DBMSResults returned unexpected result type: %d",
          res_type);
        morep = 0;
    }
  }
  if (status == NS_ERROR) {
    Ns_PdSendException(state->exceptionCode, state->exceptionMsg);
  } else {
    Ns_PdSendString(OK_STATUS);
    status == DB_ROWS ? Ns_PdSendString(EXEC_RET_ROWS) :
    Ns_PdSendString(EXEC_RET_DML);
  }
}