Merge branch 'master' of github.com:brianc/node-postgres

This commit is contained in:
Brian M. Carlson 2014-10-20 00:21:28 -04:00
commit 11c3c74994
4 changed files with 64 additions and 6 deletions

View File

@ -119,7 +119,6 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) {
this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode); this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode);
} else { } else {
this._namedQuery = true; this._namedQuery = true;
this._namedQueries[query.name] = true;
this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode); this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode);
} }
} else if(query.values) { } else if(query.values) {
@ -200,6 +199,7 @@ var clientBuilder = function(config) {
var q = this._activeQuery; var q = this._activeQuery;
//a named query finished being prepared //a named query finished being prepared
if(this._namedQuery) { if(this._namedQuery) {
this._namedQueries[q.name] = true;
this._namedQuery = false; this._namedQuery = false;
this._sendQueryPrepared(q.name, q.values||[]); this._sendQueryPrepared(q.name, q.values||[]);
} else { } else {

View File

@ -67,6 +67,9 @@ Query.prototype.handleDataRow = function(msg) {
}; };
Query.prototype.handleCommandComplete = function(msg, con) { Query.prototype.handleCommandComplete = function(msg, con) {
if(this.name) {
con.parsedStatements[this.name] = true;
}
this._result.addCommandComplete(msg); this._result.addCommandComplete(msg);
//need to sync after each command complete of a prepared statement //need to sync after each command complete of a prepared statement
if(this.isPreparedStatement) { if(this.isPreparedStatement) {
@ -137,9 +140,6 @@ Query.prototype.prepare = function(connection) {
name: self.name, name: self.name,
types: self.types types: self.types
}, true); }, true);
if(this.name) {
connection.parsedStatements[this.name] = true;
}
} }
//TODO is there some better way to prepare values for the database? //TODO is there some better way to prepare values for the database?

View File

@ -0,0 +1,58 @@
var async = require('async');
var helper = require('../test-helper');
var db = helper.client();
function createTableFoo(callback){
db.query("create temp table foo(column1 int, column2 int)", callback);
}
function createTableBar(callback){
db.query("create temp table bar(column1 text, column2 text)", callback);
}
function insertDataFoo(callback){
db.query({
name: 'insertFoo',
text: 'insert into foo values($1,$2)',
values:['one','two']
}, callback );
}
function insertDataBar(callback){
db.query({
name: 'insertBar',
text: 'insert into bar values($1,$2)',
values:['one','two']
}, callback );
}
function startTransaction(callback) {
db.query('BEGIN', callback);
}
function endTransaction(callback) {
db.query('COMMIT', callback);
}
function doTransaction(callback) {
// The transaction runs startTransaction, then all queries, then endTransaction,
// no matter if there has been an error in a query in the middle.
startTransaction(function() {
insertDataFoo(function() {
insertDataBar(function() {
endTransaction( callback );
});
});
});
}
var steps = [
createTableFoo,
createTableBar,
doTransaction,
insertDataBar
]
async.series(steps, assert.success(function() {
db.end()
}))

View File

@ -101,7 +101,7 @@ assert.success = function(callback) {
if(err) { if(err) {
console.log(err); console.log(err);
} }
assert.isNull(err); assert(!err);
callback(arg); callback(arg);
}); });
} else if (callback.length === 2) { } else if (callback.length === 2) {
@ -109,7 +109,7 @@ assert.success = function(callback) {
if(err) { if(err) {
console.log(err); console.log(err);
} }
assert.isNull(err); assert(!err);
callback(arg1, arg2); callback(arg1, arg2);
}); });
} else { } else {