Skip to content

Commit ff83589

Browse files
author
Aurynn Shaw
committed
Prepared queries now work mostly as expected. :)
1 parent 3724aae commit ff83589

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

demo.js

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

55
var db = new pg.connect("pgsql://aurynn:12345@localhost:5432/akhyana");
66
db.prepare("SELECT * FROM users WHERE id = $1;", function (sth) {
77
sys.debug("in callback");
88
sth.execute([1], function (rs) {
9-
for (var i = 0; i <= rs.length; i++) {
10-
sys.puts(rs[i]);
9+
sys.debug("In execute");
10+
for (var i = 0; i < rs.length; i++) {
11+
for (var key in rs[i]) {
12+
if (rs[i].hasOwnProperty(key)) {
13+
sys.puts(key +": " +rs[i][key]);
14+
}
15+
}
1116
}
17+
db.end();
1218
});
1319
});

lib/buffer_extras.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Buffer.makeWriter = function makeWriter() {
9898
},
9999
byte1: function byte1(string) {
100100
var b = new Buffer(1); // one octet.
101-
b.write(string, 'ascii', 0);
101+
b.write(string, 'utf8', 0);
102102
data.push(b);
103103
return writer;
104104
},

lib/postgres-pure.js

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,45 @@ function encoder(header) {
5151

5252
// http://www.postgresql.org/docs/8.3/static/protocol-message-formats.html
5353
var formatter = {
54-
Bind: function (portal, prepared_name, args) {
54+
Bind: function (portal, prepared_name, params, args) {
5555
var builder = (encoder('B'))
5656
.push.cstring(portal)
5757
.push.cstring(prepared_name)
58-
.push.int16(0) // 0 format codes. We don't want to declare types.
59-
.push.int16(args.length);
58+
.push.int16(args.length); // declare our format codes as expected.
59+
6060
for (var i = 0; i < args.length; i++) {
6161
switch (typeof args[i]) {
6262
case "number":
63-
builder.push.int32(4); // 4 bytes. int32.
64-
builder.push.int32(args[i]);
63+
case "boolean":
64+
case "object":
65+
builder.push.int16(1); // binary
6566
break;
6667
case "string":
67-
builder.push.int32(args[i].length);
68-
builder.push.string(args[i]); // Not a cstring. Don't \0
68+
builder.push.int16(0);
69+
break;
70+
}
71+
}
72+
73+
builder.push.int16(args.length);
74+
for (var i = 0; i < args.length; i++) {
75+
switch (typeof args[i]) {
76+
case "number":
77+
builder.push.int32(4) // 4 bytes. int32.
78+
.push.int32(args[i]);
79+
break;
80+
case "string":
81+
builder.push.int32(args[i].length)
82+
.push.string(args[i]); // Not a cstring. Don't \0
6983
break;
7084
case "boolean":
71-
builder.push.int32(1); // One byte.
72-
builder.push.string(args[i] ? 1 : 0);
85+
builder.push.int32(1) // One byte.
86+
.push.string(args[i] ? 1 : 0);
7387
break;
74-
}
88+
case "object":
89+
if (args[i] === null) {
90+
builder.push.int32(-1);
91+
}
92+
};
7593
}
7694
builder.push.int16(0); // They should all use text. Don't declare return
7795
// types, as we already have the types from the
@@ -193,7 +211,7 @@ function parse_response(code, buffer) {
193211
for (var i = 0; i < num_fields; i++) {
194212
data.push(reader.int32());
195213
}
196-
args = data;
214+
args = [data];
197215
break;
198216
case 'S':
199217
type = "ParameterStatus";
@@ -409,11 +427,11 @@ function Prepared(sql, conn, callback) {
409427
if (typeof(arguments[1]) == 'function') {
410428
callback = arguments[1];
411429
}
412-
eP = new execPrepared(portal, prepared_name, arguments[0], arguments[1]);
430+
eP = new execPrepared(portal, prepared_name, q.param_description, arguments[0], q.row_description, conn, arguments[1]);
413431
}
414432
}
415433
else if (typeof(arguments[0]) == 'function' ) {
416-
eP = new execPrepared(portal, prepared_name, [], arguments[0]);
434+
eP = new execPrepared(portal, prepared_name, q.param_description, [], q.row_description, conn, arguments[0]);
417435
// Announcing that my slot is released in favour of this new query.
418436
// The connection doesn't advance the buffer, but *does* replace
419437
// the current query with this one.
@@ -437,20 +455,25 @@ function Prepared(sql, conn, callback) {
437455
}
438456
Prepared.prototype = new process.EventEmitter;
439457

440-
function execPrepared (portal, prepared, args, callback) {
458+
function execPrepared (portal, prepared, params, args, row_desc, conn, callback) {
441459
var q = this;
460+
q.row_description = row_desc;
442461
var results = [];
443462
var arr = [
444463
{
445464
type: "Execute",
446465
args: [portal, 0], // No limit. Get all the rows.
466+
},
467+
{
468+
type: "Flush",
469+
args: [portal, 0]
447470
}
448471
];
449472
// If we have args, unshift the Bind.
450473
if (args instanceof Array && args.length >= 1) {
451474
arr.unshift({
452475
type: "Bind",
453-
args:[portal, prepared, args],
476+
args:[portal, prepared, params, args],
454477
callback: callback
455478
});
456479
}
@@ -473,13 +496,20 @@ function execPrepared (portal, prepared, args, callback) {
473496
// If it was a SELECT, args will be an array of rows,
474497
// If it was an INSERT, etc, it'll be the type, and the number of
475498
// affected rows.
499+
if (exports.DEBUG > 0) {
500+
sys.debug("Results length " + results.length);
501+
}
476502
if (results.length >= 1) {
477-
callback.call(data, results);
503+
if (exports.DEBUG > 0) {
504+
sys.debug("Calling with results");
505+
}
506+
callback.call(q, results);
478507
}
479508
else {
480509
this.type = data.type;
481-
callback.call(data);
510+
callback.call(q);
482511
}
512+
conn.release(q);
483513
});
484514
var pos = 0;
485515
q.next = function() {
@@ -717,7 +747,11 @@ function Connection(args) {
717747
}
718748
row[description.field] = value;
719749
}
720-
if (current_query.listeners("newRow") > 0) {
750+
if (exports.DEBUG > 0) {
751+
sys.debug(current_query.listeners("newRow").length);
752+
sys.debug(current_query);
753+
}
754+
if (current_query.listeners("newRow").length > 0) {
721755
current_query.emit("newRow", row);
722756
}
723757
else {

0 commit comments

Comments
 (0)