Skip to content

Commit 8de770e

Browse files
committed
added possibility of using encoding when reading strings
1 parent f7ae159 commit 8de770e

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

lib/compile_text_parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function readCodeFor (type, charset, config, options) {
146146
if (charset == Charsets.BINARY) {
147147
return 'packet.readLengthCodedBuffer()';
148148
} else {
149-
return 'packet.readLengthCodedString()';
149+
return 'packet.readLengthCodedString(' + charset + ')';
150150
}
151151
}
152152
}

lib/packets/column_definition.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Buffer = require('safe-buffer').Buffer;
22
var Packet = require('../packets/packet');
3+
var StringParser = require('../parsers/string');
34

45
// creating JS string is relatively expensive (compared to
56
// reading few bytes from buffer) because all string properties
@@ -34,14 +35,22 @@ function ColumnDefinition (packet)
3435
packet.offset += this._orgTableLength;
3536

3637
// name is always used, don't make it lazy
37-
this.name = packet.readLengthCodedString();
38+
var _nameLength = packet.readLengthCodedNumber();
39+
var _nameStart = packet.offset;
40+
packet.offset += _nameLength;
3841

3942
this._orgNameLength = packet.readLengthCodedNumber();
4043
this._orgNameStart = packet.offset;
4144
packet.offset += this._orgNameLength;
4245

4346
packet.skip(1); // length of the following fields (always 0x0c)
4447
this.characterSet = packet.readInt16();
48+
49+
// TODO: use this.characterSet to get proper encoding
50+
// May be keep it cesu8 since MySQL meta data is actually cesu8
51+
// https://github.com/sidorares/node-mysql2/pull/374
52+
this.name = StringParser.decode(this._buf.slice(_nameStart, _nameStart + _nameLength), 'cesu8');
53+
4554
this.columnLength = packet.readInt32();
4655
this.columnType = packet.readInt8();
4756
this.flags = packet.readInt16();

lib/packets/packet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,15 @@ Packet.prototype.readTimeString = function (convertTtoMs) {
342342
return (sign === -1 ? '-' : '') + [(d ? (d * 24) + H : H), leftPad(2, M), leftPad(2, S)].join(':') + (ms ? '.' + ms : '');
343343
};
344344

345-
Packet.prototype.readLengthCodedString = function () {
345+
Packet.prototype.readLengthCodedString = function (characterSetCode) {
346346
var len = this.readLengthCodedNumber();
347347
// TODO: check manually first byte here to avoid polymorphic return type?
348348
if (len === null) {
349349
return null;
350350
}
351351
this.offset += len;
352352

353-
// because MySQL utf8 is actually cesu8
353+
// TODO: Use characterSetCode to get proper encoding
354354
// https://github.com/sidorares/node-mysql2/pull/374
355355
return StringParser.decode(this.buffer.slice(this.offset - len, this.offset), 'cesu8');
356356
};

0 commit comments

Comments
 (0)