88 */
99
1010var 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
7067Result . 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
7481Result . 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-
9185Result . 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
0 commit comments