Skip to content
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

Parameters used in LIMIT and OFFSET in MySQL get quoted and quoted wrong #6778

Open
sma27 opened this issue Feb 10, 2025 · 2 comments
Open

Comments

@sma27
Copy link

sma27 commented Feb 10, 2025

Bug Report

Q A
Version 4.2.2

pdo_mysql driver

Summary

Double quotes parameters in LIMIT and OFFSET

Current behavior

MySQL query like the following:

$query = "
SELECT *
FROM my_table
LIMIT :limit OFFSET :offset";

$qb->executeQuery($query, ['limit' => 10, 'offset' => 2]); 

Throws an exception. It appears the SQL is being run as:

SELECT *
FROM my_table
LIMIT ''10'' OFFSET ''2''

My temporary fix was not to use parameters in the limit or offset, but shouldn't this be supported?

Expected behavior

The parameters passed in to the executeQuery functions are int parameters. I wouldn't expect them to be treated as strings.

SELECT *
FROM my_table
LIMIT 10 OFFSET 2

This is what I'd expect the SQL to be.

@sma27
Copy link
Author

sma27 commented Feb 10, 2025

It works if I pass in a types array, but it should be able to detect this in my opinion.

$qb->executeQuery($query, ['limit' => 10, 'offset' => 2], ['limit' => \Doctrine\DBAL\ParameterType::INTEGER, 'offset' => \Doctrine\DBAL\ParameterType::INTEGER]);

@morozov
Copy link
Member

morozov commented Feb 19, 2025

The default parameter type is string, that's why the values get quoted.

it should be able to detect this in my opinion.

No. The binding type shouldn't be inferred from the input, it should be defined by the query and be static. The default is string.

If you want to build the LIMIT query manually, you should use parameter types. Otherwise, you may consider using QueryBuilder#setMaxResults() and QueryBuilder#setFirstResult() instead:

$qb
    ->setMaxResults(10)
    ->setFirstResult(2)
    ->executeQuery();

Although, there is a known downside that those parameters will be rendered as literal values, not parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants