Skip to content

Commit 72db790

Browse files
yocontrabrianc
authored andcommitted
dont use dynamic functions to parse rows, closes brianc#1417
1 parent 4905471 commit 72db790

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

lib/result.js

+11-24
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
var types = require('pg-types')
11-
var escape = require('js-string-escape')
1211

1312
// result object returned from query
1413
// in the 'end' event and also
@@ -65,29 +64,24 @@ Result.prototype._parseRowAsArray = function (rowData) {
6564
return row
6665
}
6766

68-
// rowData is an array of text or binary values
69-
// this turns the row into a JavaScript object
7067
Result.prototype.parseRow = function (rowData) {
71-
return new this.RowCtor(this._parsers, rowData)
68+
var row = {}
69+
for (var i = 0, len = rowData.length; i < len; i++) {
70+
var rawValue = rowData[i]
71+
var field = this.fields[i].name
72+
if (rawValue !== null) {
73+
row[field] = this._parsers[i](rawValue)
74+
} else {
75+
row[field] = null
76+
}
77+
}
78+
return row
7279
}
7380

7481
Result.prototype.addRow = function (row) {
7582
this.rows.push(row)
7683
}
7784

78-
var inlineParser = function (fieldName, i) {
79-
return "\nthis['" +
80-
// fields containing single quotes will break
81-
// the evaluated javascript unless they are escaped
82-
// see https://github.com/brianc/node-postgres/issues/507
83-
// Addendum: However, we need to make sure to replace all
84-
// occurences of apostrophes, not just the first one.
85-
// See https://github.com/brianc/node-postgres/issues/934
86-
escape(fieldName) +
87-
"'] = " +
88-
'rowData[' + i + '] == null ? null : parsers[' + i + '](rowData[' + i + ']);'
89-
}
90-
9185
Result.prototype.addFields = function (fieldDescriptions) {
9286
// clears field definitions
9387
// multiple query statements in 1 action can result in multiple sets
@@ -97,18 +91,11 @@ Result.prototype.addFields = function (fieldDescriptions) {
9791
this.fields = []
9892
this._parsers = []
9993
}
100-
var ctorBody = ''
10194
for (var i = 0; i < fieldDescriptions.length; i++) {
10295
var desc = fieldDescriptions[i]
10396
this.fields.push(desc)
10497
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text')
10598
this._parsers.push(parser)
106-
// this is some craziness to compile the row result parsing
107-
// results in ~60% speedup on large query result sets
108-
ctorBody += inlineParser(desc.name, i)
109-
}
110-
if (!this.rowAsArray) {
111-
this.RowCtor = Function('parsers', 'rowData', ctorBody)
11299
}
113100
}
114101

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"main": "./lib",
2121
"dependencies": {
2222
"buffer-writer": "1.0.1",
23-
"js-string-escape": "1.0.1",
2423
"packet-reader": "0.3.1",
2524
"pg-connection-string": "0.1.3",
2625
"pg-pool": "~2.0.3",

0 commit comments

Comments
 (0)