【发布时间】:2012-08-19 13:37:30
【问题描述】:
我有一个返回多个结果集的 PostgresQL 函数。我可以毫无问题地在 .net 中提取这些结果集(所以我知道我的函数可以正常工作),但是我在使用 node-postgres 时遇到了麻烦。
结果对象返回一个包含 7 个项目的数组,该数组与返回的数据集的数量相匹配。
在 Node 中,7 行中的每一行都只包含一个<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.
}
那么:node-postgres 是否支持多个结果集,如果支持,有什么关于如何提取的建议吗?
编辑:这是我与 node-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
});
【问题讨论】:
-
...它们都没有真正提供有关如何从 refcursor 获取的答案。对不起。在 SQL 中从
refcursor中获取行看起来要么文档严重不足,要么真的很痛苦。 -
刚刚与创建者 Brian Carlson 核实过,他说他没有做任何事情来支持 recursors。我将提出一个 GIT 问题,看看是否可以解决。
-
Alex,你为什么一开始想要多个结果集?您是否坚持使用 postgres 功能?或者您是否像我一样认为一次完成所有数据库查询并以捆绑包的形式返回所有结果会比多次服务器到数据库往返更快?如果是后者,“多结果集方法”最终会更快吗?
标签: node.js postgresql node-postgres