diff --git a/lib/query.js b/lib/query.js index 36d52ba70..e00cee59d 100644 --- a/lib/query.js +++ b/lib/query.js @@ -18,6 +18,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; @@ -31,7 +32,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; this._promise = null; @@ -93,6 +97,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); + } }; //if a named prepared statement is created with empty query text @@ -109,7 +118,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); }; diff --git a/test/integration/client/api-tests.js b/test/integration/client/api-tests.js index f8f78f195..b8e07c983 100644 --- a/test/integration/client/api-tests.js +++ b/test/integration/client/api-tests.js @@ -171,3 +171,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(); + })) + }) + })) +})