node-postgres是否支持多个结果集

我有一个PostgresQL函数返回多个结果集。 我可以在.net中提取这些结果集没有问题(所以我知道我的function正常工作),但我有麻烦与node-postgres这样做。

结果对象返回与返回的数据集数相匹配的7个项目的数组。

在Node中,7行中的每一行都只包含一个<unnamed portal 1>的string。

 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. } 

所以:node-postgres是否支持多个结果集,如果是的话,有关如何提取的build议?

编辑:这是我使用的节点postgres的代码,如果其他人需要在未来使用它。

 // 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 }); 

更新 :看到这个优秀的教程如何获取和pipe理refcursors的解释。


由于node-postgres不能识别你作为结果集句柄返回的refcursors,它似乎不支持来自PostgreSQL的多个结果集。 这很公平,因为PostgreSQL并不真的支持多个结果集,它们只是用refcursors来模拟。

您可以通过SQL级游标命令从一个refcursor SQL 级游标命令 ,虽然它的文档是悲惨的。 你不需要使用PL / PgSQL光标处理来做到这一点。 只是:

 FETCH ALL FROM "<unnamed portal 1>"; 

注意双引号,这很重要。 使用<unnamed portal 1>replace函数返回的refcursor名称。

还要注意,除非光标是用WITH HOLD创build的,否则创buildrefcursor的事务必须是开放的。 当事务提交或回滚时,非HOLD游标被closures。

例如,给定虚拟refcursor返回函数:

 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=#