Skip to content
Browse files

Tweaks to the NULL and date handling in postgres.js

  • Loading branch information...
1 parent fdd01d2 commit 0c5293815a91de3755f201abf56ab02458a55ccc Aurynn Shaw committed Feb 1, 2011
Showing with 87 additions and 43 deletions.
  1. +26 −0 benchmark.js
  2. +49 −37 demo.js
  3. +8 −5 lib/postgres-pure.js
  4. +4 −1 package.json
View
26 benchmark.js
@@ -0,0 +1,26 @@
+var sys = require("sys");
+
+var iterations = process.argv[2];
+
+var pg = require("./lib/postgres-pure");
+var db = new pg.connect("pgsql://test:12345@localhost:5432/insert_test");
+db.transaction(function (tx) {
+ tx.query("CREATE TABLE insert_test (id serial not null, val text)", function (err, rs) {});
+ tx.begin();
+ tx.prepare("INSERT INTO insert_test (val) VALUES (?::text) RETURNING id, val", function (sth) {
+ var i = 0;
+ for (i = 0; i <= iterations; i++) {
+ sth.execute(""+i, function (err, rs) {
+ // This is the point where we have executed.
+ console.log(rs[0]);
+ })
+ }
+ });
+ tx.query("SELECT COUNT(*) FROM insert_test", function (err, rs) {
+ console.log(sys.inspect(rs));
+ });
+ tx.rollback();
+ tx.query("DROP TABLE insert_test", function () {});
+});
+console.log("Done");
+db.close();
View
86 demo.js
@@ -3,6 +3,18 @@ var pg = require("./lib/postgres-pure");
// pg.DEBUG=4;
var db = new pg.connect("pgsql://test:12345@localhost:5432/insert_test");
+
+// db.query("SELECT 1::errortest;", function (error, rs, tx) {
+// if (error) {
+// console.log("Error!");
+// console.log(error);
+// console.log(error.code);
+// }
+// else {
+// console.log(sys.inspect(rs));
+// }
+// });
+
// db.query("SELECT 1::querytest;", function (error, rs, tx) {
// if (error) {
// console.log("Error!");
@@ -32,47 +44,47 @@ var db = new pg.connect("pgsql://test:12345@localhost:5432/insert_test");
// });
// });
-// db.prepare("SELECT ?::int AS preparetest", function (sth, tx) {
-// sth.execute(1, function (err, rs) {
-// console.log(sys.inspect(rs));
-// });
-// sth.execute(2, function (err, rs) {
-// console.log(sys.inspect(rs));
-//
-// });
-// tx.prepare("SELECT ?::int AS preparetest2", function (sth) {
-// sth.execute(3, function (err, rs) {
-// console.log(sys.inspect(rs));
-// }) ;
-// });
-// tx.prepare("SELECT ?::int AS preparetest2", function (sth) {
-// sth.execute(4, function (err, rs) {
-// console.log(sys.inspect(rs));
-// }) ;
-// });
-// });
-
-db.transaction(function (tx) {
- tx.begin();
- // tx.query("INSERT INTO insert_test (val) VALUES (?) RETURNING id", "test value", function (err,rs) {
- // console.log(sys.inspect(rs));
- // });
- // tx.prepare("INSERT INTO insert_test (val) VALUES (?) RETURNING id", function (sth) {
- // sth.execute("twooooo", function (err, rs) {
- // console.log(sys.inspect(rs));
- // });
- // });
- tx.query("SELECT 1::barrrrrr", function (err, rs) {
- console.log(sys.inspect(err));
- });
- tx.query("SELECT 1::int as foobar", function (err, rs) {
- console.log(sys.inspect(err));
+db.prepare("SELECT ?::int AS preparetest", function (sth, tx) {
+ sth.execute(1, function (err, rs) {
+ console.log(sys.inspect(rs));
});
- tx.rollback();
- tx.query("SELECT 1::int as foobarbaz", function (err, rs) {
+ sth.execute(2, function (err, rs) {
console.log(sys.inspect(rs));
+
+ });
+ tx.prepare("SELECT ?::int AS preparetest2", function (sth) {
+ sth.execute(3, function (err, rs) {
+ console.log(sys.inspect(rs));
+ }) ;
+ });
+ tx.prepare("SELECT ?::int AS preparetest2", function (sth) {
+ sth.execute(4, function (err, rs) {
+ console.log(sys.inspect(rs));
+ }) ;
});
});
+
+// db.transaction(function (tx) {
+// tx.begin();
+// // tx.query("INSERT INTO insert_test (val) VALUES (?) RETURNING id", "test value", function (err,rs) {
+// // console.log(sys.inspect(rs));
+// // });
+// // tx.prepare("INSERT INTO insert_test (val) VALUES (?) RETURNING id", function (sth) {
+// // sth.execute("twooooo", function (err, rs) {
+// // console.log(sys.inspect(rs));
+// // });
+// // });
+// tx.query("SELECT 1::barrrrrr", function (err, rs) {
+// console.log(sys.inspect(err));
+// });
+// tx.query("SELECT 1::int as foobar", function (err, rs) {
+// console.log(sys.inspect(err));
+// });
+// tx.rollback();
+// tx.query("SELECT 1::int as foobarbaz", function (err, rs) {
+// console.log(sys.inspect(rs));
+// });
+// });
db.close();
// db.prepare(query, function (sth, tx) {
View
13 lib/postgres-pure.js
@@ -55,8 +55,9 @@ var formatter = {
var builder = (encoder('B'))
.push.cstring(portal)
.push.cstring(prepared_name)
- .push.int16(args.length); // declare our format codes as expected.
-
+ // .push.int16(args.length); // declare our format codes as expected.
+ .push.int16(0); // Text
+ /*
for (var i = 0; i < args.length; i++) {
switch (typeof args[i]) {
case "number":
@@ -75,13 +76,14 @@ var formatter = {
break;
}
}
+ */
builder.push.int16(args.length);
for (var i = 0; i < args.length; i++) {
switch (typeof args[i]) {
case "number":
- builder.push.int32(4) // 4 bytes. int32.
- .push.int32(args[i]);
+ builder.push.int32(args[i].toString().length) // 4 bytes. int32.
+ .push.string(args[i].toString());
break;
case "string":
builder.push.int32(args[i].length)
@@ -105,7 +107,8 @@ var formatter = {
builder.push.int32(v.length).push.string(v);
break;
}
- if (args[i] === null) {
+ if (args[i] === null || args[i] === undefined) {
+ // this isn't right...
builder.push.int32(-1);
break;
}
View
5 package.json
@@ -1,10 +1,13 @@
{"name": "postgres",
- "version": "0.1.0RC2",
+ "version": "0.1.0",
"description": "Pure-JavaScript implementation of the Postgres Protocol",
"homepage": "https://public.commandprompt.com/projects/postgres-js/",
"author": {
"name": "Aurynn Shaw",
"email": "ashaw@commandprompt.com",
},
"main": "lib/postgres-pure",
+ "dependencies" :
+ { "strtok" : ">=0.1.1"
+ }
}

0 comments on commit 0c52938

Please sign in to comment.
Something went wrong with that request. Please try again.