Skip to content

Commit 145666c

Browse files
committed
Support result rows as arrays
1 parent 325a6d9 commit 145666c

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

lib/native/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var NativeQuery = function(config, values, callback) {
2121
this.values = c.values;
2222
this.callback = c.callback;
2323

24-
this._result = new Result();
24+
this._result = new Result(config.rowMode);
2525
this._addedFields = false;
2626
//normalize values
2727
if(this.values) {

lib/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var Query = function(config, values, callback) {
2323
this.callback = config.callback;
2424
this._fieldNames = [];
2525
this._fieldConverters = [];
26-
this._result = new Result();
26+
this._result = new Result(config.rowMode);
2727
this.isPreparedStatement = false;
2828
this._canceledDueToError = false;
2929
EventEmitter.call(this);

lib/result.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ var types = require(__dirname + '/types/');
33
//result object returned from query
44
//in the 'end' event and also
55
//passed as second argument to provided callback
6-
var Result = function() {
6+
var Result = function(rowMode) {
77
this.command = null;
88
this.rowCount = null;
99
this.oid = null;
1010
this.rows = [];
1111
this.fields = [];
1212
this._parsers = [];
13+
if(rowMode == "array") {
14+
this.parseRow = this._parseRowAsArray;
15+
}
1316
};
1417

1518
var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/;
@@ -37,6 +40,19 @@ Result.prototype.addCommandComplete = function(msg) {
3740
}
3841
};
3942

43+
Result.prototype._parseRowAsArray = function(rowData) {
44+
var row = [];
45+
for(var i = 0, len = rowData.length; i < len; i++) {
46+
var rawValue = rowData[i];
47+
if(rawValue !== null) {
48+
row.push(this._parsers[i](rawValue));
49+
} else {
50+
row.push(null);
51+
}
52+
}
53+
return row;
54+
};
55+
4056
//rowData is an array of text or binary values
4157
//this turns the row into a JavaScript object
4258
Result.prototype.parseRow = function(rowData) {

test/integration/client/results-as-array-tests.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ test('returns results as array', function() {
99
var client = new Client(conInfo);
1010
var checkRow = function(row) {
1111
assert(util.isArray(row), 'row should be an array');
12+
assert.equal(row.length, 4);
13+
assert.equal(row[0].getFullYear(), new Date().getFullYear());
14+
assert.strictEqual(row[1], 1);
15+
assert.strictEqual(row[2], 'hai');
16+
assert.strictEqual(row[3], null);
1217
}
1318
client.connect(assert.success(function() {
1419
var config = {
15-
text: 'SELECT NOW(), 1::int, $1::text',
20+
text: 'SELECT NOW(), 1::int, $1::text, null',
1621
values: ['hai'],
1722
rowMode: 'array'
1823
};

0 commit comments

Comments
 (0)