mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.22k stars 2.53k forks source link

ER_PARSE_ERROR For Query With Default Formatting While Bulk Insert #2485

Closed sumitac007 closed 2 years ago

sumitac007 commented 3 years ago

I am using a connection pool that is initialized at the beginning in class. There are two functions in the class that execute the query by calling method connection.query().

Function 1 uses a custom queryFormat as below. It is used to execute queries with bind params with ':'

connection.config.queryFormat = function (query, values) {
          if (!values) {
            return query;
          }
          return query.replace(/\:(\w+)/g, function (txt, key) {
            if (values.hasOwnProperty(key)) {
              return this.escape(values[key]);
            }
            return txt;
          }.bind(this));
        };

Function 2 uses the default query format with questions marks (?). It is used for executing bulk insert queries.

Both functions are using same connection pool. Both functions are calling getConnection() method for getting new connection and releasing the connection after query is done.

When calling function 2 after function 1, though queryFormat is not set in Function 2 it is getting attached with the connection that is instantiated in function 2 and generating error as ER_PARSE_ERROR .

When only function 2 is called it works fine.

Please note I am doing bulk insert and passing array of arrays as bounded by an array as param. For example below query

INSERT INTO
  Employee (
    id,
   name,
    salary
  )
VALUES
  ?

My understanding when default query format is overwritten it should apply only to that connection. Looks like it is getting attached with all connections from the pool or if the connection is used after releasing the queryFormat is preserved for that connection.

dougwilson commented 2 years ago

Hello, you are correct, it only changes that one connection; but remember that in a pool the same connection will be recycled, so you should be careful altering the connection that you then return to the pool, as the changes will persist when it is checked out from the pool again.