@@ -78,6 +78,25 @@ function compile(fields, options, config) {
78
78
options . typeCast = config . typeCast ;
79
79
}
80
80
81
+ function wrap ( field , _this ) {
82
+ return {
83
+ type : typeNames [ field . columnType ] ,
84
+ length : field . columnLength ,
85
+ db : field . schema ,
86
+ table : field . table ,
87
+ name : field . name ,
88
+ string : function ( ) {
89
+ return _this . packet . readLengthCodedString ( field . encoding ) ;
90
+ } ,
91
+ buffer : function ( ) {
92
+ return _this . packet . readLengthCodedBuffer ( ) ;
93
+ } ,
94
+ geometry : function ( ) {
95
+ return _this . packet . parseGeometryValue ( ) ;
96
+ }
97
+ } ;
98
+ }
99
+
81
100
const parserFn = genFunc ( ) ;
82
101
83
102
/* eslint-disable no-trailing-spaces */
@@ -88,42 +107,14 @@ function compile(fields, options, config) {
88
107
) ;
89
108
90
109
// constructor method
91
- parserFn ( 'constructor() {' ) ;
110
+ parserFn ( 'constructor(fields ) {' ) ;
92
111
// node-mysql typeCast compatibility wrapper
93
112
// see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
94
113
if ( typeof options . typeCast === 'function' ) {
95
114
parserFn ( 'const _this = this;' ) ;
96
- for ( let i = 0 ; i < fields . length ; ++ i ) {
97
- const field = fields [ i ] ;
98
- const encodingExpr = helpers . srcEscape ( field . encoding ) ;
99
- const readCode = readCodeFor (
100
- fields [ i ] . columnType ,
101
- fields [ i ] . characterSet ,
102
- encodingExpr ,
103
- config ,
104
- options
105
- ) ;
106
- parserFn ( `this.wrap${ i } = {
107
- type: ${ helpers . srcEscape ( typeNames [ field . columnType ] ) } ,
108
- length: ${ field . columnLength } ,
109
- db: ${ helpers . srcEscape ( field . schema ) } ,
110
- table: ${ helpers . srcEscape ( field . table ) } ,
111
- name: ${ helpers . srcEscape ( field . name ) } ,
112
- string: function() {
113
- return _this.packet.readLengthCodedString(${ encodingExpr } );
114
- },
115
- buffer: function() {
116
- return _this.packet.readLengthCodedBuffer();
117
- },
118
- geometry: function() {
119
- return _this.packet.parseGeometryValue();
120
- },
121
- readNext: function() {
122
- const packet = _this.packet;
123
- return ${ readCode } ;
124
- }
125
- };` ) ;
126
- }
115
+ parserFn ( 'for(let i=0; i<fields.length; ++i) {' ) ;
116
+ parserFn ( 'this[`wrap${i}`] = wrap(fields[i], _this);' ) ;
117
+ parserFn ( '}' ) ;
127
118
}
128
119
parserFn ( '}' ) ;
129
120
@@ -165,9 +156,7 @@ function compile(fields, options, config) {
165
156
} else {
166
157
lvalue = `result[${ fieldName } ]` ;
167
158
}
168
- if ( typeof options . typeCast === 'function' ) {
169
- parserFn ( `${ lvalue } = options.typeCast(this.wrap${ i } , this.wrap${ i } .readNext);` ) ;
170
- } else if ( options . typeCast === false ) {
159
+ if ( options . typeCast === false ) {
171
160
parserFn ( `${ lvalue } = packet.readLengthCodedBuffer();` ) ;
172
161
} else {
173
162
const encodingExpr = `fields[${ i } ].encoding` ;
@@ -178,8 +167,12 @@ function compile(fields, options, config) {
178
167
config ,
179
168
options
180
169
) ;
181
- parserFn ( `${ lvalue } = ${ readCode } ;` ) ;
182
- }
170
+ if ( typeof options . typeCast === 'function' ) {
171
+ parserFn ( `${ lvalue } = options.typeCast(this.wrap${ i } , function() { return ${ readCode } });` ) ;
172
+ } else {
173
+ parserFn ( `${ lvalue } = ${ readCode } ;` ) ;
174
+ }
175
+ }
183
176
}
184
177
185
178
parserFn ( 'return result;' ) ;
@@ -196,6 +189,9 @@ function compile(fields, options, config) {
196
189
parserFn . toString ( )
197
190
) ;
198
191
}
192
+ if ( typeof options . typeCast === 'function' ) {
193
+ return parserFn . toFunction ( { wrap} ) ;
194
+ }
199
195
return parserFn . toFunction ( ) ;
200
196
}
201
197
0 commit comments