Skip to content

Commit 4a2517b

Browse files
committed
Handle Connection#config state for changeUser
This patch makes sure that the Connection#config state is only mutated once the ChangeUser sequence is starting. Before that the old state is assumed.
1 parent 151b1b1 commit 4a2517b

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

lib/Config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Config(options) {
2222

2323
this.maxPacketSize = 0;
2424
this.charsetNumber = (options.charset)
25-
? Charsets[options.charset]
25+
? Config.getCharsetNumber(options.charset)
2626
: Charsets.UTF8_GENERAL_CI;
2727

2828
this.clientFlags =
@@ -45,6 +45,10 @@ function Config(options) {
4545
}
4646
}
4747

48+
Config.getCharsetNumber = function(charset) {
49+
return Charsets[charset];
50+
};
51+
4852
Config.parseUrl = function(url) {
4953
url = urlParse(url, true);
5054

lib/Connection.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ Connection.prototype.connect = function(cb) {
3636
this._protocol.handshake(cb);
3737
};
3838

39-
Connection.prototype.changeUser = function(config, cb){
40-
if (typeof config === 'function') {
41-
cb = config;
42-
config = this.config;
43-
} else {
44-
var newConfig = {};
45-
46-
for (var prop in this.config) {
47-
newConfig[prop] = this.config[prop];
48-
}
49-
50-
for (var prop in config) {
51-
newConfig[prop] = config[prop];
52-
}
53-
54-
config = new Config(newConfig);
39+
Connection.prototype.changeUser = function(options, cb){
40+
if (typeof options === 'function') {
41+
cb = options;
42+
options = {};
5543
}
5644

57-
return this._protocol.changeUser(config, cb);
45+
var charsetNumber = (options.charset)
46+
? Config.getCharsetNumber(options.charset)
47+
: this.config.charsetNumber;
48+
49+
return this._protocol.changeUser({
50+
user : options.user || this.config.user,
51+
password : options.password || this.config.password,
52+
database : options.database || this.config.database,
53+
charsetNumber : charsetNumber,
54+
currentConfig : this.config,
55+
}, cb);
5856
};
5957

6058
Connection.prototype.query = function(sql, values, cb) {

lib/protocol/sequences/ChangeUser.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,37 @@ var Auth = require('../Auth');
55

66
module.exports = ChangeUser;
77
Util.inherits(ChangeUser, Sequence);
8-
function ChangeUser(config, callback) {
8+
function ChangeUser(options, callback) {
99
Sequence.call(this, callback);
1010

11-
this._config = config;
11+
this._user = options.user;
12+
this._password = options.password;
13+
this._database = options.database;
14+
this._charsetNumber = options.charsetNumber;
15+
this._currentConfig = options.currentConfig;
1216
}
1317

1418
ChangeUser.prototype.start = function(handshakeInitializationPacket) {
1519
var scrambleBuff = handshakeInitializationPacket.scrambleBuff();
16-
scrambleBuff = Auth.token(this._config.password, scrambleBuff);
20+
scrambleBuff = Auth.token(this._password, scrambleBuff);
1721

1822
var packet = new Packets.ComChangeUserPacket({
19-
user : this._config.user,
23+
user : this._user,
2024
scrambleBuff : scrambleBuff,
21-
database : this._config.database,
22-
charsetNumber : this._config.charsetNumber,
25+
database : this._database,
26+
charsetNumber : this._charsetNumber,
2327
});
2428

25-
this._emitPacket(packet);
29+
this._currentConfig.user = this._user;
30+
this._currentConfig.password = this._password;
31+
this._currentConfig.database = this._database;
32+
this._currentConfig.charsetNumber = this._charsetNumber;
33+
34+
this.emit('packet', packet);
35+
};
36+
37+
ChangeUser.prototype['OkPacket'] = function(packet) {
38+
Sequence.prototype.OkPacket.call(this, packet);
2639
};
2740

2841
ChangeUser.prototype['ErrorPacket'] = function(packet) {

test/integration/test-change-user.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ connection.query('select database() as db', function(err, results) {
1111
if (err) throw err;
1212

1313
initialDb = results[0].db;
14+
assert.equal(connection.config.database, null);
1415
});
1516

1617
connection.changeUser({database: common.testDatabase});
@@ -20,6 +21,7 @@ connection.query('select database() as db', function(err, results){
2021
if (err) throw err;
2122

2223
finalDb = results[0].db;
24+
assert.equal(connection.config.database, finalDb);
2325
});
2426

2527
connection.end();

0 commit comments

Comments
 (0)