sidorares / sidorares/node-mysql2
[Question] Automatic unescaping for column names and tables
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 4.4k
- Forks
- 680
- Avg merge
- 9h 7m
- Merged PRs (30d)
- 59
Description
I've recently posted a bug in the base library for mysql, but I'm not sure if this library shares the same problem:
https://github.com/mysqljs/mysql/issues/2391
When using mysql2 as our library, column names are incorrectly unescaped and reserved keywords conflict with column names like from, to, default, etc.
Am I doing something wrong when attempting to unescape the names in the queries?
Pasting the content of the original issue here for convenience:
Consider the following table:
CREATE TABLE example (
`from` DATE NOT NULL
);
fromis a reserved SQL keyword. If you try to insert a row into that table using named columns, thefromcolumn is not escaped, which causes a syntax error. I haven't found a way to force the library to not remove escapes for this column name even after forcing it to insert a raw value:
// this query unescapes "`from`" into "from"
connection.query(
"INSERT INTO example (`from`) VALUES (?)",
["2020-01-01"]
);
// this query does the same
connection.query(
"INSERT INTO example (?) VALUES (?)",
[mysql.raw('`from`'), "2020-01-01"]
);
This also affects exports from MySQL that contain backtick-escaped column names that are reserved keywords when being passed to the
queryfunction after reading from a file.
Is there something I'm missing? Is there a way to force identifiers to always be escaped in the final query?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two connection.query examples using the reserved column name from, then compare the behavior with the linked mysqljs/mysql issue. Determine whether mysql2 provides a way to preserve identifier escaping and document the confirmed behavior or required change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mysql, node.js, typescript
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100