From 4662d419720b149fe7384761119e909c3ceebfe6 Mon Sep 17 00:00:00 2001 From: Eugene Ware Date: Thu, 19 Sep 2013 01:50:42 +1000 Subject: [PATCH] bind Buffer variables as binary values --- lib/connection.js | 18 ++++++++++--- lib/utils.js | 3 +++ .../unit/connection/outbound-sending-tests.js | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index df3b4e8e..6f230b1c 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -209,15 +209,27 @@ Connection.prototype.bind = function(config, more) { config.binary = config.binary || false; var values = config.values || []; var len = values.length; + var useBinary = false; + for (var j = 0; j < len; j++) + useBinary |= values[j] instanceof Buffer; var buffer = this.writer .addCString(config.portal) - .addCString(config.statement) - .addInt16(0) //always use default text format - .addInt16(len); //number of parameters + .addCString(config.statement); + if (!useBinary) + buffer.addInt16(0); + else { + buffer.addInt16(len); + for (j = 0; j < len; j++) + buffer.addInt16(values[j] instanceof Buffer); + } + buffer.addInt16(len); for(var i = 0; i < len; i++) { var val = values[i]; if(val === null || typeof val === "undefined") { buffer.addInt32(-1); + } else if (val instanceof Buffer) { + buffer.addInt32(val.length); + buffer.add(val); } else { buffer.addInt32(Buffer.byteLength(val)); buffer.addString(val); diff --git a/lib/utils.js b/lib/utils.js index 3d12d827..fb1f56eb 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -46,6 +46,9 @@ function arrayString(val) { //note: you can override this function to provide your own conversion mechanism //for complex types, etc... var prepareValue = function(val) { + if (val instanceof Buffer) { + return val; + } if(val instanceof Date) { return dateToString(val); } diff --git a/test/unit/connection/outbound-sending-tests.js b/test/unit/connection/outbound-sending-tests.js index 2d5c0a04..3dde8a3c 100644 --- a/test/unit/connection/outbound-sending-tests.js +++ b/test/unit/connection/outbound-sending-tests.js @@ -116,6 +116,32 @@ test('bind messages', function() { }); }); +test('with named statement, portal, and buffer value', function() { + con.bind({ + portal: 'bang', + statement: 'woo', + values: ['1', 'hi', null, new Buffer('zing', 'UTF-8')] + }); + var expectedBuffer = new BufferList() + .addCString('bang') //portal name + .addCString('woo') //statement name + .addInt16(4)//value count + .addInt16(0)//string + .addInt16(0)//string + .addInt16(0)//string + .addInt16(1)//binary + .addInt16(4) + .addInt32(1) + .add(Buffer("1")) + .addInt32(2) + .add(Buffer("hi")) + .addInt32(-1) + .addInt32(4) + .add(new Buffer('zing', 'UTF-8')) + .addInt16(0) + .join(true, 'B'); + assert.received(stream, expectedBuffer); +}); test("sends execute message", function() {