Skip to content

Commit b53f6e2

Browse files
Merge pull request #374 from sidorares/fix-char-encoding
Fixes #302
2 parents ac28fb6 + 96d6e56 commit b53f6e2

39 files changed

+475
-154
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ node_js:
88
- '0.12'
99
- '4.5'
1010
- '5.12'
11-
- '6.4'
11+
- '6.5'
1212

1313

1414
script:

examples/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ server.on('connection', function(conn) {
2626
connectionId: 1234,
2727
statusFlags: 2,
2828
characterSet: 8,
29-
capabilityFlags: 0xffffff,
29+
//capabilityFlags: 0xffffff,
30+
//capabilityFlags: -2113931265,
31+
capabilityFlags: 2181036031,
3032
authCallback: authenticate
3133
});
3234

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,14 @@ exports.__defineGetter__('createPoolClusterPromise', function () {
4646
return require('./promise.js').createPoolCluster;
4747
});
4848

49-
module.exports.Types = require('./lib/constants/types.js');
49+
exports.__defineGetter__('Types', function () {
50+
return require('./lib/constants/types.js');
51+
});
52+
53+
exports.__defineGetter__('Charsets', function () {
54+
return require('./lib/constants/charsets.js');
55+
});
56+
57+
exports.__defineGetter__('CharsetToEncoding', function () {
58+
return require('./lib/constants/charset_encodings.js');
59+
});

lib/commands/client_handshake.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var util = require('util');
33
var Command = require('./command.js');
44
var Packets = require('../packets/index.js');
55
var ClientConstants = require('../constants/client.js');
6+
var CharsetToEncoding = require('../constants/charset_encodings.js');
67

78
function ClientHandshake (clientFlags)
89
{
@@ -17,7 +18,7 @@ ClientHandshake.prototype.start = function () {
1718
};
1819

1920
ClientHandshake.prototype.sendSSLRequest = function (connection) {
20-
var sslRequest = new Packets.SSLRequest(this.clientFlags);
21+
var sslRequest = new Packets.SSLRequest(this.clientFlags, connection.config.charsetNumber);
2122
connection.writePacket(sslRequest.toPacket());
2223
};
2324

@@ -85,6 +86,7 @@ ClientHandshake.prototype.handshakeInit = function (helloPacket, connection) {
8586
flagNames(this.handshake.capabilityFlags).join(', '));
8687
}
8788
connection.serverCapabilityFlags = this.handshake.capabilityFlags;
89+
connection.serverEncoding = CharsetToEncoding[this.handshake.characterSet];
8890
connection.connectionId = this.handshake.connectionId;
8991
var serverSSLSupport = this.handshake.capabilityFlags & ClientConstants.SSL;
9092

lib/commands/command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Command.prototype.execute = function (packet, connection) {
2525
}
2626

