-
-
Notifications
You must be signed in to change notification settings - Fork 619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Error: Incorrect arguments to mysqld_stmt_execute" when a comment includes a parameter #1521
Comments
Hi @ferrybig this is known problem, solution is worked on in #1407 pre 8.0.22 mysql server does not show this problem and happy to accept input parameters sent as a type different from what prepared statement expects some more context here - #1239 (comment) |
some difficulty in solving this is that we can't completely rely on the type hints for parameters ( we can trust the results types though ). Sometimes mysql just does not know what type is expected, example: |
That's a good point, I kind of overlooked statements where there is no underlying explicit column data type. Would the custom hinting mechanism work on a per-statement or a per-connection basis? |
@sidorares We are running MySQL 5.7.32, so I'm not sure if you picked the correct duplicate target. Type inference seems to work correctly with the space after the : in the comment, it is only when an executed statement contains an : followed by name that matches a bound parameter name that causes this bug (not sure what happens when the comment contains a name that is not defined) EDIT: This is buggy: const sql = `
SELECT
-- The following code return the value of the :input parameter as a single row
:input as input
FROM
DUAL
`; This is NOT buggy: const sql = `
SELECT
-- The following code return the value of the : input parameter as a single row
:input as input
FROM
DUAL
`; |
might be a wrong duplicate indeed are you using |
@ruiquelhas I was thinking per-execute if I understand your question correctly |
Yes, |
oh ok I see . So the issue is that placeholder gets replaced in both comment and query text |
const sql = `
SELECT
-- The following code return the value of the :input parameter as a single row
:input as input
FROM
DUAL
`;
conn.query(sql, [{ input: 123 }]) becomes const sql = `
SELECT
-- The following code return the value of the ? parameter as a single row
? as input
FROM
DUAL
`;
conn.query(sql, [123, 123]) because of 2 |
I need to double check why this error from the server, I was sure we have check on the client before even sending execute command I expect it to return error if number of parameters does not match response for matching |
Looking at what you wrote already for a parser, you seems to have though of string quotations, but not of comments. (probably because comments can be parsed with just looking at 1 char, while support for This also seems related to named-placeholders#9 Does this issue needs to be moved to https://github.com/mysqljs/named-placeholders because the root cause of the problem seems to be in that project? |
Here's a quick and easy workaround in the meantime. It only filters out comments that start const sqlWithoutComments = sql.split('\n').filter(e => !e.trim().startsWith('-- ')).join('\n');
const [resultSet] = await db.execute(sqlWithoutComments, params); EDIT: After reading about the format of mysql comments, my code should be updated where |
@ferrybig yes, probably better to move to https://github.com/mysqljs/named-placeholders |
I'll close this issue for now, the root cause was identified, I'd classify it as not a bug but "there is a room for improvement" and "maybe better documentation would help". Happy to see comments parsing support PR in named-paceholders repo |
Description
When any SQL query includes a command containing the syntax for a parameter and is passed to the execute function, the library gives an SQL error. I would expect the SQL parser in the library to properly identify that it is inside an command expression and handle the situation gracefully.
Reproducing:
Run the following code:
Expected
I get a result set back which contains
[ { input: 1234 } ]
Actual:
I receive the following error
Use case
In my use case, I get complex queries from third parties, which sometimes includes comments using
--
, or/* */
or//
. The MySQL2 library just refuses to run the queries with comments containing parameter strings with the above error, needing me to use workaroundsCurrent workaround
For every SQL query that I get from my third party, I have to inspect every comment and add a space after
:
if text follows this symbol. Missing any means I get a production crashThe text was updated successfully, but these errors were encountered: