Move row parsing into result object

This commit is contained in:
Brian Carlson 2013-07-08 09:30:10 -05:00
parent 3f96bbbc5c
commit 413eff72e5
3 changed files with 38 additions and 59 deletions

View File

@ -34,41 +34,12 @@ var NativeQuery = function(config, values, callback) {
util.inherits(NativeQuery, EventEmitter); util.inherits(NativeQuery, EventEmitter);
//maps from native rowdata into api compatible row object
var mapRowData = function(row) {
var result = {};
for(var i = 0, len = row.length; i < len; i++) {
var item = row[i];
result[item.name] = item.value === null ? null :
types.getTypeParser(item.dataTypeID, 'text')(item.value);
}
return result;
};
NativeQuery.prototype.handleRowDescription = function(rowDescription) { NativeQuery.prototype.handleRowDescription = function(rowDescription) {
//multiple query statements in 1 action can result in multiple sets this._result.addFields(rowDescription);
//of rowDescriptions...eg: 'select NOW(); select 1::int;'
if(this._result.fields.length) {
this._result.fields = [];
}
for(var i = 0, len = rowDescription.length; i < len; i++) {
this._result.addField(rowDescription[i]);
}
}; };
NativeQuery.prototype.handleRow = function(rowData) { NativeQuery.prototype.handleRow = function(rowData) {
var row = {}; var row = this._result.parseRow(rowData);
for(var i = 0, len = rowData.length; i < len; i++) {
var rawValue = rowData[i];
var field = this._result.fields[i];
var fieldType = field.dataTypeID;
var parsedValue = null;
if(rawValue !== null) {
parsedValue = types.getTypeParser(fieldType, 'text')(rawValue);
}
var fieldName = field.name;
row[fieldName] = parsedValue;
}
if(this.callback) { if(this.callback) {
this._result.addRow(row); this._result.addRow(row);
} }

View File

@ -55,36 +55,16 @@ var noParse = function(val) {
//message with this query object //message with this query object
//metadata used when parsing row results //metadata used when parsing row results
Query.prototype.handleRowDescription = function(msg) { Query.prototype.handleRowDescription = function(msg) {
this._fieldNames = []; this._result.addFields(msg.fields);
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._fieldConverters[i] = Types.getTypeParser(field.dataTypeID, format);
this._result.addField(field);
}
}; };
Query.prototype.handleDataRow = function(msg) { Query.prototype.handleDataRow = function(msg) {
var self = this; var row = this._result.parseRow(msg.fields);
var row = {}; this.emit('row', row, this._result);
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);
}
}
self.emit('row', row, self._result);
//if there is a callback collect rows //if there is a callback collect rows
if(self.callback) { if(this.callback) {
self._result.addRow(row); this._result.addRow(row);
} }
}; };

View File

@ -1,3 +1,5 @@
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
@ -34,13 +36,39 @@ Result.prototype.addCommandComplete = function(msg) {
} }
}; };
//rowData is an array of text or binary values
//this turns the row into a JavaScript object
Result.prototype.parseRow = function(rowData) {
var row = {};
for(var i = 0, len = rowData.length; i < len; i++) {
var rawValue = rowData[i];
var field = this.fields[i];
var fieldType = field.dataTypeID;
var parsedValue = null;
if(rawValue !== null) {
parsedValue = types.getTypeParser(fieldType, field.format || 'text')(rawValue);
}
var fieldName = field.name;
row[fieldName] = parsedValue;
}
return row;
};
Result.prototype.addRow = function(row) { Result.prototype.addRow = function(row) {
this.rows.push(row); this.rows.push(row);
}; };
//Add a field definition to the result Result.prototype.addFields = function(fieldDescriptions) {
Result.prototype.addField = function(field) { //clears field definitions
this.fields.push(field); //multiple query statements in 1 action can result in multiple sets
//of rowDescriptions...eg: 'select NOW(); select 1::int;'
//you need to reset the fields
if(this.fields.length) {
this.fields = [];
}
for(var i = 0; i < fieldDescriptions.length; i++) {
this.fields.push(fieldDescriptions[i]);
}
}; };
module.exports = Result; module.exports = Result;