mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
dont use dynamic functions to parse rows, closes #1417
This commit is contained in:
parent
49054717b4
commit
72db7902fa
@ -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)
|
||||
}
|
||||
if (!this.rowAsArray) {
|
||||
this.RowCtor = Function('parsers', 'rowData', ctorBody)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -20,7 +20,6 @@
|
||||
"main": "./lib",
|
||||
"dependencies": {
|
||||
"buffer-writer": "1.0.1",
|
||||
"js-string-escape": "1.0.1",
|
||||
"packet-reader": "0.3.1",
|
||||
"pg-connection-string": "0.1.3",
|
||||
"pg-pool": "~2.0.3",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user