From 46cb93c6eaa6ea077e06a87df29e74f230b030ef Mon Sep 17 00:00:00 2001 From: Luke Murray Date: Wed, 6 May 2015 20:10:42 +1000 Subject: [PATCH 1/2] feat(757) support returning an array of results if multiResult: true passed in options. --- lib/query.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/query.js b/lib/query.js index 69be6e473..039fe86ea 100644 --- a/lib/query.js +++ b/lib/query.js @@ -10,6 +10,7 @@ var Query = function(config, values, callback) { config = utils.normalizeQueryConfig(config, values, callback); + this.multiResult = config.multiResult; this.text = config.text; this.values = config.values; this.rows = config.rows; @@ -23,7 +24,10 @@ var Query = function(config, values, callback) { if(process.domain && config.callback) { this.callback = process.domain.bind(config.callback); } - this._result = new Result(config.rowMode, config.types); + this.rowMode = config.rowMode; + // the active result in muti result mode + this._result = new Result(this.rowMode); + this._resultsArray = []; this.isPreparedStatement = false; this._canceledDueToError = false; EventEmitter.call(this); @@ -70,6 +74,11 @@ Query.prototype.handleCommandComplete = function(msg, con) { if(this.isPreparedStatement) { con.sync(); } + // new result for next query + if(this.multiResult) { + this._resultsArray.push(this._result); + this._result = new Result(this.rowMode); + } }; Query.prototype.handleReadyForQuery = function() { @@ -77,7 +86,7 @@ Query.prototype.handleReadyForQuery = function() { return this.handleError(this._canceledDueToError); } if(this.callback) { - this.callback(null, this._result); + this.callback(null, this.multiResult ? this._resultsArray : this._result); } this.emit('end', this._result); }; From 9517efd764e4d37d445297b6319cee86c97eeab2 Mon Sep 17 00:00:00 2001 From: Luke Murray Date: Sat, 9 May 2015 12:31:57 +1000 Subject: [PATCH 2/2] add a couple of simple integration tests for the multiResult option --- test/integration/client/api-tests.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/integration/client/api-tests.js b/test/integration/client/api-tests.js index c3baca8f6..9f6a63f22 100644 --- a/test/integration/client/api-tests.js +++ b/test/integration/client/api-tests.js @@ -165,3 +165,26 @@ test('null and undefined are both inserted as NULL', function() { })) })) }) + +test('can configure multiResult option', function() { + pg.connect(helper.config, assert.calls(function(err, client, done) { + assert.isNull(err); + test('result is not an array by default', function() { + client.query({ text: 'select \'hi\' as val; select \'bye\' as val;' }, assert.calls(function(err, result) { + assert.isNull(err); + assert.equal(result instanceof Array, false); + done(); + })) + }) + test('result is an array when asked', function() { + client.query({ text: 'select \'hi\' as val; select \'bye\' as val;', multiResult: true }, assert.calls(function(err, results) { + assert.isNull(err); + assert.equal(results instanceof Array, true); + assert.equal(results.length, 2); + assert.equal(results[0].rows[0].val, 'hi'); + assert.equal(results[1].rows[0].val, 'bye'); + done(); + })) + }) + })) +})