diff --git a/lib/query.js b/lib/query.js index 65ffa44b9..1a3e90196 100644 --- a/lib/query.js +++ b/lib/query.js @@ -21,7 +21,7 @@ var Query = function(config, values, callback) { //use unique portal name each time this.portal = config.portal || ""; this.callback = config.callback; - this._fieldNames = []; + this.rowtype = config.rowtype; this._fieldConverters = []; this._result = new Result(); this.isPreparedStatement = false; @@ -56,30 +56,41 @@ var noParse = function(val) { //message with this query object //metadata used when parsing row results p.handleRowDescription = function(msg) { - this._fieldNames = []; this._fieldConverters = []; var len = msg.fields.length; for(var i = 0; i < len; i++) { var field = msg.fields[i]; var format = field.format; - this._fieldNames[i] = field.name; + this._result.fieldNames[i] = field.name; this._fieldConverters[i] = Types.getTypeParser(field.dataTypeID, format); } }; p.handleDataRow = function(msg) { - var self = this; - var row = {}; - for(var i = 0; i < msg.fields.length; i++) { - var rawValue = msg.fields[i]; - if(rawValue === null) { - //leave null values alone - row[self._fieldNames[i]] = null; - } else { - //convert value to javascript - row[self._fieldNames[i]] = self._fieldConverters[i](rawValue); + var self = this, row, rawValue, + fieldNames = this._result.fieldNames, + i, c = msg.fields.length; + + if (this.rowtype === 'array') { + row = new Array(c); + for(i = 0; i < c; i++) { + rawValue = msg.fields[i]; + row[i] = (rawValue !== null) ? self._fieldConverters[i](rawValue) : null; + } + } else { + row = {}; + for(i = 0; i < c; i++) { + rawValue = msg.fields[i]; + if(rawValue === null) { + //leave null values alone + row[fieldNames[i]] = null; + } else { + //convert value to javascript + row[fieldNames[i]] = self._fieldConverters[i](rawValue); + } } } + self.emit('row', row, self._result); //if there is a callback collect rows diff --git a/lib/result.js b/lib/result.js index 688209335..8149aa9dc 100644 --- a/lib/result.js +++ b/lib/result.js @@ -5,6 +5,7 @@ var Result = function() { this.command = null; this.rowCount = null; this.oid = null; + this.fieldNames = []; this.rows = []; }; diff --git a/test/integration/client/simple-query-tests.js b/test/integration/client/simple-query-tests.js index f8ef1adad..a504fa207 100644 --- a/test/integration/client/simple-query-tests.js +++ b/test/integration/client/simple-query-tests.js @@ -63,3 +63,17 @@ test("multiple select statements", function() { }); client.on('drain', client.end.bind(client)); }); + +test("array rows", function() { + var client = helper.client(); + client.query("create temp table t(a integer, b varchar(5)); insert into t values (1, 'one'), (2, 'two');"); + client.query({ + text: "select a,b from t order by a", + rowtype: "array" + }, function(error, result) { + assert.deepEqual(result.fieldNames, ["a", "b"]); + assert.deepEqual(result.rows, [[1,"one"], [2,"two"]]); + client.end(); + }); +}); +