-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
dont use dynamic functions to parse rows, closes #1417 #1603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
*/ | ||
|
||
var types = require('pg-types') | ||
var escape = require('js-string-escape') | ||
|
||
// result object returned from query | ||
// in the 'end' event and also | ||
|
@@ -65,29 +64,24 @@ Result.prototype._parseRowAsArray = function (rowData) { | |
return row | ||
} | ||
|
||
// rowData is an array of text or binary values | ||
// this turns the row into a JavaScript object | ||
Result.prototype.parseRow = function (rowData) { | ||
return new this.RowCtor(this._parsers, rowData) | ||
var row = {} | ||
for (var i = 0, len = rowData.length; i < len; i++) { | ||
var rawValue = rowData[i] | ||
var field = this.fields[i].name | ||
if (rawValue !== null) { | ||
row[field] = this._parsers[i](rawValue) | ||
} else { | ||
row[field] = null | ||
} | ||
} | ||
return row | ||
} | ||
|
||
Result.prototype.addRow = function (row) { | ||
this.rows.push(row) | ||
} | ||
|
||
var inlineParser = function (fieldName, i) { | ||
return "\nthis['" + | ||
// fields containing single quotes will break | ||
// the evaluated javascript unless they are escaped | ||
// see https://github.com/brianc/node-postgres/issues/507 | ||
// Addendum: However, we need to make sure to replace all | ||
// occurences of apostrophes, not just the first one. | ||
// See https://github.com/brianc/node-postgres/issues/934 | ||
escape(fieldName) + | ||
"'] = " + | ||
'rowData[' + i + '] == null ? null : parsers[' + i + '](rowData[' + i + ']);' | ||
} | ||
|
||
Result.prototype.addFields = function (fieldDescriptions) { | ||
// clears field definitions | ||
// multiple query statements in 1 action can result in multiple sets | ||
|
@@ -97,18 +91,11 @@ Result.prototype.addFields = function (fieldDescriptions) { | |
this.fields = [] | ||
this._parsers = [] | ||
} | ||
var ctorBody = '' | ||
for (var i = 0; i < fieldDescriptions.length; i++) { | ||
var desc = fieldDescriptions[i] | ||
this.fields.push(desc) | ||
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text') | ||
this._parsers.push(parser) | ||
// this is some craziness to compile the row result parsing | ||
// results in ~60% speedup on large query result sets | ||
ctorBody += inlineParser(desc.name, i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you guys are dead set against this pull, then at least one improvement that could be definitely made to pg is to make ctorBody an array, have this line push the inline parser string into the array and then do an array.join at the end, currently here we are making 2 strings for each field of the results |
||
} | ||
if (!this.rowAsArray) { | ||
this.RowCtor = Function('parsers', 'rowData', ctorBody) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a simple
reduce
but I kept the same style as the array parser for consistency.