Skip to content

Commit 413eff7

Browse files
committed
Move row parsing into result object
1 parent 3f96bbb commit 413eff7

File tree

3 files changed

+38
-59
lines changed

3 files changed

+38
-59
lines changed

lib/native/query.js

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,41 +34,12 @@ var NativeQuery = function(config, values, callback) {
3434

3535
util.inherits(NativeQuery, EventEmitter);
3636

37-
//maps from native rowdata into api compatible row object
38-
var mapRowData = function(row) {
39-
var result = {};
40-
for(var i = 0, len = row.length; i < len; i++) {
41-
var item = row[i];
42-
result[item.name] = item.value === null ? null :
43-
types.getTypeParser(item.dataTypeID, 'text')(item.value);
44-
}
45-
return result;
46-
};
47-
4837
NativeQuery.prototype.handleRowDescription = function(rowDescription) {
49-
//multiple query statements in 1 action can result in multiple sets
50-
//of rowDescriptions...eg: 'select NOW(); select 1::int;'
51-
if(this._result.fields.length) {
52-
this._result.fields = [];
53-
}
54-
for(var i = 0, len = rowDescription.length; i < len; i++) {
55-
this._result.addField(rowDescription[i]);
56-
}
38+
this._result.addFields(rowDescription);
5739
};
5840

5941
NativeQuery.prototype.handleRow = function(rowData) {
60-
var row = {};
61-
for(var i = 0, len = rowData.length; i < len; i++) {
62-
var rawValue = rowData[i];
63-
var field = this._result.fields[i];
64-
var fieldType = field.dataTypeID;
65-
var parsedValue = null;
66-
if(rawValue !== null) {
67-
parsedValue = types.getTypeParser(fieldType, 'text')(rawValue);
68-
}
69-
var fieldName = field.name;
70-
row[fieldName] = parsedValue;
71-
}
42+
var row = this._result.parseRow(rowData);
7243
if(this.callback) {
7344
this._result.addRow(row);
7445
}

lib/query.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,16 @@ var noParse = function(val) {
5555
//message with this query object
5656
//metadata used when parsing row results
5757
Query.prototype.handleRowDescription = function(msg) {
58-
this._fieldNames = [];
59-
this._fieldConverters = [];
60-
var len = msg.fields.length;
61-
for(var i = 0; i < len; i++) {
62-
var field = msg.fields[i];
63-
var format = field.format;
64-
this._fieldNames[i] = field.name;
65-
this._fieldConverters[i] = Types.getTypeParser(field.dataTypeID, format);
66-
this._result.addField(field);
67-
}
58+
this._result.addFields(msg.fields);
6859
};
6960

7061
Query.prototype.handleDataRow = function(msg) {
71-
var self = this;
72-
var row = {};
73-
for(var i = 0; i < msg.fields.length; i++) {
74-
var rawValue = msg.fields[i];
75-
if(rawValue === null) {
76-
//leave null values alone
77-
row[self._fieldNames[i]] = null;
78-
} else {
79-
//convert value to javascript
80-
row[self._fieldNames[i]] = self._fieldConverters[i](rawValue);
81-
}
82-
}
83-
self.emit('row', row, self._result);
62+
var row = this._result.parseRow(msg.fields);
63+
this.emit('row', row, this._result);
8464

8565
//if there is a callback collect rows
86-
if(self.callback) {
87-
self._result.addRow(row);
66+
if(this.callback) {
67+
this._result.addRow(row);
8868
}
8969
};
9070

lib/result.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var types = require(__dirname + '/types/');
2+
13
//result object returned from query
24
//in the 'end' event and also
35
//passed as second argument to provided callback
@@ -34,13 +36,39 @@ Result.prototype.addCommandComplete = function(msg) {
3436
}
3537
};
3638

39+
//rowData is an array of text or binary values
40+
//this turns the row into a JavaScript object
41+
Result.prototype.parseRow = function(rowData) {
42+
var row = {};
43+
for(var i = 0, len = rowData.length; i < len; i++) {
44+
var rawValue = rowData[i];
45+
var field = this.fields[i];
46+
var fieldType = field.dataTypeID;
47+
var parsedValue = null;
48+
if(rawValue !== null) {
49+
parsedValue = types.getTypeParser(fieldType, field.format || 'text')(rawValue);
50+
}
51+
var fieldName = field.name;
52+
row[fieldName] = parsedValue;
53+
}
54+
return row;
55+
};
56+
3757
Result.prototype.addRow = function(row) {
3858
this.rows.push(row);
3959
};
4060

41-
//Add a field definition to the result
42-
Result.prototype.addField = function(field) {
43-
this.fields.push(field);
61+
Result.prototype.addFields = function(fieldDescriptions) {
62+
//clears field definitions
63+
//multiple query statements in 1 action can result in multiple sets
64+
//of rowDescriptions...eg: 'select NOW(); select 1::int;'
65+
//you need to reset the fields
66+
if(this.fields.length) {
67+
this.fields = [];
68+
}
69+
for(var i = 0; i < fieldDescriptions.length; i++) {
70+
this.fields.push(fieldDescriptions[i]);
71+
}
4472
};
4573

4674
module.exports = Result;

0 commit comments

Comments
 (0)