Skip to content

Commit 2329883

Browse files
committed
Fix 100% CPU problem
1 parent 3c300a8 commit 2329883

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

lib/commands/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class Query extends Command {
212212
if (this._receivedFieldsCount === this._fieldCount) {
213213
const fields = this._fields[this._resultIndex];
214214
this.emit('fields', fields);
215-
this._rowParser = new (getTextParser(fields, this.options, connection.config))();
215+
this._rowParser = new (getTextParser(fields, this.options, connection.config))(fields);
216216
return Query.prototype.fieldsEOF;
217217
}
218218
return Query.prototype.readField;

lib/parsers/text_parser.js

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ function compile(fields, options, config) {
7878
options.typeCast = config.typeCast;
7979
}
8080

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+
81100
const parserFn = genFunc();
82101

83102
/* eslint-disable no-trailing-spaces */
@@ -88,42 +107,14 @@ function compile(fields, options, config) {
88107
);
89108

90109
// constructor method
91-
parserFn('constructor() {');
110+
parserFn('constructor(fields) {');
92111
// node-mysql typeCast compatibility wrapper
93112
// see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
94113
if (typeof options.typeCast === 'function') {
95114
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('}');
127118
}
128119
parserFn('}');
129120

@@ -165,9 +156,7 @@ function compile(fields, options, config) {
165156
} else {
166157
lvalue = `result[${fieldName}]`;
167158
}
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) {
171160
parserFn(`${lvalue} = packet.readLengthCodedBuffer();`);
172161
} else {
173162
const encodingExpr = `fields[${i}].encoding`;
@@ -178,8 +167,12 @@ function compile(fields, options, config) {
178167
config,
179168
options
180169
);
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+
}
183176
}
184177

185178
parserFn('return result;');
@@ -196,6 +189,9 @@ function compile(fields, options, config) {
196189
parserFn.toString()
197190
);
198191
}
192+
if (typeof options.typeCast === 'function') {
193+
return parserFn.toFunction({wrap});
194+
}
199195
return parserFn.toFunction();
200196
}
201197

0 commit comments

Comments
 (0)