From bcfbb535b8529b1155b1406c35b61aac84db41c0 Mon Sep 17 00:00:00 2001 From: brianc Date: Sun, 24 Oct 2010 13:46:50 -0500 Subject: [PATCH] full support for bind and execute --- lib/connection.js | 35 ++++++--- test/test-helper.js | 6 +- .../unit/connection/outbound-sending-tests.js | 74 ++++++++++++++----- 3 files changed, 86 insertions(+), 29 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 3434a9c9..0e30af4c 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -117,23 +117,38 @@ p.parse = function(query) { p.bind = function(config) { //normalize config config = config || {}; - config.portalName = config.portalName || ''; - config.statementName = config.statementName || ''; - config.values = config.values || []; + config.portal = config.portal || ''; + config.statement = config.statement || ''; + var values = config.values || []; + var len = values.length; var buffer = new BufferList() - .addCString(config.portalName) - .addCString(config.statementName) + .addCString(config.portal) + .addCString(config.statement) .addInt16(0) //always use default text format - .addInt16(0); //number of parameters - if(config.values.length > 0) { - sys.debug("Not supporting parameters yet"); + .addInt16(len); //number of parameters + for(var i = 0; i < len; i++) { + var val = values[i]; + if(val === null) { + buffer.addInt32(-1); + } else { + val = val.toString(); + buffer.addInt32(Buffer.byteLength(val)); + buffer.add(Buffer(val,this.encoding)); + } } buffer.addInt16(0); //no format codes, use text this.send('B', buffer.join()); }; -p.execute = function(name, rows) { - this.send('E',new BufferList().addCString(name||'').addInt32(rows||0).join()); +p.execute = function(config) { + config = config || {}; + config.portal = config.portal || ''; + config.rows = config.rows || ''; + var buffer = new BufferList() + .addCString(config.portal) + .addInt32(config.rows) + .join(); + this.send('E', buffer); }; p.flush = function() { diff --git a/test/test-helper.js b/test/test-helper.js index f11d31e9..a208d5ce 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -35,8 +35,10 @@ assert.raises = function(item, eventName, callback) { assert.equalBuffers = function(actual, expected) { if(actual.length != expected.length) { - console.log(actual); - console.log(expected); + console.log(""); + console.log("actual " + sys.inspect(actual)); + console.log("expect " + sys.inspect(expected)); + console.log(""); assert.equal(actual.length, expected.length); } for(var i = 0; i < actual.length; i++) { diff --git a/test/unit/connection/outbound-sending-tests.js b/test/unit/connection/outbound-sending-tests.js index 798e6f05..0547645a 100644 --- a/test/unit/connection/outbound-sending-tests.js +++ b/test/unit/connection/outbound-sending-tests.js @@ -75,28 +75,68 @@ test('sends parse message with named query', function() { }); }); -test('sends bind to unamed statement with no values', function() { - con.bind(); +test('bind messages', function() { + test('with no values', function() { + con.bind(); - var expectedBuffer = new BufferList() - .addCString("") - .addCString("") - .addInt16(0) - .addInt16(0) - .addInt16(0).join(true,"B"); - assert.recieved(stream, expectedBuffer); + var expectedBuffer = new BufferList() + .addCString("") + .addCString("") + .addInt16(0) + .addInt16(0) + .addInt16(0) + .join(true,"B"); + assert.recieved(stream, expectedBuffer); + }); + + test('with named statement, portal, and values', function() { + con.bind({ + portal: 'bang', + statement: 'woo', + values: [1, 'hi', null, 'zing'] + }); + var expectedBuffer = new BufferList() + .addCString('bang') //portal name + .addCString('woo') //statement name + .addInt16(0) + .addInt16(4) + .addInt32(1) + .add(Buffer("1")) + .addInt32(2) + .add(Buffer("hi")) + .addInt32(-1) + .addInt32(4) + .add(Buffer('zing')) + .addInt16(0) + .join(true, 'B'); + assert.recieved(stream, expectedBuffer); + }); }); -test("sends execute message for unamed portal with no row limit", function() { - con.execute(); - var expectedBuffer = new BufferList() - .addCString('') - .addInt32(0) - .join(true,'E'); - assert.recieved(stream, expectedBuffer); -}); +test("sends execute message", function() { + test("for unamed portal with no row limit", function() { + con.execute(); + var expectedBuffer = new BufferList() + .addCString('') + .addInt32(0) + .join(true,'E'); + assert.recieved(stream, expectedBuffer); + }); + + test("for named portal with row limit", function() { + con.execute({ + portal: 'my favorite portal', + rows: 100 + }); + var expectedBuffer = new BufferList() + .addCString("my favorite portal") + .addInt32(100) + .join(true, 'E'); + assert.recieved(stream, expectedBuffer); + }); +}); test('sends flush command', function() { con.flush();