Skip to content

Commit e608772

Browse files
author
Aurynn Shaw
committed
Adding error test runner, t/error.js
Update the test runners and demo.js to use console.log instead of sys.puts. Updated the Query object to be compliant with multiple Queries in the same Transaction. Better support for system Transactions.
1 parent fa7aa22 commit e608772

File tree

6 files changed

+96
-87
lines changed

6 files changed

+96
-87
lines changed

demo.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,47 @@ var db = new pg.connect("pgsql://test:12345@localhost:5432/returning_test");
66
db.query("SELECT 1::querytest;", function (error, rs, tx) {
77
if (error) {
88
console.log("Error!");
9-
sys.puts(error);
10-
sys.puts(error.code);
9+
console.log(error);
10+
console.log(error.code);
1111
}
1212
else {
13-
sys.puts(sys.inspect(rs));
13+
console.log(sys.inspect(rs));
1414
}
1515

16-
// tx.query("SELECT 2::int as querytest2", function (rs) {
17-
// sys.puts(sys.inspect(rs));
18-
// });
19-
});
20-
21-
db.prepare("INSERT INTO returning_test (val) VALUES (?) RETURNING id, val", function (sth) {
22-
sth.execute("text value", function(e, rs) {
23-
if (rs === undefined) {
24-
console.log("No data.");
25-
}
26-
else {
27-
console.log(sys.inspect(rs));
28-
}
16+
tx.query("SELECT 2::int as querytest2", function (err, rs) {
17+
console.log(sys.inspect(rs));
2918
});
19+
tx.query("SELECT 3::qt" ,function (err, rs) {
20+
console.log(sys.inspect(err));
21+
})
3022
});
3123

24+
// db.prepare("INSERT INTO returning_test (val) VALUES (?) RETURNING id, val", function (sth) {
25+
// sth.execute("text value", function(e, rs) {
26+
// if (rs === undefined) {
27+
// console.log("No data.");
28+
// }
29+
// else {
30+
// console.log(sys.inspect(rs));
31+
// }
32+
// });
33+
// });
34+
3235
// db.prepare().on("some_event");
3336

3437
// db.prepare("SELECT ?::int AS preparetest", function (sth, tx) {
35-
// sth.execute(1, function (rs) {
38+
// sth.execute(1, function (err, rs) {
3639
// sys.puts(sys.inspect(rs));
3740
// });
38-
// sth.execute(2, function (rs) {
41+
// sth.execute(2, function (err, rs) {
3942
// sys.puts(sys.inspect(rs));
4043
//
4144
// });
42-
// // tx.prepare("SELECT ?::int AS preparetest2", function (sth) {
43-
// // sth.execute(3, function (rs) {
44-
// // sys.puts(sys.inspect(rs));
45-
// // }) ;
46-
// // });
45+
// tx.prepare("SELECT ?::int AS preparetest2", function (sth) {
46+
// sth.execute(3, function (err, rs) {
47+
// sys.puts(sys.inspect(rs));
48+
// }) ;
49+
// });
4750
// });
4851

4952
// db.transaction(function (tx) {

lib/postgres-pure.js

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,14 @@ function message () {
310310
this._position = 0; // instance variable
311311
this.setMessages = function (msg) {
312312
if (msg instanceof Array) {
313-
self.messages = msg;
313+
this.messages = msg;
314314
}
315-
self.length = self.messages.length;
315+
this.length = this.messages.length;
316316
}
317317
this.addMessage = function (msg) {
318318
if (msg != null && msg != undefined) {
319-
self.messages.push(msg);
320-
self.length = self.messages.length;
319+
this.messages.push(msg);
320+
this.length = this.messages.length;
321321
}
322322
}
323323
this.next = function() {
@@ -347,64 +347,57 @@ function message () {
347347
}
348348
return false;
349349
}
350-
this.on("Error", function (err) {
351-
if (errback !== null && errback !== undefined) {
352-
errback(err);
353-
}
354-
else {
355-
// We don't have an error handler? Odd..
356-
// Pass it up to our connection/transaction.
357-
conn.emit("Error", err);
358-
}
359-
});
360350
}
361351

362352
message.prototype = new process.EventEmitter;
363353
// message.prototype.constructor = message;
364354

365355
function Query(sql, callback) {
356+
var self = this;
366357
message.call(this);
367358
this.sql = sql;
368-
var q = this;
369-
q.results = [];
359+
this.results = [];
370360
var pos = 0;
361+
this.callback = callback;
371362
/*
372363
Returns the next query object in this object buffer.
373364
This can be null.
374365
375366
Should there be a Sync message here?
376367
*/
377-
q.setMessages([
368+
this.setMessages([
378369
{
379370
type: 'Query',
380-
args: [sql]
371+
args: [this.sql]
381372
},
382373
// {
383374
// type: 'Flush',
384375
// args: []
385376
// }
386377
]);
387-
q.on("newRow", function (row) {
388-
q.results.push(row);
378+
this.on("newRow", function (row) {
379+
// sys.debug("Got row: " + sys.inspect(row));
380+
// sys.debug(sys.inspect(self.results));
381+
self.results.push(row);
389382
});
390-
q.on("Complete", function (data) {
383+
this.on("Complete", function (data) {
391384
if (exports.DEBUG > 2) {
392-
sys.debug("Callback: " + callback);
385+
sys.debug("Callback: " + self.callback);
393386
}
394387

395-
callback(null, q.results);
388+
self.callback(null, self.results);
396389
});
397-
q.toString = function () { return "Query: " + q.length};
398-
q.on("RowDescription", function (desc) {
399-
q.row_description = desc;
390+
this.toString = function () { return "Query: " + this.length};
391+
this.on("RowDescription", function (desc) {
392+
self.row_description = desc;
400393
if (exports.DEBUG > 2) {
401394
sys.debug("Caught RowDescription message.");
402395
}
403396
});
404-
q.on("ErrorResponse", function (e) {
405-
callback(e);
397+
this.on("ErrorResponse", function (e) {
398+
self.callback(e);
406399
});
407-
400+
// sys.debug(this.listeners("newRow"));
408401
}
409402

410403
Query.prototype = new message;
@@ -657,20 +650,6 @@ Prepared.prototype = new message();
657650
Prepared.prototype.constructor = Prepared;
658651

659652

660-
function Sync (callback) {
661-
var q = this;
662-
q.setMessages([
663-
{
664-
type: "Sync",
665-
args: null
666-
},
667-
]);
668-
//
669-
}
670-
671-
Sync.prototype = new message;
672-
673-
674653
/* Initializes a connection to the database.
675654
DB connections are of the form:
676655
@@ -1064,10 +1043,10 @@ function Connection(args) {
10641043
// No callback.
10651044
args.push(callback); // re-add it.
10661045
args.push(function (rs) {
1067-
// This will eventually be handled by an autocommit flag in
1068-
// the parameters object as passed to the connection.
1069-
1070-
//tx.(); // implicit rollback.
1046+
/*
1047+
This becomes the implied callback.
1048+
For the moment, do nothing.
1049+
*/
10711050
});
10721051
}
10731052
tx.query.apply(tx, args);
@@ -1228,7 +1207,7 @@ function Transaction (connection /*, params */) {
12281207
/* sql, some_args, callback */
12291208
this.query = function () {
12301209
var args = Array.prototype.slice.call(arguments);
1231-
if (exports.DEBUG>3) {
1210+
if (exports.DEBUG > 3) {
12321211
sys.debug("Args are: " + args);
12331212
}
12341213
var sql = args.shift();
@@ -1246,6 +1225,7 @@ function Transaction (connection /*, params */) {
12461225
// We have an otherwise normal query.
12471226
// This does not require a normal Sync message
12481227
// or any other such magic-ery.
1228+
12491229
var q = new Query(sql, function (err, rs) {
12501230
if (exports.DEBUG > 3) {
12511231
sys.debug("RS is:" + sys.inspect(rs));
@@ -1256,7 +1236,7 @@ function Transaction (connection /*, params */) {
12561236
if (exports.DEBUG > 0) {
12571237
sys.debug("New plain query created: "+q);
12581238
}
1259-
thisp.push(q);
1239+
this.push(q);
12601240
}
12611241
}
12621242

t/complex_prepared.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ pg.DEBUG=0;
44

55
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
66

7-
db.prepare("SELECT ?::int AS foobar", function (sth, tx) {
8-
sth.execute(1, function (rs) {
7+
db.prepare("SELECT ?::int AS foobar", function (err, sth, tx) {
8+
sth.execute(1, function (err, rs) {
99
console.log(eq(rs[0]['foobar'], 1));
10-
//sys.puts(sys.inspect(rs));
10+
//console.log(sys.inspect(rs));
1111
});
12-
sth.execute(2, function (rs) {
12+
sth.execute(2, function (err, rs) {
1313
console.log(eq(rs[0]['foobar'], 2));
14-
// sys.puts(sys.inspect(rs));
14+
// console.log(sys.inspect(rs));
1515

1616
});
1717
tx.prepare("SELECT ?::int AS cheese", function (sth) {
18-
sth.execute(3, function (rs) {
18+
sth.execute(3, function (err, rs) {
1919
console.log(eq(rs[0]['cheese'], 3));
20-
// sys.puts(sys.inspect(rs));
20+
// console.log(sys.inspect(rs));
2121
});
2222
});
2323
// db.close();

t/error.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Tests whether the error object is being correctly passed to the query
2+
callback function */
3+
var sys = require("sys");
4+
var pg = require("../lib/postgres-pure");
5+
pg.DEBUG=0;
6+
7+
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
8+
db.query("SELECT 1::foobar;", function (err, rs, tx) {
9+
console.log("error not null: " + not_eq(null, err));
10+
console.log("error code is 42704 (no such type foobar): " + eq("42704", err.code));
11+
});
12+
db.close();
13+
14+
function not_eq (l, r) {
15+
if (l !== r) {
16+
return "ok"
17+
}
18+
return "not ok\n " + l + " != " + r;
19+
}
20+
21+
function eq (l, r) {
22+
if (l === r) {
23+
return "ok"
24+
}
25+
return "not ok\n " + l + " != " + r;
26+
}

t/query.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ var pg = require("../lib/postgres-pure");
33
pg.DEBUG=0;
44

55
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
6-
db.query("SELECT 1::int as foobar;", function (rs, tx) {
7-
sys.puts(sys.inspect(rs));
8-
tx.query("SELECT 2::int as foobartwo", function (rs) {
9-
sys.puts(sys.inspect(rs));
6+
db.query("SELECT 1::int as foobar;", function (err, rs, tx) {
7+
console.log(sys.inspect(rs));
8+
tx.query("SELECT 2::int as foobartwo", function (err, rs) {
9+
console.log(sys.inspect(rs));
1010
});
1111
});
1212
db.close();

t/simple_prepared.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ pg.DEBUG=0;
44

55
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
66
db.prepare("SELECT ?::int AS foobar", function (sth, tx) {
7-
sth.execute(1, function (rs) {
8-
sys.puts(sys.inspect(rs));
7+
sth.execute(1, function (err, rs) {
8+
console.log(sys.inspect(rs));
99
});
10-
sth.execute(2, function (rs) {
11-
sys.puts(sys.inspect(rs));
10+
sth.execute(2, function (err, rs) {
11+
console.log(sys.inspect(rs));
1212

1313
});
1414
});

0 commit comments

Comments
 (0)