Skip to content

Commit 1a31bde

Browse files
author
Andrey Sidorov
committed
WIP: pass encoding to writers and readers
1 parent 7acf22c commit 1a31bde

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

lib/commands/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Query.prototype.doneInsert = function (rs) {
8585
};
8686

8787
Query.prototype.resultsetHeader = function (packet, connection) {
88-
var rs = new Packets.ResultSetHeader(packet, connection.config.bigNumberStrings);
88+
var rs = new Packets.ResultSetHeader(packet, connection.config.bigNumberStrings, connection.serverEncoding);
8989
this._fieldCount = rs.fieldCount;
9090
if (connection.config.debug) {
9191
console.log(' Resultset header received, expecting ' + rs.fieldCount + ' column definition packets');

lib/compile_binary_parser.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ function compile (fields, options, config) {
9999
function readCodeFor (field, config, options) {
100100
var supportBigNumbers = options.supportBigNumbers || config.supportBigNumbers;
101101
var bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
102-
103102
var unsigned = field.flags & FieldFlags.UNSIGNED;
104103
switch (field.columnType) {
105104
case Types.TINY:

lib/compile_text_parser.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ function compile (fields, options, config) {
1414

1515
// node-mysql typeCast compatibility wrapper
1616
// see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
17-
function wrap (field, type, packet) {
17+
function wrap (field, type, packet, encoding) {
1818
return {
1919
type: type,
2020
length: field.columnLength,
2121
db: field.schema,
2222
table: field.table,
2323
name: field.name,
24-
string: function () { return packet.readLengthCodedString(); },
24+
string: function () { return packet.readLengthCodedString(encoding); },
2525
buffer: function () { return packet.readLengthCodedBuffer(); },
2626
geometry: function () { return packet.parseGeometryValue(); }
2727
};
28-
};
28+
}
2929

3030
var result = [];
3131
var i = 0;
@@ -74,9 +74,10 @@ function compile (fields, options, config) {
7474
} else {
7575
lvalue = ' this[' + srcEscape(fields[i].name) + ']';
7676
}
77-
var readCode = readCodeFor(fields[i].columnType, fields[i].characterSet, config, options);
77+
var encoding = CharsetToEncoding[fields[i].characterSet];
78+
var readCode = readCodeFor(fields[i].columnType, fields[i].characterSet, encoding, config, options);
7879
if (typeof options.typeCast === 'function') {
79-
result.push(lvalue + ' = options.typeCast(wrap(fields[' + i + '], ' + srcEscape(typeNames[fields[i].columnType]) + ', packet), function() { return ' + readCode + ';})');
80+
result.push(lvalue + ' = options.typeCast(wrap(fields[' + i + '], ' + srcEscape(typeNames[fields[i].columnType]) + ', packet, "' + encoding + '"), function() { return ' + readCode + ';})');
8081
} else if (options.typeCast === false) {
8182
result.push(lvalue + ' = packet.readLengthCodedBuffer();');
8283
} else {
@@ -99,7 +100,7 @@ function compile (fields, options, config) {
99100
return vm.runInThisContext(src);
100101
}
101102

102-
function readCodeFor (type, charset, config, options) {
103+
function readCodeFor (type, charset, encoding, config, options) {
103104
var supportBigNumbers = options.supportBigNumbers || config.supportBigNumbers;
104105
var bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
105106

@@ -142,12 +143,12 @@ function readCodeFor (type, charset, config, options) {
142143
case Types.GEOMETRY:
143144
return 'packet.parseGeometryValue()';
144145
case Types.JSON:
145-
return 'JSON.parse(packet.readLengthCodedString("' + CharsetToEncoding[charset] + '"))';
146+
return 'JSON.parse(packet.readLengthCodedString("' + encoding + '"))';
146147
default:
147148
if (charset == Charsets.BINARY) {
148149
return 'packet.readLengthCodedBuffer()';
149150
} else {
150-
return 'packet.readLengthCodedString("' + CharsetToEncoding[charset] + '")';
151+
return 'packet.readLengthCodedString("' + encoding + '")';
151152
}
152153
}
153154
}

lib/connection.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Connection.prototype.writePacket = function (packet) {
177177
packet.writeHeader(this.sequenceId);
178178
if (this.config.debug) {
179179
console.log(this._internalId + ' ' + this.connectionId + ' <== ' + this._command._commandName + '#' + this._command.stateName() + '(' + [this.sequenceId, packet._name, packet.length()].join(',') + ')');
180+
console.log(this._internalId + ' ' + this.connectionId + ' <== ' + packet.buffer.toString('hex'));
180181
}
181182
this.sequenceId++;
182183
if (this.sequenceId == 256) {
@@ -661,7 +662,7 @@ Connection.prototype.writeColumns = function (columns) {
661662
var connection = this;
662663
this.writePacket(Packets.ResultSetHeader.toPacket(columns.length));
663664
columns.forEach(function (column) {
664-
connection.writePacket(Packets.ColumnDefinition.toPacket(column, this.serverConfig.encoding));
665+
connection.writePacket(Packets.ColumnDefinition.toPacket(column, connection.serverConfig.encoding));
665666
});
666667
this.writeEof();
667668
};
@@ -696,6 +697,8 @@ Connection.prototype.writeOk = function (args) {
696697
};
697698

698699
Connection.prototype.writeError = function (args) {
700+
// if we want to send error before initial hello was sent, use default encoding
701+
var encoding = this.serverConfig ? this.serverConfig.encoding : 'cesu8';
699702
this.writePacket(Packets.Error.toPacket(args, this.serverConfig.encoding));
700703
};
701704

lib/packets/packet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@ Packet.prototype.writeLengthCodedString = function (s, encoding) {
760760

761761
Packet.prototype.writeLengthCodedBuffer = function (b) {
762762
this.writeLengthCodedNumber(b.length);
763-
this.offset += b.length;
764763
b.copy(this.buffer, this.offset);
764+
this.offset += b.length;
765765
};
766766

767767
Packet.prototype.writeLengthCodedNumber = function (n) {

lib/packets/resultset_header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var Buffer = require('safe-buffer').Buffer;
44
var Packet = require('../packets/packet');
55

6-
function ResultSetHeader (packet, bigNumberStrings)
6+
function ResultSetHeader (packet, bigNumberStrings, encoding)
77
{
88
if (packet.buffer[packet.offset] !== 0) {
99
this.fieldCount = packet.readLengthCodedNumber();
@@ -15,7 +15,7 @@ function ResultSetHeader (packet, bigNumberStrings)
1515
this.warningStatus = packet.readInt16();
1616
}
1717
if (this.fieldCount === null) {
18-
this.infileName = packet.readString();
18+
this.infileName = packet.readString(undefined, encoding);
1919
}
2020

2121
// snippet from mysql-native:

0 commit comments

Comments
 (0)