denodrivers/mysql

Binding multiple values

Schotsl opened this issue · 2 comments

When I want to store an UUIDv4 in my MySQL database I use UNHEX(REPLACE(?, "-", "")) to store it efficiently in an BINARY(16) column like this:

await client.execute(
  `INSERT INTO test.single (first) VALUES(UNHEX(REPLACE(?, "-", "")))`,
  [
    `b49450a5-5d16-4ff6-81f8-faf83f04f9a0`,
  ]
);

Which results in a single row being inserted, as expected! When I try to run the same query but insert into 2 BINARY(16) columns like this:

await client.execute(
  `INSERT INTO test.double (first, second) VALUES(UNHEX(REPLACE(?, "-", "")), UNHEX(REPLACE(?, "-", "")))`,
  [
    `b49450a5-5d16-4ff6-81f8-faf83f04f9a0`,
    `b49450a5-5d16-4ff6-81f8-faf83f04f9a0`
  ]
);

The driver throws an error:

error: Uncaught (in promise) Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, "-", "")))' at line 1
throw new Error(error.message);
^
at PoolConnection.nextPacket (https://deno.land/x/mysql@v2.9.0/src/connection.ts:185:13)
at async PoolConnection.execute (https://deno.land/x/mysql@v2.9.0/src/connection.ts:268:21)
at async https://deno.land/x/mysql@v2.9.0/src/client.ts:97:14
at async Client.useConnection (https://deno.land/x/mysql@v2.9.0/src/client.ts:107:14)
at async Client.execute (https://deno.land/x/mysql@v2.9.0/src/client.ts:96:12)
at async file:///usr/local/var/www/Deno/index.ts:28:1

Now I would assume there is something wrong with my query but when I run the same query but instead of binding the variables I just hard code them in the string like this:

await client.execute(
  `INSERT INTO test.double (first, second) VALUES(UNHEX(REPLACE("b49450a5-5d16-4ff6-81f8-faf83f04f9a0", "-", "")), UNHEX(REPLACE("b49450a5-5d16-4ff6-81f8-faf83f04f9a0", "-", "")))`,
);

It inserts just fine, what am I doing wrong? If need be I can provide credentials to my database but a friend of mine running a completely different database has the same issue so I think it isn't specific to my MySQL configuration.

These are the tables I'm running the queries on for debugging purposes:

CREATE TABLE test.double (
 	first binary(16) NOT NULL,
	second binary(16) NOT NULL,
	PRIMARY KEY (first)
)

CREATE TABLE test.single (
 	first binary(16) NOT NULL,
	PRIMARY KEY (first)
)

This is because of an open issue with a dependency: manyuanrong/sql-builder#9.

The scope of the issue goes beyond "multiple values" -- any ? that is not at the end, will not bind reliably. (But yes, this is an issue with the dependency)