diff --git a/lib/native/index.js b/lib/native/index.js index 1e1d446..dd16ea8 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -119,7 +119,6 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) { this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode); } else { this._namedQuery = true; - this._namedQueries[query.name] = true; this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode); } } else if(query.values) { @@ -200,6 +199,7 @@ var clientBuilder = function(config) { var q = this._activeQuery; //a named query finished being prepared if(this._namedQuery) { + this._namedQueries[q.name] = true; this._namedQuery = false; this._sendQueryPrepared(q.name, q.values||[]); } else { diff --git a/lib/query.js b/lib/query.js index 0c6bbc7..841e9a4 100644 --- a/lib/query.js +++ b/lib/query.js @@ -67,6 +67,9 @@ Query.prototype.handleDataRow = function(msg) { }; Query.prototype.handleCommandComplete = function(msg, con) { + if(this.name) { + con.parsedStatements[this.name] = true; + } this._result.addCommandComplete(msg); //need to sync after each command complete of a prepared statement if(this.isPreparedStatement) { @@ -137,9 +140,6 @@ Query.prototype.prepare = function(connection) { name: self.name, types: self.types }, true); - if(this.name) { - connection.parsedStatements[this.name] = true; - } } //TODO is there some better way to prepare values for the database? diff --git a/test/integration/gh-issues/600-tests.js b/test/integration/gh-issues/600-tests.js new file mode 100644 index 0000000..faf8fd8 --- /dev/null +++ b/test/integration/gh-issues/600-tests.js @@ -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() +})) diff --git a/test/test-helper.js b/test/test-helper.js index acd092b..cb7e606 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -101,7 +101,7 @@ assert.success = function(callback) { if(err) { console.log(err); } - assert.isNull(err); + assert(!err); callback(arg); }); } else if (callback.length === 2) { @@ -109,7 +109,7 @@ assert.success = function(callback) { if(err) { console.log(err); } - assert.isNull(err); + assert(!err); callback(arg1, arg2); }); } else {