Support result rows as arrays

This commit is contained in:
Brian Carlson 2013-07-08 17:45:06 -05:00
parent 325a6d9153
commit 145666c1b3
4 changed files with 25 additions and 4 deletions

View File

@ -21,7 +21,7 @@ var NativeQuery = function(config, values, callback) {
this.values = c.values; this.values = c.values;
this.callback = c.callback; this.callback = c.callback;
this._result = new Result(); this._result = new Result(config.rowMode);
this._addedFields = false; this._addedFields = false;
//normalize values //normalize values
if(this.values) { if(this.values) {

View File

@ -23,7 +23,7 @@ var Query = function(config, values, callback) {
this.callback = config.callback; this.callback = config.callback;
this._fieldNames = []; this._fieldNames = [];
this._fieldConverters = []; this._fieldConverters = [];
this._result = new Result(); this._result = new Result(config.rowMode);
this.isPreparedStatement = false; this.isPreparedStatement = false;
this._canceledDueToError = false; this._canceledDueToError = false;
EventEmitter.call(this); EventEmitter.call(this);

View File

@ -3,13 +3,16 @@ var types = require(__dirname + '/types/');
//result object returned from query //result object returned from query
//in the 'end' event and also //in the 'end' event and also
//passed as second argument to provided callback //passed as second argument to provided callback
var Result = function() { var Result = function(rowMode) {
this.command = null; this.command = null;
this.rowCount = null; this.rowCount = null;
this.oid = null; this.oid = null;
this.rows = []; this.rows = [];
this.fields = []; this.fields = [];
this._parsers = []; this._parsers = [];
if(rowMode == "array") {
this.parseRow = this._parseRowAsArray;
}
}; };
var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/; var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/;
@ -37,6 +40,19 @@ Result.prototype.addCommandComplete = function(msg) {
} }
}; };
Result.prototype._parseRowAsArray = function(rowData) {
var row = [];
for(var i = 0, len = rowData.length; i < len; i++) {
var rawValue = rowData[i];
if(rawValue !== null) {
row.push(this._parsers[i](rawValue));
} else {
row.push(null);
}
}
return row;
};
//rowData is an array of text or binary values //rowData is an array of text or binary values
//this turns the row into a JavaScript object //this turns the row into a JavaScript object
Result.prototype.parseRow = function(rowData) { Result.prototype.parseRow = function(rowData) {

View File

@ -9,10 +9,15 @@ test('returns results as array', function() {
var client = new Client(conInfo); var client = new Client(conInfo);
var checkRow = function(row) { var checkRow = function(row) {
assert(util.isArray(row), 'row should be an array'); assert(util.isArray(row), 'row should be an array');
assert.equal(row.length, 4);
assert.equal(row[0].getFullYear(), new Date().getFullYear());
assert.strictEqual(row[1], 1);
assert.strictEqual(row[2], 'hai');
assert.strictEqual(row[3], null);
} }
client.connect(assert.success(function() { client.connect(assert.success(function() {
var config = { var config = {
text: 'SELECT NOW(), 1::int, $1::text', text: 'SELECT NOW(), 1::int, $1::text, null',
values: ['hai'], values: ['hai'],
rowMode: 'array' rowMode: 'array'
}; };