Saturday, 18 March 2017

Return callback from procedure in node oracledb

I have a procedure which returns a cursor as one of its OUT variables. I am using node oracledb to get the result. Taking the help from the node oracledb documentation, I was able to execute the procedure. Now, I want to return callback from procedure so that I can use it in multiple files without having to write the entire code again just like this.

The code I have written is as follows -

var oracledb = require('oracledb');
var dbConfig = require('./config.js');

var numRows = 10;  // number of rows to return from each call to getRows()
oracledb.outFormat = oracledb.OBJECT;
oracledb.getConnection(
  {
    user          : dbConfig.user,
    password      : dbConfig.password,
    connectString : dbConfig.connectString,
    myProc : dbConfig.proc1
  },
  function(err, connection)
  {
    if (err) { console.error(err.message); return; }
    var bindvars = {
      cursor:  { type: oracledb.CURSOR, dir : oracledb.BIND_OUT },
      error_code : { type: oracledb.STRING, dir: oracledb.BIND_OUT },
      error_msg : { type: oracledb.STRING, dir: oracledb.BIND_OUT }
    };
    connection.execute(
      "BEGIN gt_log(:cursor,:e_cde,:e_msg); END;",
      bindvars,
      function(err, result)
      {
        if (err) {
          console.error(err.message);
          doRelease(connection);
          return;
        }
        console.log(result.outBinds.cursor.metaData);
        fetchRowsFromRS(connection, result.outBinds.cursor, numRows);
      });
  });

function fetchRowsFromRS(connection, resultSet, numRows)
{
  resultSet.getRows( // get numRows rows
    numRows,
    function (err, rows)
    {
      if (err) {
        console.log(err);
        doClose(connection, resultSet); // always close the result set
      } else if (rows.length === 0) {    // no rows, or no more rows
        doClose(connection, resultSet); // always close the result set
      } else if (rows.length > 0) {
        console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
        console.log(rows);
        fetchRowsFromRS(connection, resultSet, numRows);
      }
    });
}

function doRelease(connection)
{
  connection.close(
    function(err)
    {
      if (err) { console.error(err.message); }
    });
}

function doClose(connection, resultSet)
{
  resultSet.close(
    function(err)
    {
      if (err) { console.error(err.message); }
      doRelease(connection);
    });
}

Can anyone help me in converting this to something like this? So that I just have to call a function.



via Dalton2

No comments:

Post a Comment