Skip to content

Commit fd28786

Browse files
committed
Accept MySQL charset (like UTF8 or UTF8MB4) in charset option
closes #808
1 parent a83cc76 commit fd28786

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ you spot any mistakes.
66

77
## HEAD
88

9+
* Accept MySQL charset (like `UTF8` or `UTF8MB4`) in `charset` option #808
910
* Clone connection config for new pool connections
1011
* Default `connectTimeout` to 2 minutes
1112
* Throw on unknown SSL profile name #817

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ When establishing a connection, you can set the following options:
141141
* `user`: The MySQL user to authenticate as.
142142
* `password`: The password of that MySQL user.
143143
* `database`: Name of the database to use for this connection (Optional).
144-
* `charset`: The charset for the connection. (Default: `'UTF8_GENERAL_CI'`)
144+
* `charset`: The charset for the connection. This is called "collation" in the SQL-level
145+
of MySQL (like `utf8_general_ci`). If a SQL-level charset is specified (like `utf8mb4`)
146+
then the default collation for that charset is used. (Default: `'UTF8_GENERAL_CI'`)
145147
* `timezone`: The timezone used to store local dates. (Default: `'local'`)
146148
* `connectTimeout`: The milliseconds before a timeout occurs during the initial connection
147149
to the MySQL server. (Default: 2 minutes)

lib/protocol/constants/charsets.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,45 @@ exports.UTF8MB4_CROATIAN_MYSQL561_CI = 245;
218218
exports.UTF8MB4_UNICODE_520_CI = 246;
219219
exports.UTF8MB4_VIETNAMESE_CI = 247;
220220
exports.UTF8_GENERAL50_CI = 253;
221+
222+
// short aliases
223+
exports.ARMSCII8 = exports.ARMSCII8_GENERAL_CI;
224+
exports.ASCII = exports.ASCII_GENERAL_CI;
225+
exports.BIG5 = exports.BIG5_CHINESE_CI;
226+
exports.BINARY = exports.BINARY;
227+
exports.CP1250 = exports.CP1250_GENERAL_CI;
228+
exports.CP1251 = exports.CP1251_GENERAL_CI;
229+
exports.CP1256 = exports.CP1256_GENERAL_CI;
230+
exports.CP1257 = exports.CP1257_GENERAL_CI;
231+
exports.CP866 = exports.CP866_GENERAL_CI;
232+
exports.CP850 = exports.CP850_GENERAL_CI;
233+
exports.CP852 = exports.CP852_GENERAL_CI;
234+
exports.CP932 = exports.CP932_JAPANESE_CI;
235+
exports.DEC8 = exports.DEC8_SWEDISH_CI;
236+
exports.EUCJPMS = exports.EUCJPMS_JAPANESE_CI;
237+
exports.EUCKR = exports.EUCKR_KOREAN_CI;
238+
exports.GB2312 = exports.GB2312_CHINESE_CI;
239+
exports.GBK = exports.GBK_CHINESE_CI;
240+
exports.GEOSTD8 = exports.GEOSTD8_GENERAL_CI;
241+
exports.GREEK = exports.GREEK_GENERAL_CI;
242+
exports.HEBREW = exports.HEBREW_GENERAL_CI;
243+
exports.HP8 = exports.HP8_ENGLISH_CI;
244+
exports.KEYBCS2 = exports.KEYBCS2_GENERAL_CI;
245+
exports.KOI8R = exports.KOI8R_GENERAL_CI;
246+
exports.KOI8U = exports.KOI8U_GENERAL_CI;
247+
exports.LATIN1 = exports.LATIN1_SWEDISH_CI;
248+
exports.LATIN2 = exports.LATIN2_GENERAL_CI;
249+
exports.LATIN5 = exports.LATIN5_TURKISH_CI;
250+
exports.LATIN7 = exports.LATIN7_GENERAL_CI;
251+
exports.MACCE = exports.MACCE_GENERAL_CI;
252+
exports.MACROMAN = exports.MACROMAN_GENERAL_CI;
253+
exports.SJIS = exports.SJIS_JAPANESE_CI;
254+
exports.SWE7 = exports.SWE7_SWEDISH_CI;
255+
exports.TIS620 = exports.TIS620_THAI_CI;
256+
exports.UCS2 = exports.UCS2_GENERAL_CI;
257+
exports.UJIS = exports.UJIS_JAPANESE_CI;
258+
exports.UTF16 = exports.UTF16_GENERAL_CI;
259+
exports.UTF16LE = exports.UTF16LE_GENERAL_CI;
260+
exports.UTF8 = exports.UTF8_GENERAL_CI;
261+
exports.UTF8MB4 = exports.UTF8MB4_GENERAL_CI;
262+
exports.UTF32 = exports.UTF32_GENERAL_CI;

test/unit/test-ConnectionConfig.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,33 @@ test('ConnectionConfig#Constructor', {
2626
assert.equal(config.debug, true);
2727
assert.equal(config.charsetNumber, Charsets.BIG5_CHINESE_CI);
2828
},
29+
});
30+
31+
test('ConnectionConfig#Constructor.charset', {
32+
'accepts charset name': function() {
33+
var config = new ConnectionConfig({
34+
charset: 'LATIN1_SWEDISH_CI',
35+
});
2936

30-
'allows case-insensitive charset name': function() {
37+
assert.equal(config.charsetNumber, Charsets.LATIN1_SWEDISH_CI);
38+
},
39+
40+
'accepts case-insensitive charset name': function() {
3141
var config = new ConnectionConfig({
3242
charset: 'big5_chinese_ci',
3343
});
3444

3545
assert.equal(config.charsetNumber, Charsets.BIG5_CHINESE_CI);
3646
},
3747

48+
'accepts short charset name': function() {
49+
var config = new ConnectionConfig({
50+
charset: 'UTF8MB4',
51+
});
52+
53+
assert.equal(config.charsetNumber, Charsets.UTF8MB4_GENERAL_CI);
54+
},
55+
3856
'throws on unknown charset': function() {
3957
var error;
4058

@@ -50,6 +68,16 @@ test('ConnectionConfig#Constructor', {
5068
assert.equal(error.name, 'TypeError');
5169
assert.equal(error.message, 'Unknown charset \'INVALID_CHARSET\'');
5270
},
71+
72+
'all charsets should have short name': function() {
73+
var charsets = Object.keys(Charsets);
74+
75+
for (var i = 0; i < charsets.length; i++) {
76+
var charset = charsets[i];
77+
assert.ok(Charsets[charset]);
78+
assert.ok(Charsets[charset.split('_')[0]]);
79+
}
80+
},
5381
});
5482

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

0 commit comments

Comments
 (0)