2727
if (packet && packet.isError()) {
28-
var err = packet.asError();
28+
var err = packet.asError(connection.serverEncoding);
2929
if (this.onResult) {
3030
this.onResult(err);
3131
} else {

lib/commands/execute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Execute.prototype.buildParserFromFields = function (fields, connection) {
4040
};
4141

4242
Execute.prototype.start = function (packet, connection) {
43-
var executePacket = new Packets.Execute(this.statement.id, this.parameters);
43+
var executePacket = new Packets.Execute(this.statement.id, this.parameters, connection.config.charsetNumber);
4444
connection.writePacket(executePacket.toPacket(1));
4545
return Execute.prototype.resultsetHeader;
4646
};

lib/commands/prepare.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function Prepare (options, callback)
2121
util.inherits(Prepare, Command);
2222

2323
Prepare.prototype.start = function (packet, connection) {
24-
connection.writePacket(new Packets.PrepareStatement(this.query).toPacket(1));
24+
var cmdPacket = new Packets.PrepareStatement(this.query, connection.config.charsetNumber);
25+
connection.writePacket(cmdPacket.toPacket(1));
2526
return Prepare.prototype.prepareHeader;
2627
};
2728

lib/commands/query.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Query.prototype.start = function (packet, connection) {
3535
console.log(' Sending query command: %s', this.sql);
3636
}
3737
this._connection = connection;
38-
var cmdPacket = new Packets.Query(this.sql);
38+
var cmdPacket = new Packets.Query(this.sql, connection.config.charsetNumber);
3939
connection.writePacket(cmdPacket.toPacket(1));
4040
return Query.prototype.resultsetHeader;
4141
};
@@ -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/commands/server_handshake.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ ServerHandshake.prototype.start = function (packet, connection) {
3939
ServerHandshake.prototype.readClientReply = function (packet, connection) {
4040
// check auth here
4141
var clientHelloReply = new Packets.HandshakeResponse.fromPacket(packet);
42+
43+
// TODO check we don't have something similar already
44+
connection.clientHelloReply = clientHelloReply;
45+
4246
if (this.args.authCallback) {
4347
try {
4448
this.args.authCallback({
@@ -73,6 +77,7 @@ ServerHandshake.prototype.readClientReply = function (packet, connection) {
7377
ServerHandshake.prototype.dispatchCommands = function (packet, connection) {
7478
// command from client to server
7579
var knownCommand = true;
80+
var encoding = connection.clientHelloReply.encoding;
7681
var commandCode = packet.readInt8();
7782
switch (commandCode) {
7883
case CommandCode.QUIT:
@@ -85,7 +90,7 @@ ServerHandshake.prototype.dispatchCommands = function (packet, connection) {
8590

8691
case CommandCode.INIT_DB:
8792
if (connection.listeners('init_db').length) {
88-
var schemaName = packet.readString();
93+
var schemaName = packet.readString(encoding);
8994
connection.emit('init_db', schemaName);
9095
} else {
9196
connection.writeOk();
@@ -94,7 +99,7 @@ ServerHandshake.prototype.dispatchCommands = function (packet, connection) {
9499

95100
case CommandCode.QUERY:
96101
if (connection.listeners('query').length) {
97-
var query = packet.readString();
102+
var query = packet.readString(undefined, encoding);
98103
connection.emit('query', query);
99104
} else {
100105
connection.writeError({
@@ -107,7 +112,7 @@ ServerHandshake.prototype.dispatchCommands = function (packet, connection) {
107112
case CommandCode.FIELD_LIST:
108113
if (connection.listeners('field_list').length) {
109114
var table = packet.readNullTerminatedString();
110-
var fields = packet.readString();
115+
var fields = packet.readString(encoding);
111116
connection.emit('field_list', table, fields);
112117
} else {
113118
connection.writeError({

lib/compile_binary_parser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var vm = require('vm');
22

33
var FieldFlags = require('./constants/field_flags.js');
44
var Charsets = require('./constants/charsets.js');
5+
var CharsetToEncoding = require('./constants/charset_encodings.js');
56
var Types = require('./constants/types.js');
67
var srcEscape = require('./helpers').srcEscape;
78

@@ -98,7 +99,6 @@ function compile (fields, options, config) {
9899
function readCodeFor (field, config, options) {
99100
var supportBigNumbers = options.supportBigNumbers || config.supportBigNumbers;
100101
var bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
101-
102102
var unsigned = field.flags & FieldFlags.UNSIGNED;
103103
switch (field.columnType) {
104104
case Types.TINY:
@@ -131,11 +131,11 @@ function readCodeFor (field, config, options) {
131131
if (config.decimalNumbers) {
132132
return 'packet.parseLengthCodedFloat();';
133133
}
134-
return 'packet.readLengthCodedString();';
134+
return 'packet.readLengthCodedString("ascii");';
135135
case Types.GEOMETRY:
136136
return 'packet.parseGeometryValue();';
137137
case Types.JSON:
138-
return 'JSON.parse(packet.readLengthCodedString());';
138+
return 'JSON.parse(packet.readLengthCodedString("' + CharsetToEncoding[field.characterSet] + '"));';
139139
case Types.LONGLONG:
140140
if (!supportBigNumbers) {
141141
return unsigned ? 'packet.readInt64JSNumber();' : 'packet.readSInt64JSNumber();';
@@ -150,7 +150,7 @@ function readCodeFor (field, config, options) {
150150
if (field.characterSet == Charsets.BINARY) {
151151
return 'packet.readLengthCodedBuffer();';
152152
} else {
153-
return 'packet.readLengthCodedString();';
153+
return 'packet.readLengthCodedString("' + CharsetToEncoding[field.characterSet] + '")';
154154
}
155155
}
156156
}

0 commit comments

Comments
 (0)