Skip to content

Commit 6aceb79

Browse files
committed
Allow both charset and collation as config options, #807
1 parent f2d6c04 commit 6aceb79

File tree

4 files changed

+86
-18
lines changed

4 files changed

+86
-18
lines changed

lib/ConnectionConfig.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var urlParse = require('url').parse;
22
var ClientConstants = require('./protocol/constants/client');
3+
var Collations = require('./protocol/constants/collations');
34
var Charsets = require('./protocol/constants/charsets');
45

56
module.exports = ConnectionConfig;
@@ -30,7 +31,7 @@ function ConnectionConfig(options) {
3031
this.queryFormat = options.queryFormat;
3132
this.pool = options.pool || undefined;
3233
this.ssl = options.ssl || undefined;
33-
this.multipleStatements = options.multipleStatements || false;
34+
this.multipleStatements = options.multipleStatements || false;
3435
this.typeCast = (options.typeCast === undefined)
3536
? true
3637
: options.typeCast;
@@ -43,9 +44,9 @@ function ConnectionConfig(options) {
4344
}
4445

4546
this.maxPacketSize = 0;
46-
this.charsetNumber = (options.charset)
47-
? ConnectionConfig.getCharsetNumber(options.charset)
48-
: options.charsetNumber||Charsets.UTF8_GENERAL_CI;
47+
this.charsetNumber = (options.charset || options.collation)
48+
? ConnectionConfig.getCharsetNumber(options.charset, options.collation)
49+
: options.charsetNumber||Collations.UTF8_GENERAL_CI;
4950

5051
this.clientFlags = ConnectionConfig.mergeFlags(ConnectionConfig.getDefaultFlags(options),
5152
options.flags || '');
@@ -86,13 +87,23 @@ ConnectionConfig.getDefaultFlags = function(options) {
8687
return defaultFlags;
8788
};
8889

89-
ConnectionConfig.getCharsetNumber = function getCharsetNumber(charset) {
90-
var num = Charsets[charset.toUpperCase()];
90+
ConnectionConfig.getCharsetNumber = function getCharsetNumber(charset, collation) {
91+
var num;
92+
if (collation) {
93+
num = this.getCollationNumber(collation);
94+
} else if (charset) {
95+
charset = Charsets[charset.toUpperCase()] || charset;
96+
num = this.getCollationNumber(charset);
97+
}
98+
return num;
99+
};
91100

101+
ConnectionConfig.getCollationNumber = function getCollationNumber(collation) {
102+
var num = Collations[collation.toUpperCase()];
103+
// Check the Collations first, for backward compat.
92104
if (num === undefined) {
93-
throw new TypeError('Unknown charset \'' + charset + '\'');
105+
throw new TypeError('Unknown collation \'' + collation + '\'');
94106
}
95-
96107
return num;
97108
};
98109

lib/protocol/constants/charsets.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
exports.BIG5 = 'BIG5_CHINESE_CI';
2+
exports.DEC8 = 'DEC8_SWEDISH_CI';
3+
exports.CP850 = 'CP850_GENERAL_CI';
4+
exports.HP8 = 'HP8_ENGLISH_CI';
5+
exports.KOI8R = 'KOI8R_GENERAL_CI';
6+
exports.LATIN1 = 'LATIN1_SWEDISH_CI';
7+
exports.LATIN2 = 'LATIN2_GENERAL_CI';
8+
exports.SWE7 = 'SWE7_SWEDISH_CI';
9+
exports.ASCII = 'ASCII_GENERAL_CI';
10+
exports.UJIS = 'UJIS_JAPANESE_CI';
11+
exports.SJIS = 'SJIS_JAPANESE_CI';
12+
exports.HEBREW = 'HEBREW_GENERAL_CI';
13+
exports.TIS620 = 'TIS620_THAI_CI';
14+
exports.EUCKR = 'EUCKR_KOREAN_CI';
15+
exports.KOI8U = 'KOI8U_GENERAL_CI';
16+
exports.GB2312 = 'GB2312_CHINESE_CI';
17+
exports.GREEK = 'GREEK_GENERAL_CI';
18+
exports.CP1250 = 'CP1250_GENERAL_CI';
19+
exports.GBK = 'GBK_CHINESE_CI';
20+
exports.LATIN5 = 'LATIN5_TURKISH_CI';
21+
exports.ARMSCII8 = 'ARMSCII8_GENERAL_CI';
22+
exports.UTF8 = 'UTF8_GENERAL_CI';
23+
exports.UCS2 = 'UCS2_GENERAL_CI';
24+
exports.CP866 = 'CP866_GENERAL_CI';
25+
exports.KEYBCS2 = 'KEYBCS2_GENERAL_CI';
26+
exports.MACCE = 'MACCE_GENERAL_CI';
27+
exports.MACROMAN = 'MACROMAN_GENERAL_CI';
28+
exports.CP852 = 'CP852_GENERAL_CI';
29+
exports.LATIN7 = 'LATIN7_GENERAL_CI';
30+
exports.UTF8MB4 = 'UTF8MB4_GENERAL_CI';
31+
exports.CP1251 = 'CP1251_GENERAL_CI';
32+
exports.UTF16 = 'UTF16_GENERAL_CI';
33+
exports.UTF16LE = 'UTF16LE_GENERAL_CI';
34+
exports.CP1256 = 'CP1256_GENERAL_CI';
35+
exports.CP1257 = 'CP1257_GENERAL_CI';
36+
exports.UTF32 = 'UTF32_GENERAL_CI';
37+
exports.BINARY = 'BINARY';
38+
exports.GEOSTD8 = 'GEOSTD8_GENERAL_CI';
39+
exports.CP932 = 'CP932_JAPANESE_CI';
40+
exports.EUCJPMS = 'EUCJPMS_JAPANESE_CI';

lib/protocol/packets/RowDataPacket.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var Types = require('../constants/types');
2-
var Charsets = require('../constants/charsets');
2+
var Collations = require('../constants/collations');
33
var Field = require('./Field');
44
var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53);
55

@@ -22,7 +22,7 @@ RowDataPacket.prototype.parse = function(parser, fieldPackets, typeCast, nestTab
2222
} else {
2323
value = (typeCast)
2424
? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings)
25-
: ( (fieldPacket.charsetNr === Charsets.BINARY)
25+
: ( (fieldPacket.charsetNr === Collations.BINARY)
2626
? parser.parseLengthCodedBuffer()
2727
: parser.parseLengthCodedString() );
2828
}
@@ -97,7 +97,7 @@ RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBig
9797
case Types.MEDIUM_BLOB:
9898
case Types.LONG_BLOB:
9999
case Types.BLOB:
100-
return (field.charsetNr === Charsets.BINARY)
100+
return (field.charsetNr === Collations.BINARY)
101101
? parser.parseLengthCodedBuffer()
102102
: parser.parseLengthCodedString();
103103
case Types.GEOMETRY:

test/unit/test-ConnectionConfig.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var common = require('../common');
22
var test = require('utest');
33
var assert = require('assert');
4-
var Charsets = require(common.lib + '/protocol/constants/charsets');
4+
var Collations = require(common.lib + '/protocol/constants/collations');
55
var ConnectionConfig = require(common.lib + '/ConnectionConfig');
66

77
test('ConnectionConfig#Constructor', {
@@ -17,22 +17,22 @@ test('ConnectionConfig#Constructor', {
1717
},
1818

1919
'allows additional options via url query': function() {
20-
var url = 'mysql://myhost/mydb?debug=true&charset=BIG5_CHINESE_CI';
20+
var url = 'mysql://myhost/mydb?debug=true&collation=BIG5_CHINESE_CI';
2121
var config = new ConnectionConfig(url);
2222

2323
assert.equal(config.host, 'myhost');
2424
assert.equal(config.port, 3306);
2525
assert.equal(config.database, 'mydb');
2626
assert.equal(config.debug, true);
27-
assert.equal(config.charsetNumber, Charsets.BIG5_CHINESE_CI);
27+
assert.equal(config.charsetNumber, Collations.BIG5_CHINESE_CI);
2828
},
2929

30-
'allows case-insensitive charset name': function() {
30+
'allows case-insensitive collation name': function() {
3131
var config = new ConnectionConfig({
32-
charset: 'big5_chinese_ci',
32+
collation: 'big5_chinese_ci',
3333
});
3434

35-
assert.equal(config.charsetNumber, Charsets.BIG5_CHINESE_CI);
35+
assert.equal(config.charsetNumber, Collations.BIG5_CHINESE_CI);
3636
},
3737

3838
'throws on unknown charset': function() {
@@ -48,8 +48,25 @@ test('ConnectionConfig#Constructor', {
4848

4949
assert.ok(error);
5050
assert.equal(error.name, 'TypeError');
51-
assert.equal(error.message, 'Unknown charset \'INVALID_CHARSET\'');
51+
assert.equal(error.message, 'Unknown collation \'INVALID_CHARSET\'');
52+
},
53+
54+
'allows charset for collation for backward compatibility': function() {
55+
var config = new ConnectionConfig({
56+
charset: 'big5_chinese_ci',
57+
});
58+
59+
assert.equal(config.charsetNumber, Collations.BIG5_CHINESE_CI);
5260
},
61+
62+
'uses the default charset collation': function() {
63+
var config = new ConnectionConfig({
64+
charset: 'utf8',
65+
});
66+
67+
assert.equal(config.charsetNumber, Collations.UTF8_GENERAL_CI);
68+
},
69+
5370
});
5471

5572
test('ConnectionConfig#Constructor.connectTimeout', {

0 commit comments

Comments
 (0)