Skip to content

Commit fa7aa22

Browse files
author
Aurynn Shaw
committed
Update to support the fully expanded, node-style error response pattern, specifically:
db.query(query, args, function (error, results){});
1 parent a99f336 commit fa7aa22

File tree

2 files changed

+70
-34
lines changed

2 files changed

+70
-34
lines changed

demo.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
var sys = require("sys");
22
var pg = require("./lib/postgres-pure");
3-
pg.DEBUG=0;
3+
// pg.DEBUG=4;
44

5-
var db = new pg.connect("pgsql://test:12345@localhost:5432/pdxpugtest");
6-
// db.query("explain analyze select * from pg_class ;", function (rs, tx) {
7-
// sys.puts(sys.inspect(rs));
8-
// // tx.query("SELECT 2::int as querytest2", function (rs) {
9-
// // sys.puts(sys.inspect(rs));
10-
// // });
11-
// });
5+
var db = new pg.connect("pgsql://test:12345@localhost:5432/returning_test");
6+
db.query("SELECT 1::querytest;", function (error, rs, tx) {
7+
if (error) {
8+
console.log("Error!");
9+
sys.puts(error);
10+
sys.puts(error.code);
11+
}
12+
else {
13+
sys.puts(sys.inspect(rs));
14+
}
15+
16+
// tx.query("SELECT 2::int as querytest2", function (rs) {
17+
// sys.puts(sys.inspect(rs));
18+
// });
19+
});
1220

13-
db.prepare("INSERT INTO pdxpug (id) VALUES (?) RETURNING id", function (sth) {
14-
sth.execute(1, function(rs) {
21+
db.prepare("INSERT INTO returning_test (val) VALUES (?) RETURNING id, val", function (sth) {
22+
sth.execute("text value", function(e, rs) {
1523
if (rs === undefined) {
1624
console.log("No data.");
1725
}
1826
else {
1927
console.log(sys.inspect(rs));
2028
}
21-
2229
});
2330
});
2431

lib/postgres-pure.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,18 @@ function parse_response(code, buffer) {
199199
break;
200200
case 'E':
201201
type = "ErrorResponse";
202-
args = [{}];
202+
var err = {};
203+
// args = [{}];
203204
reader.multicstring().forEach(function (field) {
204-
args[0][field[0]] = field.substr(1);
205+
err[field[0]] = field.substr(1);
205206
});
207+
// Now, convert it to a more useful object.
208+
var obj = new Error();
209+
obj.code = err['C'];
210+
obj.message = err['M'];
211+
obj.severity = err['S'];
212+
213+
args = [obj];
206214
break;
207215
case 't':
208216
type = "ParameterDescription",
@@ -286,6 +294,14 @@ function parse_response(code, buffer) {
286294
return {type: type, args: args};
287295
}
288296

297+
function Error () {
298+
this.severity = "";
299+
this.code = "";
300+
this.message = "";
301+
this.toString = function () {
302+
return "[postgres.js] " + this.severity + ": " + this.message;
303+
}
304+
}
289305

290306
function message () {
291307
var self = this;
@@ -346,7 +362,7 @@ function message () {
346362
message.prototype = new process.EventEmitter;
347363
// message.prototype.constructor = message;
348364

349-
function Query(sql, callback, errback) {
365+
function Query(sql, callback) {
350366
message.call(this);
351367
this.sql = sql;
352368
var q = this;
@@ -376,7 +392,7 @@ function Query(sql, callback, errback) {
376392
sys.debug("Callback: " + callback);
377393
}
378394

379-
callback(q.results);
395+
callback(null, q.results);
380396
});
381397
q.toString = function () { return "Query: " + q.length};
382398
q.on("RowDescription", function (desc) {
@@ -385,6 +401,10 @@ function Query(sql, callback, errback) {
385401
sys.debug("Caught RowDescription message.");
386402
}
387403
});
404+
q.on("ErrorResponse", function (e) {
405+
callback(e);
406+
});
407+
388408
}
389409

390410
Query.prototype = new message;
@@ -492,6 +512,15 @@ function Prepared(sql, conn /*, use_named */) {
492512
}
493513
});
494514

515+
self.on("ErrorResponse", function (err) {
516+
if (currExec != null) {
517+
currExec.emit("ErrorResponse", err);
518+
}
519+
else {
520+
self.error = e;
521+
}
522+
});
523+
495524
// self.empty = function () {
496525
// return self.__proto__.empty.call(self);
497526
// }
@@ -594,7 +623,7 @@ function Prepared(sql, conn /*, use_named */) {
594623
}
595624
// Callback gets wrapped at the tx layer,
596625
// so we know when this gets tripped.
597-
callback(eP.results);
626+
callback(null, eP.results);
598627
}
599628
else if (eP.noData === true) {
600629
// Callback gets wrapped at the tx layer,
@@ -615,14 +644,14 @@ function Prepared(sql, conn /*, use_named */) {
615644
eP.on("newRow", function (row) {
616645
eP.results.push(row);
617646
});
647+
eP.on("ErrorResponse", function (e) {
648+
callback(e);
649+
});
618650
if(exports.DEBUG > 0) {
619651
sys.debug("Pushing execute message to eB in Prepared.");
620652
}
621653
eB.push(eP);
622654
}
623-
self.on("Error", function (err) {
624-
625-
});
626655
}
627656
Prepared.prototype = new message();
628657
Prepared.prototype.constructor = Prepared;
@@ -1025,10 +1054,10 @@ function Connection(args) {
10251054
var callback = args.pop();
10261055

10271056
if (typeof(callback) === 'function') {
1028-
args.push(function (rs) {
1057+
args.push(function (err, rs) {
10291058
// do the implicit sync/commit here?
10301059
// tx.commit();
1031-
callback(rs, tx); // So they have access to the transaction?
1060+
callback(err, rs, tx); // So they have access to the transaction?
10321061
});
10331062
}
10341063
else {
@@ -1135,17 +1164,17 @@ function Connection(args) {
11351164
11361165
Easy, and very nifty.
11371166
*/
1138-
conn.on('newListener', function (e, listener) {
1139-
if (e === 'String') {
1140-
// It's a string.
1141-
if (!(e in ['newListener'])) {
1142-
conn.notify(e, listener);
1143-
}
1144-
}
1145-
});
1146-
conn.notify = function (name) {
1147-
1148-
}
1167+
// conn.on('newListener', function (e, listener) {
1168+
// if (e === 'String') {
1169+
// // It's a string.
1170+
// if (!(e in ['newListener'])) {
1171+
// conn.notify(e, listener);
1172+
// }
1173+
// }
1174+
// });
1175+
// conn.notify = function (name) {
1176+
//
1177+
// }
11491178
}
11501179
Connection.prototype = new process.EventEmitter();
11511180

@@ -1217,11 +1246,11 @@ function Transaction (connection /*, params */) {
12171246
// We have an otherwise normal query.
12181247
// This does not require a normal Sync message
12191248
// or any other such magic-ery.
1220-
var q = new Query(sql, function (rs) {
1249+
var q = new Query(sql, function (err, rs) {
12211250
if (exports.DEBUG > 3) {
12221251
sys.debug("RS is:" + sys.inspect(rs));
12231252
}
1224-
callback(rs);
1253+
callback(err, rs);
12251254
// callbackRun();
12261255
});
12271256
if (exports.DEBUG > 0) {

0 commit comments

Comments
 (0)