From c176489348fec97b456ac1eb234ed160be898315 Mon Sep 17 00:00:00 2001 From: brianc Date: Sun, 24 Oct 2010 00:18:48 -0500 Subject: [PATCH] add type IDs to parse command --- lib/connection.js | 9 ++--- script/list-db-types.js | 36 ++++++++++++------- .../unit/connection/outbound-sending-tests.js | 17 +++++++++ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 3db19316..3434a9c9 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -101,14 +101,15 @@ p.parse = function(query) { query.name = query.name || ''; //normalize null type array query.types = query.types || []; + var len = query.types.length; var buffer = new BufferList() .addCString(query.name) //name of query .addCString(query.text) //actual query text - .addInt16(0).join(); - if(query.types.length) { - sys.debug("Typed parameters not supported yet!"); + .addInt16(len); + for(var i = 0; i < len; i++) { + buffer.addInt32(query.types[i]); } - this.send('P', buffer); + this.send('P', buffer.join()); return this; }; diff --git a/script/list-db-types.js b/script/list-db-types.js index 1a395869..d7f41372 100644 --- a/script/list-db-types.js +++ b/script/list-db-types.js @@ -1,14 +1,24 @@ -var Client = require(__dirname+"/../lib/client"); -var client = new Client({ - user: 'brian', - database: 'postgres' -}); -client.connect(); -var query = client.query('select oid, typname, typlen from pg_type where typtype = \'b\' order by typname'); -query.on('row', function(row) { - console.log(row); -}); -query.on('end',function() { - client.disconnect(); -}) +var net = require('net') +var Connection = require(__dirname+'/../lib/connection'); +var con = new Connection({stream: new net.Stream()}); +con.connect('5432', 'localhost'); + +con.on('connect', function() { + con.startupMessage({ + user: 'brian', + database: 'postgres' + }); +}); + +con.on('dataRow', function(msg) { + console.log(msg.fields); +}); + +con.on('readyForQuery', function() { + con.query('select oid, typname from pg_type where typtype = \'b\' order by typname'); +}); + +con.on('commandComplete', function() { + con.end(); +}); diff --git a/test/unit/connection/outbound-sending-tests.js b/test/unit/connection/outbound-sending-tests.js index ccaffb9f..798e6f05 100644 --- a/test/unit/connection/outbound-sending-tests.js +++ b/test/unit/connection/outbound-sending-tests.js @@ -56,6 +56,23 @@ test('sends parse message with named query', function() { .addCString("select * from boom") .addInt16(0).join(true,'P'); assert.recieved(stream, expected); + + test('with multiple parameters', function() { + con.parse({ + name: 'force', + text: 'select * from bang where name = $1', + types: [1, 2, 3 ,4] + }); + var expected = new BufferList() + .addCString("force") + .addCString("select * from bang where name = $1") + .addInt16(4) + .addInt32(1) + .addInt32(2) + .addInt32(3) + .addInt32(4).join(true,'P'); + assert.recieved(stream, expected); + }); }); test('sends bind to unamed statement with no values', function() {