From 03eed3e3bca9aee743bef4da381cad05b30e2afc Mon Sep 17 00:00:00 2001 From: contra Date: Mon, 2 Apr 2018 13:55:10 -0400 Subject: [PATCH] dont use dynamic functions to parse rows, closes #1417 --- lib/result.js | 35 +++++++++++------------------------ package.json | 1 - 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/lib/result.js b/lib/result.js index 7dfd116f0..1e6bf849d 100644 --- a/lib/result.js +++ b/lib/result.js @@ -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) } } diff --git a/package.json b/package.json index 7eeff6c43..3aa3de865 100644 --- a/package.json +++ b/package.json @@ -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",