mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
simple prepared statements working!
This commit is contained in:
parent
35d2b2e4ad
commit
104727fd87
@ -105,14 +105,14 @@ var Query = function(config) {
|
||||
sys.inherits(Query, EventEmitter);
|
||||
var p = Query.prototype;
|
||||
|
||||
p.isBoundCommand = function() {
|
||||
p.requiresPreparation = function() {
|
||||
return (this.values || 0).length > 0 || this.name || this.rows;
|
||||
};
|
||||
|
||||
p.submit = function(connection) {
|
||||
var self = this;
|
||||
if(this.isBoundCommand()) {
|
||||
throw new Error('Bound command not supported yet! :(');
|
||||
if(this.requiresPreparation()) {
|
||||
this.prepare(connection);
|
||||
} else {
|
||||
connection.query(this.text);
|
||||
}
|
||||
@ -132,6 +132,52 @@ p.submit = function(connection) {
|
||||
});
|
||||
};
|
||||
|
||||
p.prepare = function(connection) {
|
||||
var self = this;
|
||||
|
||||
connection.parse({
|
||||
text: self.text,
|
||||
name: self.name,
|
||||
types: self.types
|
||||
});
|
||||
connection.flush();
|
||||
|
||||
var onParseComplete = function() {
|
||||
connection.bind({
|
||||
portal: self.name,
|
||||
statement: self.name,
|
||||
values: self.values
|
||||
});
|
||||
connection.flush();
|
||||
};
|
||||
|
||||
connection.once('parseComplete', onParseComplete);
|
||||
|
||||
var onBindComplete = function() {
|
||||
connection.describe({
|
||||
type: 'P',
|
||||
name: self.name || ""
|
||||
});
|
||||
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
|
||||
//TODO get ourselves a rowDescription for result type coercion
|
||||
connection.execute({
|
||||
portal: self.name,
|
||||
rows: self.rows
|
||||
});
|
||||
connection.flush();
|
||||
};
|
||||
|
||||
connection.once('bindComplete', onBindComplete);
|
||||
|
||||
//TODO support EmptyQueryResponse, ErrorResponse, and PortalSuspended
|
||||
var onCommandComplete = function() {
|
||||
connection.sync();
|
||||
};
|
||||
connection.once('commandComplete', onCommandComplete);
|
||||
|
||||
};
|
||||
|
||||
|
||||
p.onRowDescription = function(msg) {
|
||||
var typeIds = msg.fields.map(function(field) {
|
||||
return field.dataTypeID;
|
||||
|
||||
@ -165,7 +165,9 @@ p.end = function() {
|
||||
};
|
||||
|
||||
p.describe = function(msg) {
|
||||
this.send('D',Buffer(msg.type+msg.name+'\0'));
|
||||
var str = msg.type + (msg.name || "" ) + '\0';
|
||||
var buffer = Buffer(str, this.encoding);
|
||||
this.send('D', buffer);
|
||||
};
|
||||
|
||||
//parsing methods
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
var helper = require(__dirname +'/test-helper');
|
||||
|
||||
test("simple prepared statement", function(){
|
||||
var client = helper.client();
|
||||
client.connection.on('message', function(msg) {
|
||||
console.log(msg.name);
|
||||
});
|
||||
var query = client.query({
|
||||
text: 'select age from person where name = $1',
|
||||
values: ['Brian']
|
||||
});
|
||||
|
||||
assert.raises(query, 'row', function(row) {
|
||||
assert.equal(row.fields[0], 20);
|
||||
});
|
||||
|
||||
assert.raises(query, 'end', function() {
|
||||
client.end();
|
||||
});
|
||||
});
|
||||
@ -158,8 +158,14 @@ test('sends end command', function() {
|
||||
|
||||
test('sends describe command',function() {
|
||||
test('describe statement', function() {
|
||||
con.describe({type: 's', name: 'bang'});
|
||||
var expected = new BufferList().addChar('s').addCString('bang').join(true, 'D')
|
||||
con.describe({type: 'S', name: 'bang'});
|
||||
var expected = new BufferList().addChar('S').addCString('bang').join(true, 'D')
|
||||
assert.recieved(stream, expected);
|
||||
});
|
||||
|
||||
test("describe unnamed portal", function() {
|
||||
con.describe({type: 'P'});
|
||||
var expected = new BufferList().addChar('P').addCString("").join(true, "D");
|
||||
assert.recieved(stream, expected);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user