Skip to content

Commit 4662d41

Browse files
committed
bind Buffer variables as binary values
1 parent 39e0483 commit 4662d41

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

lib/connection.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,27 @@ Connection.prototype.bind = function(config, more) {
209209
config.binary = config.binary || false;
210210
var values = config.values || [];
211211
var len = values.length;
212+
var useBinary = false;
213+
for (var j = 0; j < len; j++)
214+
useBinary |= values[j] instanceof Buffer;
212215
var buffer = this.writer
213216
.addCString(config.portal)
214-
.addCString(config.statement)
215-
.addInt16(0) //always use default text format
216-
.addInt16(len); //number of parameters
217+
.addCString(config.statement);
218+
if (!useBinary)
219+
buffer.addInt16(0);
220+
else {
221+
buffer.addInt16(len);
222+
for (j = 0; j < len; j++)
223+
buffer.addInt16(values[j] instanceof Buffer);
224+
}
225+
buffer.addInt16(len);
217226
for(var i = 0; i < len; i++) {
218227
var val = values[i];
219228
if(val === null || typeof val === "undefined") {
220229
buffer.addInt32(-1);
230+
} else if (val instanceof Buffer) {
231+
buffer.addInt32(val.length);
232+
buffer.add(val);
221233
} else {
222234
buffer.addInt32(Buffer.byteLength(val));
223235
buffer.addString(val);

lib/utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ function arrayString(val) {
4646
//note: you can override this function to provide your own conversion mechanism
4747
//for complex types, etc...
4848
var prepareValue = function(val) {
49+
if (val instanceof Buffer) {
50+
return val;
51+
}
4952
if(val instanceof Date) {
5053
return dateToString(val);
5154
}

test/unit/connection/outbound-sending-tests.js

+26
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ test('bind messages', function() {
116116
});
117117
});
118118

119+
test('with named statement, portal, and buffer value', function() {
120+
con.bind({
121+
portal: 'bang',
122+
statement: 'woo',
123+
values: ['1', 'hi', null, new Buffer('zing', 'UTF-8')]
124+
});
125+
var expectedBuffer = new BufferList()
126+
.addCString('bang') //portal name
127+
.addCString('woo') //statement name
128+
.addInt16(4)//value count
129+
.addInt16(0)//string
130+
.addInt16(0)//string
131+
.addInt16(0)//string
132+
.addInt16(1)//binary
133+
.addInt16(4)
134+
.addInt32(1)
135+
.add(Buffer("1"))
136+
.addInt32(2)
137+
.add(Buffer("hi"))
138+
.addInt32(-1)
139+
.addInt32(4)
140+
.add(new Buffer('zing', 'UTF-8'))
141+
.addInt16(0)
142+
.join(true, 'B');
143+
assert.received(stream, expectedBuffer);
144+
});
119145

120146
test("sends execute message", function() {
121147

0 commit comments

Comments
 (0)