From ded6c05ed6e55d9249506f0e4442fff4ff7f43e9 Mon Sep 17 00:00:00 2001 From: brianc Date: Mon, 28 Feb 2011 23:09:09 -0600 Subject: [PATCH] callback api failures --- lib/native.js | 33 +++++++++++++++++++++++++------ test/native/callback-api-tests.js | 5 ++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/native.js b/lib/native.js index 32e05bc..f18592f 100644 --- a/lib/native.js +++ b/lib/native.js @@ -46,8 +46,8 @@ p.connect = function() { }) } -p.query = function(config, values) { - var q = new NativeQuery(config, values); +p.query = function(config, values, callback) { + var q = new NativeQuery(config, values, callback); this._queryQueue.push(q); this._pulseQueryQueue(); return q; @@ -87,7 +87,7 @@ var ctor = function(config) { //proxy some events to active query connection.on('_row', function(row) { - connection._activeQuery.emit('row', row); + connection._activeQuery.handleRow(row); }) connection.on('_error', function(err) { if(connection._activeQuery) { @@ -97,7 +97,7 @@ var ctor = function(config) { } }) connection.on('_readyForQuery', function() { - connection._activeQuery.emit('end'); + connection._activeQuery.handleReadyForQuery(); connection._activeQuery = null; connection._pulseQueryQueue(); }); @@ -113,14 +113,20 @@ var connect = function(config, callback) { }; //event emitter proxy -var NativeQuery = function(text, values) { +var NativeQuery = function(text, values, callback) { if(typeof text == 'object') { this.text = text.text; this.values = text.values; } else { this.text = text; - this.values = values; + if(typeof values == 'function') { + this.callback = values; + } else { + this.callback = callback; + this.values = values; + } } + this.rows = []; EventEmitter.call(this); this._translateValues(); }; @@ -128,6 +134,21 @@ var NativeQuery = function(text, values) { sys.inherits(NativeQuery, EventEmitter); var p = NativeQuery.prototype; +p.handleRow = function(row) { + console.log('handling row'); + if(this.callback) { + this.rows.push(row); + } + this.emit('row', row); +}; + +p.handleReadyForQuery = function() { + if(this.callback) { + this.callback(null, this.rows); + } + this.emit('end'); +}; + //translates values into strings p._translateValues = function() { if(this.values) { diff --git a/test/native/callback-api-tests.js b/test/native/callback-api-tests.js index a074d92..2514060 100644 --- a/test/native/callback-api-tests.js +++ b/test/native/callback-api-tests.js @@ -4,7 +4,10 @@ var conString = helper.connectionString(); test('fires callback with results', function() { var client = new Client(conString); - client.query('SELECT 1', assert.calls(function(result) { + var q = client.query('SELECT 1', assert.calls(function(err, result) { })); + q.on('row', function(row) { + console.log(row); + }) })