Skip to content

Commit 102a069

Browse files
committed
have native bindings emit proper result object on 'end' event - closes brianc#219
1 parent 99f1717 commit 102a069

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

lib/native/query.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var NativeQuery = function(text, values, callback) {
3434
this.callback = values;
3535
}
3636
}
37-
this.result = new Result();
37+
this._result = new Result();
3838
//normalize values
3939
if(this.values) {
4040
for(var i = 0, len = this.values.length; i < len; i++) {
@@ -59,9 +59,9 @@ var mapRowData = function(row) {
5959
p.handleRow = function(rowData) {
6060
var row = mapRowData(rowData);
6161
if(this.callback) {
62-
this.result.addRow(row);
62+
this._result.addRow(row);
6363
}
64-
this.emit('row', row, this.result);
64+
this.emit('row', row, this._result);
6565
};
6666

6767
p.handleError = function(error) {
@@ -74,12 +74,13 @@ p.handleError = function(error) {
7474
}
7575

7676
p.handleReadyForQuery = function(meta) {
77+
if(meta) {
78+
this._result.addCommandComplete(meta);
79+
}
7780
if(this.callback) {
78-
this.result.command = meta.command.split(' ')[0];
79-
this.result.rowCount = parseInt(meta.value);
80-
this.callback(null, this.result);
81+
this.callback(null, this._result);
8182
}
82-
this.emit('end');
83+
this.emit('end', this._result);
8384
};
8485

8586
module.exports = NativeQuery;

lib/result.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/
1414

1515
//adds a command complete message
1616
p.addCommandComplete = function(msg) {
17-
var match = matchRegexp.exec(msg.text);
17+
if(msg.text) {
18+
//pure javascript
19+
var match = matchRegexp.exec(msg.text);
20+
} else {
21+
//native bindings
22+
var match = matchRegexp.exec(msg.command);
23+
}
1824
if(match) {
1925
this.command = match[1];
2026
//match 3 will only be existing on insert commands
2127
if(match[3]) {
22-
this.rowCount = parseInt(match[3]);
28+
//msg.value is from native bindings
29+
this.rowCount = parseInt(match[3] || msg.value);
2330
this.oid = parseInt(match[2]);
2431
} else {
2532
this.rowCount = parseInt(match[2]);

test/integration/client/result-metadata-tests.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@ var pg = helper.pg;
44
test('should return insert metadata', function() {
55
pg.connect(helper.config, assert.calls(function(err, client) {
66
assert.isNull(err);
7+
78
client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) {
89
assert.isNull(err);
910
assert.equal(result.oid, null);
1011
assert.equal(result.command, 'CREATE');
11-
client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) {
12+
13+
var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) {
1214
assert.equal(result.command, "INSERT");
1315
assert.equal(result.rowCount, 1);
16+
1417
client.query('SELECT * FROM zugzug', assert.calls(function(err, result) {
1518
assert.isNull(err);
1619
assert.equal(result.rowCount, 1);
1720
assert.equal(result.command, 'SELECT');
1821
process.nextTick(pg.end.bind(pg));
19-
}))
20-
}))
21-
}))
22-
}))
23-
})
22+
}));
23+
}));
24+
25+
assert.emits(q, 'end', function(result) {
26+
assert.equal(result.command, "INSERT");
27+
assert.equal(result.rowCount, 1);
28+
});
29+
30+
}));
31+
}));
32+
});

0 commit comments

Comments
 (0)