Move row parsing into result object
This commit is contained in:
parent
3f96bbbc5c
commit
413eff72e5
@ -34,41 +34,12 @@ var NativeQuery = function(config, values, callback) {
|
||||
|
||||
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) {
|
||||
//multiple query statements in 1 action can result in multiple sets
|
||||
//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]);
|
||||
}
|
||||
this._result.addFields(rowDescription);
|
||||
};
|
||||
|
||||
NativeQuery.prototype.handleRow = function(rowData) {
|
||||
var row = {};
|
||||
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;
|
||||
}
|
||||
var row = this._result.parseRow(rowData);
|
||||
if(this.callback) {
|
||||
this._result.addRow(row);
|
||||
}
|
||||
|
30
lib/query.js
30
lib/query.js
@ -55,36 +55,16 @@ var noParse = function(val) {
|
||||
//message with this query object
|
||||
//metadata used when parsing row results
|
||||
Query.prototype.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._fieldConverters[i] = Types.getTypeParser(field.dataTypeID, format);
|
||||
this._result.addField(field);
|
||||
}
|
||||
this._result.addFields(msg.fields);
|
||||
};
|
||||
|
||||
Query.prototype.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);
|
||||
}
|
||||
}
|
||||
self.emit('row', row, self._result);
|
||||
var row = this._result.parseRow(msg.fields);
|
||||
this.emit('row', row, this._result);
|
||||
|
||||
//if there is a callback collect rows
|
||||
if(self.callback) {
|
||||
self._result.addRow(row);
|
||||
if(this.callback) {
|
||||
this._result.addRow(row);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
var types = require(__dirname + '/types/');
|
||||
|
||||
//result object returned from query
|
||||
//in the 'end' event and also
|
||||
//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) {
|
||||
this.rows.push(row);
|
||||
};
|
||||
|
||||
//Add a field definition to the result
|
||||
Result.prototype.addField = function(field) {
|
||||
this.fields.push(field);
|
||||
Result.prototype.addFields = function(fieldDescriptions) {
|
||||
//clears field definitions
|
||||
//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;
|
||||
|
Loading…
Reference in New Issue
Block a user