Skip to content

Commit 91c38ce

Browse files
committed
added asArray and uniqueFields options to return array instead of object and to
return unique list of field names (adding $x if fieldname is already included)
1 parent 3bfc8e9 commit 91c38ce

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/query.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var Query = function(config, values, callback) {
1818
this.name = config.name;
1919
this.binary = config.binary;
2020
this.stream = config.stream;
21+
this.asArray = config.asArray;
22+
this.uniqueFields = config.uniqueFields;
2123
//use unique portal name each time
2224
this.portal = config.portal || "";
2325
this.callback = config.callback;
@@ -61,28 +63,42 @@ Query.prototype.handleRowDescription = function(msg) {
6163
for(var i = 0; i < len; i++) {
6264
var field = msg.fields[i];
6365
var format = field.format;
64-
this._fieldNames[i] = field.name;
66+
var dupecount = 0;
67+
if(this.uniqueFields) {
68+
this._fieldNames.forEach(function(val) {if (new RegExp("^"+field.name+"(\\$\\d+)?$").test(val)) { dupecount++;} });
69+
}
70+
this._fieldNames[i] = field.name+(dupecount?'$'+dupecount:'') ;
6571
this._fieldConverters[i] = Types.getTypeParser(field.dataTypeID, format);
6672
}
73+
if(this.callback) {
74+
this._result.addFieldNames(this._fieldNames);
75+
}
6776
};
6877

6978
Query.prototype.handleDataRow = function(msg) {
7079
var self = this;
80+
7181
var row = {};
82+
if (this.asArray) {
83+
row = [];
84+
}
7285
for(var i = 0; i < msg.fields.length; i++) {
7386
var rawValue = msg.fields[i];
87+
var parsedValue;
7488
if(rawValue === null) {
7589
//leave null values alone
76-
row[self._fieldNames[i]] = null;
90+
parsedValue = null;
7791
} else {
7892
//convert value to javascript
79-
row[self._fieldNames[i]] = self._fieldConverters[i](rawValue);
93+
parsedValue = self._fieldConverters[i](rawValue);
8094
}
95+
row[(this.asArray?i:self._fieldNames[i])] = parsedValue;
8196
}
8297
self.emit('row', row, self._result);
8398

8499
//if there is a callback collect rows
85100
if(self.callback) {
101+
86102
self._result.addRow(row);
87103
}
88104
};

lib/result.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Result.prototype.addCommandComplete = function(msg) {
3232
}
3333
}
3434
};
35+
Result.prototype.addFieldNames = function(fieldnames) {
36+
this.fieldnames = fieldnames;
37+
};
3538

3639
Result.prototype.addRow = function(row) {
3740
this.rows.push(row);

0 commit comments

Comments
 (0)