diff --git a/lib/query.js b/lib/query.js index c1b5ee5ed..1c177b02d 100644 --- a/lib/query.js +++ b/lib/query.js @@ -18,6 +18,8 @@ var Query = function(config, values, callback) { this.name = config.name; this.binary = config.binary; this.stream = config.stream; + this.asArray = config.asArray; + this.uniqueFields = config.uniqueFields; //use unique portal name each time this.portal = config.portal || ""; this.callback = config.callback; @@ -61,28 +63,42 @@ Query.prototype.handleRowDescription = function(msg) { for(var i = 0; i < len; i++) { var field = msg.fields[i]; var format = field.format; - this._fieldNames[i] = field.name; + var dupecount = 0; + if(this.uniqueFields) { + this._fieldNames.forEach(function(val) {if (new RegExp("^"+field.name+"(\\$\\d+)?$").test(val)) { dupecount++;} }); + } + this._fieldNames[i] = field.name+(dupecount?'$'+dupecount:'') ; this._fieldConverters[i] = Types.getTypeParser(field.dataTypeID, format); } + if(this.callback) { + this._result.addFieldNames(this._fieldNames); + } }; Query.prototype.handleDataRow = function(msg) { var self = this; + var row = {}; + if (this.asArray) { + row = []; + } for(var i = 0; i < msg.fields.length; i++) { var rawValue = msg.fields[i]; + var parsedValue; if(rawValue === null) { //leave null values alone - row[self._fieldNames[i]] = null; + parsedValue = null; } else { //convert value to javascript - row[self._fieldNames[i]] = self._fieldConverters[i](rawValue); + parsedValue = self._fieldConverters[i](rawValue); } + row[(this.asArray?i:self._fieldNames[i])] = parsedValue; } self.emit('row', row, self._result); //if there is a callback collect rows if(self.callback) { + self._result.addRow(row); } }; diff --git a/lib/result.js b/lib/result.js index 4eabbe7f4..b789ae549 100644 --- a/lib/result.js +++ b/lib/result.js @@ -32,6 +32,9 @@ Result.prototype.addCommandComplete = function(msg) { } } }; +Result.prototype.addFieldNames = function(fieldnames) { + this.fieldnames = fieldnames; +}; Result.prototype.addRow = function(row) { this.rows.push(row);