Skip to content

Commit

Permalink
[CONJS-194] Charset change using parameterized query fails with "Unca…
Browse files Browse the repository at this point in the history
…ught TypeError: opts.emit is not a function"
  • Loading branch information
rusher committed Apr 26, 2022
1 parent 7281586 commit 787a0f9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/cmd/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Command extends EventEmitter {
this.emit('end');
}

parseOkPacket(packet, out, opts, info) {
parseOkPacket(packet, out, opts, connectionOpts, info) {
packet.skip(1); //skip header

const affectedRows = packet.readUnsignedLength();
Expand Down Expand Up @@ -139,7 +139,7 @@ class Command extends EventEmitter {
this.throwError(new Error("unknown charset : '" + value + "'"), info);
return;
}
opts.emit('collation', info.collation);
connectionOpts.emit('collation', info.collation);
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion lib/cmd/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Parser extends Command {
* @returns {*} null or {Resultset.readResponsePacket} in case of multi-result-set
*/
readOKPacket(packet, out, opts, info) {
const okPacket = this.parseOkPacket(packet, out, this.opts, info);
const okPacket = this.parseOkPacket(packet, out, this.opts, opts, info);
this._rows.push(okPacket);

if (info.status & ServerStatus.MORE_RESULTS_EXISTS) {
Expand Down
2 changes: 1 addition & 1 deletion lib/const/collations.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Collation {
}

static fromCharset(charset) {
return defaultCharsets[charset];
return defaultCharsets[charset == 'utf8mb3' ? 'utf8' : charset];
}

static fromIndex(index) {
Expand Down
15 changes: 15 additions & 0 deletions test/integration/test-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,21 @@ describe('Pool', () => {
});
});

it('pool charset change', async function () {
const pool = base.createPool({ connectionLimit: 1 });
try {
const query = "SET NAMES 'utf8'";

const result1 = await pool.query(query);
assert.equal(result1.constructor.name, 'OkPacket');

const result2 = await pool.query({ sql: query });
assert.equal(result2.constructor.name, 'OkPacket');
} finally {
await pool.end();
}
});

it('pool batch', function (done) {
let params = { connectionLimit: 1, resetAfterUse: false };
if (isXpand()) params['initSql'] = 'SET NAMES UTF8';
Expand Down

0 comments on commit 787a0f9

Please sign in to comment.