mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
full support for bind and execute
This commit is contained in:
parent
c176489348
commit
bcfbb535b8
@ -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() {
|
||||
|
||||
@ -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++) {
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user