mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
366 stars 91 forks source link

Using `SET NAMES` in 3.0.0 fails with `TypeError` #199

Closed Supinic closed 2 years ago

Supinic commented 2 years ago

When using the SET NAMES statement inside of a pool.query({ sql: query }) call, the execution fails with TypeError opts.emit is not a function.

Consider the following script:

(async () => {
    const { strictEqual } = require("assert");
    const Maria = require("mariadb");
    const pool = Maria.createPool({
        user: "user",
        password: "password",
        host: "host"
    });

    const query = "SET NAMES 'utf8'";

    // Using string directly works as expected
    const result1 = await pool.query(query); 
    strictEqual(result1.constructor.name, "OkPacket");
    console.log("result 1 ok");

    // Using { sql: string } fails
    const result2 = await pool.query({ sql: query }); 
    strictEqual(result2.constructor.name, "OkPacket");
    console.log("result 2 ok");
})();

In mariadb version 2.5.2, the snippet passes, whereas in 3.0.0, the following error is thrown when attempting to initialize result2:

Uncaught TypeError: opts.emit is not a function
    at Query.parseOkPacket (VM195 command.js:142:24)
    at Query.readOKPacket (VM214 parser.js:108:27)
    at Query.readResponsePacket (VM214 parser.js:46:21)
    at PacketInputStream.receivePacketBasic (VM164 packet-input-stream.js:76:9)
    at PacketInputStream.onData (VM164 packet-input-stream.js:139:20)
    at Socket.emit (node:events:527:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

Using other meta SQL commands (such as USE database) seems to cause no issues. Other arguments for SET NAMES fail similarly to utf8mb4.

Tested on NodeJS versions 12.22.9, 14.15.0, 16.14.2, 18.0.0, with identical results.

rusher commented 2 years ago

Thanks for clear description ! Created a task for that (https://jira.mariadb.org/browse/CONJS-194) This will be corrected in 3.0.1 release