Node-postgres support multiple result sets

I have a PostgresQL function that returns multiple result sets. I can extract these results in .net without problems (so that I know that my function works correctly), but I am having problems with node-postgres.

The result object returns an array of 7 elements, which corresponds to the number of returned data sets.

In Node, each of the 7 lines simply contains a line <unnamed portal 1>.

connection.query("BEGIN");
connection.query({text: "SELECT getoperationaldatasetmodel($1)", values : [clientid]}, function(err, results) {


  if (err) {
    connection.query("COMMIT");
    self.pool.release(connection);
    callback(err);
  }
  else {
    var opsDataset = null;
    var rows = results.rows;
    // this returns 7 rows but the rows do not contain data but rather the name of the dataset.
  }

So: does node-postgres support multiple result sets, and if so, any suggestions on how to extract it?

EDIT: Here is the code I used with node-postgres if anyone else should use it in the future.

// must wrap in a transaction otherwise won't be able to see the multiple sets.
connection.query("BEGIN");
connection.query({text: "SELECT myfunction($1)", values : [clientid]}, function(err, results) {

  if (err) {

     // handle error here
     connection.query("COMMIT;");
  }
  else {

    connection.query('FETCH ALL FROM "<unnamed portal 1>"',  function(err, r1) {
        // r1.rows will contain the data for the first refcursor
    });
    connection.query('FETCH ALL FROM "<unnamed portal 2>"',  function(err, r2) {
        // r2.rows will contain the data for the second refcursor
    });

    // remember to handle the closure of the transaction

});
+5
source share
1 answer

: . , .


node -postgres , , PostgreSQL. , PostgreSQL , refcursors.

FETCH refcursor SQL SQL, . PL/PgSQL. :

FETCH ALL FROM "<unnamed portal 1>";

, . refcursor, , <unnamed portal 1>.

, , refcursor, , WITH HOLD. -HOLD , .

, :

CREATE OR REPLACE FUNCTION dummy_cursor_returning_fn() RETURNS SETOF refcursor AS $$
DECLARE
    curs1 refcursor;
    curs2 refcursor;
BEGIN
    OPEN curs1 FOR SELECT generate_series(1,4);
    OPEN curs2 FOR SELECT generate_series(5,8);
    RETURN NEXT curs1;
    RETURN NEXT curs2;
    RETURN;
END;
$$ LANGUAGE 'plpgsql';

..., , , FETCH, :

regress=# BEGIN;
BEGIN
regress=# SELECT dummy_cursor_returning_fn();
 dummy_cursor_returning_fn 
---------------------------
 <unnamed portal 7>
 <unnamed portal 8>
(2 rows)

regress=# FETCH ALL FROM "<unnamed portal 7>";
 generate_series 
-----------------
               1
               2
               3
               4
(4 rows)

regress=# FETCH ALL FROM "<unnamed portal 8>";
 generate_series 
-----------------
               5
               6
               7
               8
(4 rows)

regress=# 
+5

All Articles