Skip to content

Commit 886926a

Browse files
committed
pass result object to native query 'row' event - closes brianc#183
1 parent 6640271 commit 886926a

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

lib/native/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ var clientBuilder = function(config) {
133133
});
134134

135135
connection.on('_cmdStatus', function(status) {
136-
var meta = {
137-
};
138-
meta.command = status.command.split(' ')[0];
139-
meta.rowCount = parseInt(status.value);
140-
connection._lastMeta = meta;
136+
//set this here so we can pass it to the query
137+
//when the query completes
138+
connection._lastMeta = status;
141139
});
142140

143141
//TODO: emit more native error properties (make it match js error)

lib/native/query.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ var util = require('util');
33

44
var types = require(__dirname + '/../types');
55
var utils = require(__dirname + '/../utils');
6+
var Result = require(__dirname + '/../result');
67

78
//event emitter proxy
89
var NativeQuery = function(text, values, callback) {
9-
//TODO there are better ways to detect overloads
10+
EventEmitter.call(this);
11+
12+
this.text = null;
13+
this.values = null;
14+
this.callback = null;
15+
this.name = null;
16+
17+
//allow 'config object' as first parameter
1018
if(typeof text == 'object') {
1119
this.text = text.text;
1220
this.values = text.values;
@@ -26,17 +34,13 @@ var NativeQuery = function(text, values, callback) {
2634
this.callback = values;
2735
}
2836
}
29-
if(this.callback) {
30-
this.rows = [];
31-
}
37+
this.result = new Result();
3238
//normalize values
3339
if(this.values) {
3440
for(var i = 0, len = this.values.length; i < len; i++) {
3541
this.values[i] = utils.prepareValue(this.values[i]);
3642
}
3743
}
38-
39-
EventEmitter.call(this);
4044
};
4145

4246
util.inherits(NativeQuery, EventEmitter);
@@ -55,9 +59,9 @@ var mapRowData = function(row) {
5559
p.handleRow = function(rowData) {
5660
var row = mapRowData(rowData);
5761
if(this.callback) {
58-
this.rows.push(row);
62+
this.result.addRow(row);
5963
}
60-
this.emit('row', row);
64+
this.emit('row', row, this.result);
6165
};
6266

6367
p.handleError = function(error) {
@@ -71,8 +75,9 @@ p.handleError = function(error) {
7175

7276
p.handleReadyForQuery = function(meta) {
7377
if(this.callback) {
74-
(meta || {}).rows = this.rows;
75-
this.callback(null, meta);
78+
this.result.command = meta.command.split(' ')[0];
79+
this.result.rowCount = parseInt(meta.value);
80+
this.callback(null, this.result);
7681
}
7782
this.emit('end');
7883
};

lib/result.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
//in the 'end' event and also
33
//passed as second argument to provided callback
44
var Result = function() {
5+
this.command = null;
6+
this.rowCount = null;
7+
this.oid = null;
58
this.rows = [];
69
};
710

811
var p = Result.prototype;
912

10-
1113
var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/
1214

1315
//adds a command complete message

0 commit comments

Comments
 (0)