Skip to content

Commit 11c3c74

Browse files
committed
Merge branch 'master' of github.com:brianc/node-postgres
2 parents 1ccc5eb + bdab97c commit 11c3c74

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

lib/native/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) {
119119
this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode);
120120
} else {
121121
this._namedQuery = true;
122-
this._namedQueries[query.name] = true;
123122
this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode);
124123
}
125124
} else if(query.values) {
@@ -200,6 +199,7 @@ var clientBuilder = function(config) {
200199
var q = this._activeQuery;
201200
//a named query finished being prepared
202201
if(this._namedQuery) {
202+
this._namedQueries[q.name] = true;
203203
this._namedQuery = false;
204204
this._sendQueryPrepared(q.name, q.values||[]);
205205
} else {

lib/query.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Query.prototype.handleDataRow = function(msg) {
6767
};
6868

6969
Query.prototype.handleCommandComplete = function(msg, con) {
70+
if(this.name) {
71+
con.parsedStatements[this.name] = true;
72+
}
7073
this._result.addCommandComplete(msg);
7174
//need to sync after each command complete of a prepared statement
7275
if(this.isPreparedStatement) {
@@ -137,9 +140,6 @@ Query.prototype.prepare = function(connection) {
137140
name: self.name,
138141
types: self.types
139142
}, true);
140-
if(this.name) {
141-
connection.parsedStatements[this.name] = true;
142-
}
143143
}
144144

145145
//TODO is there some better way to prepare values for the database?
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var async = require('async');
2+
var helper = require('../test-helper');
3+
4+
var db = helper.client();
5+
6+
function createTableFoo(callback){
7+
db.query("create temp table foo(column1 int, column2 int)", callback);
8+
}
9+
10+
function createTableBar(callback){
11+
db.query("create temp table bar(column1 text, column2 text)", callback);
12+
}
13+
14+
function insertDataFoo(callback){
15+
db.query({
16+
name: 'insertFoo',
17+
text: 'insert into foo values($1,$2)',
18+
values:['one','two']
19+
}, callback );
20+
}
21+
22+
function insertDataBar(callback){
23+
db.query({
24+
name: 'insertBar',
25+
text: 'insert into bar values($1,$2)',
26+
values:['one','two']
27+
}, callback );
28+
}
29+
30+
function startTransaction(callback) {
31+
db.query('BEGIN', callback);
32+
}
33+
function endTransaction(callback) {
34+
db.query('COMMIT', callback);
35+
}
36+
37+
function doTransaction(callback) {
38+
// The transaction runs startTransaction, then all queries, then endTransaction,
39+
// no matter if there has been an error in a query in the middle.
40+
startTransaction(function() {
41+
insertDataFoo(function() {
42+
insertDataBar(function() {
43+
endTransaction( callback );
44+
});
45+
});
46+
});
47+
}
48+
49+
var steps = [
50+
createTableFoo,
51+
createTableBar,
52+
doTransaction,
53+
insertDataBar
54+
]
55+
56+
async.series(steps, assert.success(function() {
57+
db.end()
58+
}))

test/test-helper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ assert.success = function(callback) {
101101
if(err) {
102102
console.log(err);
103103
}
104-
assert.isNull(err);
104+
assert(!err);
105105
callback(arg);
106106
});
107107
} else if (callback.length === 2) {
108108
return assert.calls(function(err, arg1, arg2) {
109109
if(err) {
110110
console.log(err);
111111
}
112-
assert.isNull(err);
112+
assert(!err);
113113
callback(arg1, arg2);
114114
});
115115
} else {

0 commit comments

Comments
 (0)