Skip to content
Browse files

pass result object to native query 'row' event - closes #183

  • Loading branch information...
1 parent 6640271 commit 886926a777c3ce34bfdb8499e32f6155e499723d @brianc committed
Showing with 21 additions and 16 deletions.
  1. +3 −5 lib/native/index.js
  2. +15 −10 lib/native/query.js
  3. +3 −1 lib/result.js
View
8 lib/native/index.js
@@ -133,11 +133,9 @@ var clientBuilder = function(config) {
});
connection.on('_cmdStatus', function(status) {
- var meta = {
- };
- meta.command = status.command.split(' ')[0];
- meta.rowCount = parseInt(status.value);
- connection._lastMeta = meta;
+ //set this here so we can pass it to the query
+ //when the query completes
+ connection._lastMeta = status;
});
//TODO: emit more native error properties (make it match js error)
View
25 lib/native/query.js
@@ -3,10 +3,18 @@ var util = require('util');
var types = require(__dirname + '/../types');
var utils = require(__dirname + '/../utils');
+var Result = require(__dirname + '/../result');
//event emitter proxy
var NativeQuery = function(text, values, callback) {
- //TODO there are better ways to detect overloads
+ EventEmitter.call(this);
+
+ this.text = null;
+ this.values = null;
+ this.callback = null;
+ this.name = null;
+
+ //allow 'config object' as first parameter
if(typeof text == 'object') {
this.text = text.text;
this.values = text.values;
@@ -26,17 +34,13 @@ var NativeQuery = function(text, values, callback) {
this.callback = values;
}
}
- if(this.callback) {
- this.rows = [];
- }
+ this.result = new Result();
//normalize values
if(this.values) {
for(var i = 0, len = this.values.length; i < len; i++) {
this.values[i] = utils.prepareValue(this.values[i]);
}
}
-
- EventEmitter.call(this);
};
util.inherits(NativeQuery, EventEmitter);
@@ -55,9 +59,9 @@ var mapRowData = function(row) {
p.handleRow = function(rowData) {
var row = mapRowData(rowData);
if(this.callback) {
- this.rows.push(row);
+ this.result.addRow(row);
}
- this.emit('row', row);
+ this.emit('row', row, this.result);
};
p.handleError = function(error) {
@@ -71,8 +75,9 @@ p.handleError = function(error) {
p.handleReadyForQuery = function(meta) {
if(this.callback) {
- (meta || {}).rows = this.rows;
- this.callback(null, meta);
+ this.result.command = meta.command.split(' ')[0];
+ this.result.rowCount = parseInt(meta.value);
+ this.callback(null, this.result);
}
this.emit('end');
};
View
4 lib/result.js
@@ -2,12 +2,14 @@
//in the 'end' event and also
//passed as second argument to provided callback
var Result = function() {
+ this.command = null;
+ this.rowCount = null;
+ this.oid = null;
this.rows = [];
};
var p = Result.prototype;
-
var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/
//adds a command complete message

0 comments on commit 886926a

Please sign in to comment.
Something went wrong with that request. Please try again.