Skip to content

Commit 67c1993

Browse files
committed
Throw if invalid charset is specified for collation
1 parent 6aceb79 commit 67c1993

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/ConnectionConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ ConnectionConfig.getDefaultFlags = function(options) {
9090
ConnectionConfig.getCharsetNumber = function getCharsetNumber(charset, collation) {
9191
var num;
9292
if (collation) {
93+
if (charset && (collation.split('_')[0].toUpperCase() !== charset.toUpperCase())) {
94+
throw new TypeError('Invalid charset \'' + charset +
95+
'\' specified with collation \'' + collation + '\'');
96+
}
9397
num = this.getCollationNumber(collation);
9498
} else if (charset) {
9599
charset = Charsets[charset.toUpperCase()] || charset;

test/unit/test-ConnectionConfig.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ test('ConnectionConfig#Constructor', {
6767
assert.equal(config.charsetNumber, Collations.UTF8_GENERAL_CI);
6868
},
6969

70+
'throws if incompatible charset and collation are specified': function() {
71+
var error;
72+
try {
73+
var config = new ConnectionConfig({
74+
charset: 'latin1',
75+
collation: 'UTF8MB4_BIN'
76+
});
77+
} catch (err) {
78+
error = err;
79+
}
80+
assert.ok(error);
81+
assert.equal(error.name, 'TypeError');
82+
assert.equal(error.message, 'Invalid charset \'latin1\' specified with collation \'UTF8MB4_BIN\'');
83+
}
84+
7085
});
7186

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

0 commit comments

Comments
 (0)