diff --git a/lib/native/query.js b/lib/native/query.js index 38fb2fd..905d1f7 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -21,7 +21,7 @@ var NativeQuery = function(config, values, callback) { this.values = c.values; this.callback = c.callback; - this._result = new Result(); + this._result = new Result(config.rowMode); this._addedFields = false; //normalize values if(this.values) { diff --git a/lib/query.js b/lib/query.js index cce3930..44ecee9 100644 --- a/lib/query.js +++ b/lib/query.js @@ -23,7 +23,7 @@ var Query = function(config, values, callback) { this.callback = config.callback; this._fieldNames = []; this._fieldConverters = []; - this._result = new Result(); + this._result = new Result(config.rowMode); this.isPreparedStatement = false; this._canceledDueToError = false; EventEmitter.call(this); diff --git a/lib/result.js b/lib/result.js index d638305..2e4feec 100644 --- a/lib/result.js +++ b/lib/result.js @@ -3,13 +3,16 @@ var types = require(__dirname + '/types/'); //result object returned from query //in the 'end' event and also //passed as second argument to provided callback -var Result = function() { +var Result = function(rowMode) { this.command = null; this.rowCount = null; this.oid = null; this.rows = []; this.fields = []; this._parsers = []; + if(rowMode == "array") { + this.parseRow = this._parseRowAsArray; + } }; var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/; @@ -37,6 +40,19 @@ Result.prototype.addCommandComplete = function(msg) { } }; +Result.prototype._parseRowAsArray = function(rowData) { + var row = []; + for(var i = 0, len = rowData.length; i < len; i++) { + var rawValue = rowData[i]; + if(rawValue !== null) { + row.push(this._parsers[i](rawValue)); + } else { + row.push(null); + } + } + return row; +}; + //rowData is an array of text or binary values //this turns the row into a JavaScript object Result.prototype.parseRow = function(rowData) { diff --git a/test/integration/client/results-as-array-tests.js b/test/integration/client/results-as-array-tests.js index e8eb58d..ef11a89 100644 --- a/test/integration/client/results-as-array-tests.js +++ b/test/integration/client/results-as-array-tests.js @@ -9,10 +9,15 @@ test('returns results as array', function() { var client = new Client(conInfo); var checkRow = function(row) { assert(util.isArray(row), 'row should be an array'); + assert.equal(row.length, 4); + assert.equal(row[0].getFullYear(), new Date().getFullYear()); + assert.strictEqual(row[1], 1); + assert.strictEqual(row[2], 'hai'); + assert.strictEqual(row[3], null); } client.connect(assert.success(function() { var config = { - text: 'SELECT NOW(), 1::int, $1::text', + text: 'SELECT NOW(), 1::int, $1::text, null', values: ['hai'], rowMode: 'array' };