Skip to content

Commit 13fe013

Browse files
author
Aurynn Shaw
committed
Cleanup of readme. Adding license to readme.
Modification of the execute stage of prepared statements to allow multiple bound portals on the same prepared query.
1 parent 98cfd12 commit 13fe013

File tree

3 files changed

+18
-38
lines changed

3 files changed

+18
-38
lines changed

README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
This library is a implementation of the PostgreSQL backend/frontend protocol in javascript.
44
It uses the node.js tcp and event libraries. A javascript md5 library is included for servers that require md5 password hashing (this is default).
55

6-
This library allows for the handling of prepared queries.
7-
8-
Parameterized queries are (currently) no longer supported.
6+
This library allows for the correct handling of prepared queries.
97

108
If you wish to nest DB calls, db.close must be in the deepest callback, or all statements that occur inside of callbacks deeper than the callback which handles db.close will not be executed.
119

10+
All code is available under the terms of the MIT license, unless otherwise noted (md5.js)
11+
1212
## Example use
1313

1414
var sys = require("sys");
15-
var Postgres = require("postgres");
15+
var pg = require("postgres");
1616

17-
var db = new Postgres.Connection("database", "username", "password");
17+
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
1818
db.query("SELECT * FROM sometable", function (data) {
1919
sys.p(data);
2020
});
@@ -25,9 +25,8 @@ If you wish to nest DB calls, db.close must be in the deepest callback, or all s
2525
var sys = require("sys");
2626
var pg = require("postgres");
2727

28-
var db = new pg.Connection("database", "username", "password");
28+
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
2929
db.query("SELECT * FROM yourtable WHERE id = ?", [1], function (data) {
30-
3130
sys.p(data);
3231
});
3332
db.close();
@@ -37,14 +36,11 @@ If you wish to nest DB calls, db.close must be in the deepest callback, or all s
3736
var sys = require("sys");
3837
var pg = require("postgres");
3938

40-
var db = new pg.Connection("database", "username", "password");
39+
var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
4140

4241
var stmt = db.prepare("SELECT * FROM yourtable WHERE id = ?");
4342

44-
stmt.execute([1]).addCallback(function (d) {
45-
43+
stmt.execute([1], function (d) {
4644
sys.p(d);
4745
db.close();
48-
});
49-
50-
46+
});

demo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ db.prepare("SELECT ?::int", function (sth) {
1414
}
1515
db.end();
1616
});
17+
sth.execute([2], function (rs) {
18+
sys.puts(sys.inspect(rs));
19+
})
1720
});

lib/postgres-pure.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function encoder(header) {
4949
return w;
5050
}
5151

52-
// http://www.postgresql.org/docs/8.3/static/protocol-message-formats.html
52+
// http://www.postgresql.org/docs/8.4/static/protocol-message-formats.html
5353
var formatter = {
5454
Bind: function (portal, prepared_name, params, args) {
5555
var builder = (encoder('B'))
@@ -331,7 +331,6 @@ function Prepared(sql, conn, callback) {
331331

332332
var prepared_name = md5(sql); // Use the md5 hash. This is easily selectable later.
333333
var q = this;
334-
var portal = 'meh';
335334
q.row_description = null;
336335
q.param_description = null;
337336
q.noData = false;
@@ -434,11 +433,11 @@ function Prepared(sql, conn, callback) {
434433
if (typeof(arguments[1]) == 'function') {
435434
callback = arguments[1];
436435
}
437-
eP = new execPrepared(portal, prepared_name, q.param_description, arguments[0], q.row_description, conn, arguments[1]);
436+
eP = new execPrepared(prepared_name, q.param_description, arguments[0], q.row_description, conn, arguments[1]);
438437
}
439438
}
440439
else if (typeof(arguments[0]) == 'function' ) {
441-
eP = new execPrepared(portal, prepared_name, q.param_description, [], q.row_description, conn, arguments[0]);
440+
eP = new execPrepared(prepared_name, q.param_description, [], q.row_description, conn, arguments[0]);
442441
// Announcing that my slot is released in favour of this new query.
443442
// The connection doesn't advance the buffer, but *does* replace
444443
// the current query with this one.
@@ -462,8 +461,9 @@ function Prepared(sql, conn, callback) {
462461
}
463462
Prepared.prototype = new process.EventEmitter;
464463

465-
function execPrepared (portal, prepared, params, args, row_desc, conn, callback) {
464+
function execPrepared (prepared, params, args, row_desc, conn, callback) {
466465
var q = this;
466+
var portal = md5(prepared + args.join("|")); // Clearly need a generated portal name here.
467467
q.row_description = row_desc;
468468
var results = [];
469469
var arr = [
@@ -741,6 +741,7 @@ function Connection(args) {
741741
if (value !== null) {
742742
// TODO: investigate to see if these numbers are stable across databases or
743743
// if we need to dynamically pull them from the pg_types table
744+
// TODO: Investigate javascript Date objects.
744745
switch (description.type_id) {
745746
case 16: // bool
746747
value = value === 't';
@@ -919,24 +920,4 @@ function Connection(args) {
919920
}
920921
Connection.prototype = new process.EventEmitter();
921922

922-
923-
/*
924-
Connection.prototype.get_store = function (name, columns) {
925-
return new sqllib.Store(this, name, columns, {
926-
do_insert: function (data, keys, values, callback) {
927-
this.conn.query("INSERT INTO " +
928-
this.name + "(" + keys.join(", ") + ")" +
929-
" VALUES (" + values.join(", ") + ")" +
930-
" RETURNING _id",
931-
function (result) {
932-
data._id = parseInt(result[0]._id, 10);
933-
callback(data._id);
934-
}
935-
);
936-
},
937-
index_col: '_id',
938-
types: ["_id SERIAL"]
939-
});
940-
};
941-
*/
942923
exports.connect = Connection;

0 commit comments

Comments
 (0)