From 886926a777c3ce34bfdb8499e32f6155e499723d Mon Sep 17 00:00:00 2001 From: bmc Date: Sun, 9 Sep 2012 21:13:36 -0500 Subject: [PATCH] pass result object to native query 'row' event - closes #183 --- lib/native/index.js | 8 +++----- lib/native/query.js | 25 +++++++++++++++---------- lib/result.js | 4 +++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/native/index.js b/lib/native/index.js index bf2aa0f8..8522cd23 100644 --- a/lib/native/index.js +++ b/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) diff --git a/lib/native/query.js b/lib/native/query.js index 3d515d7b..db94b29b 100644 --- a/lib/native/query.js +++ b/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'); }; diff --git a/lib/result.js b/lib/result.js index f46ed418..3bc5cde5 100644 --- a/lib/result.js +++ b/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