Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Result = function() {
this.command = null;
this.rowCount = null;
this.oid = null;
this.fieldNames = [];
this.rows = [];
};

Expand Down
14 changes: 14 additions & 0 deletions test/integration/client/simple-query-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});