bind Buffer variables as binary values

This commit is contained in:
Eugene Ware 2013-09-19 01:50:42 +10:00
parent 39e048330c
commit 4662d41972
3 changed files with 44 additions and 3 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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() {