@@ -70,27 +70,6 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
70
70
}
71
71
72
72
function compile ( fields , options , config ) {
73
- // node-mysql typeCast compatibility wrapper
74
- // see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
75
- function wrap ( field , type , packet , encoding ) {
76
- return {
77
- type : type ,
78
- length : field . columnLength ,
79
- db : field . schema ,
80
- table : field . table ,
81
- name : field . name ,
82
- string : function ( ) {
83
- return packet . readLengthCodedString ( encoding ) ;
84
- } ,
85
- buffer : function ( ) {
86
- return packet . readLengthCodedBuffer ( ) ;
87
- } ,
88
- geometry : function ( ) {
89
- return packet . parseGeometryValue ( ) ;
90
- }
91
- } ;
92
- }
93
-
94
73
// use global typeCast if current query doesn't specify one
95
74
if (
96
75
typeof config . typeCast === 'function' &&
@@ -100,41 +79,78 @@ function compile(fields, options, config) {
100
79
}
101
80
102
81
const parserFn = genFunc ( ) ;
103
- let i = 0 ;
104
82
105
83
/* eslint-disable no-trailing-spaces */
106
84
/* eslint-disable no-spaced-func */
107
85
/* eslint-disable no-unexpected-multiline */
108
86
parserFn ( '(function () {' ) (
109
- 'return function TextRow(packet, fields, options, CharsetToEncoding) {'
87
+ 'return class TextRow {'
110
88
) ;
111
89
112
- if ( options . rowsAsArray ) {
113
- parserFn ( `const result = new Array(${ fields . length } )` ) ;
114
- } else {
115
- parserFn ( "const result = {}" ) ;
90
+ // constructor method
91
+ parserFn ( 'constructor() {' ) ;
92
+ // node-mysql typeCast compatibility wrapper
93
+ // see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
94
+ if ( typeof options . typeCast === 'function' ) {
95
+ parserFn ( 'const _this = this;' ) ;
96
+ for ( let i = 0 ; i < fields . length ; ++ i ) {
97
+ const field = fields [ i ] ;
98
+ const encodingExpr = `fields[${ i } ].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: ${ helpers . srcEscape ( 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(${ helpers . srcEscape ( field . encoding ) } );
114
+ },
115
+ buffer: function() {
116
+ return _this.packet.readLengthCodedBuffer();
117
+ },
118
+ geometry: function() {
119
+ return _this.packet.parseGeometryValue();
120
+ },
121
+ readNext: function() {
122
+ return _this.${ readCode } ;
123
+ }
124
+ };` ) ;
125
+ }
116
126
}
127
+ parserFn ( '}' ) ;
117
128
118
- if ( typeof options . typeCast === 'function' ) {
119
- parserFn ( `const wrap = ${ wrap . toString ( ) } ` ) ;
129
+ // next method
130
+ parserFn ( 'next(packet, fields, options) {' ) ;
131
+ parserFn ( "this.packet = packet;" ) ;
132
+ if ( options . rowsAsArray ) {
133
+ parserFn ( `const result = new Array(${ fields . length } );` ) ;
134
+ } else {
135
+ parserFn ( "const result = {};" ) ;
120
136
}
121
137
122
138
const resultTables = { } ;
123
139
let resultTablesArray = [ ] ;
124
140
125
141
if ( options . nestTables === true ) {
126
- for ( i = 0 ; i < fields . length ; i ++ ) {
142
+ for ( let i = 0 ; i < fields . length ; i ++ ) {
127
143
resultTables [ fields [ i ] . table ] = 1 ;
128
144
}
129
145
resultTablesArray = Object . keys ( resultTables ) ;
130
- for ( i = 0 ; i < resultTablesArray . length ; i ++ ) {
146
+ for ( let i = 0 ; i < resultTablesArray . length ; i ++ ) {
131
147
parserFn ( `result[${ helpers . srcEscape ( resultTablesArray [ i ] ) } ] = {};` ) ;
132
148
}
133
149
}
134
150
135
151
let lvalue = '' ;
136
152
let fieldName = '' ;
137
- for ( i = 0 ; i < fields . length ; i ++ ) {
153
+ for ( let i = 0 ; i < fields . length ; i ++ ) {
138
154
fieldName = helpers . srcEscape ( fields [ i ] . name ) ;
139
155
parserFn ( `// ${ fieldName } : ${ typeNames [ fields [ i ] . columnType ] } ` ) ;
140
156
if ( typeof options . nestTables === 'string' ) {
@@ -148,29 +164,25 @@ function compile(fields, options, config) {
148
164
} else {
149
165
lvalue = `result[${ fieldName } ]` ;
150
166
}
151
- const encodingExpr = `CharsetToEncoding[fields[${ i } ].characterSet]` ;
152
- const readCode = readCodeFor (
153
- fields [ i ] . columnType ,
154
- fields [ i ] . characterSet ,
155
- encodingExpr ,
156
- config ,
157
- options
158
- ) ;
159
167
if ( typeof options . typeCast === 'function' ) {
160
- parserFn (
161
- `${ lvalue } = options.typeCast(wrap(fields[${ i } ], ${ helpers . srcEscape (
162
- typeNames [ fields [ i ] . columnType ]
163
- ) } , packet, ${ encodingExpr } ), function() { return ${ readCode } ;})`
164
- ) ;
168
+ parserFn ( `${ lvalue } = options.typeCast(this.wrap${ i } , this.wrap${ i } .readNext);` ) ;
165
169
} else if ( options . typeCast === false ) {
166
170
parserFn ( `${ lvalue } = packet.readLengthCodedBuffer();` ) ;
167
171
} else {
172
+ const encodingExpr = `fields[${ i } ].encoding` ;
173
+ const readCode = readCodeFor (
174
+ fields [ i ] . columnType ,
175
+ fields [ i ] . characterSet ,
176
+ encodingExpr ,
177
+ config ,
178
+ options
179
+ ) ;
168
180
parserFn ( `${ lvalue } = ${ readCode } ;` ) ;
169
181
}
170
182
}
171
183
172
184
parserFn ( 'return result;' ) ;
173
-
185
+ parserFn ( '}' ) ;
174
186
parserFn ( '};' ) ( '})()' ) ;
175
187
176
188
/* eslint-enable no-trailing-spaces */
0 commit comments