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

Question Mark in Field Alias Issue #6774

Open
sma27 opened this issue Feb 6, 2025 · 3 comments
Open

Question Mark in Field Alias Issue #6774

sma27 opened this issue Feb 6, 2025 · 3 comments

Comments

@sma27
Copy link

sma27 commented Feb 6, 2025

Bug Report

Q A
Version 4.2.2

pdo_mysql driver

Summary

Positional parameter at index 0 does not have a bound value error for question marks used within quotes.

Current behavior

MySQL query like the following:
SELECT my_question AS 'Why do you like turtles?'
FROM...

Causes an error: "Positional parameter at index 0 does not have a bound value." despite being enclosed in single quotes.

Modifying the query as follows:
SELECT my_question AS 'Why do you like turtles??'
FROM...

Returns two question marks in the result instead of one. As far as I can tell there is no way to use question marks in field aliases.

Expected behavior

I would expect the question mark to be treated as a literal within single quotes. Or, if that is not the case, I'd expect the escaped question mark to return a single question mark.

P.S. I am not using the query builder to perform this query. The code and query ran fine in Laminas DB. I am currently porting to Doctrine DBAL.

@sma27
Copy link
Author

sma27 commented Feb 7, 2025

It looks like the problem is actually the apostrophe mixed with a question mark.

SELECT my_question AS 'Why you''d like turtles?'
FROM...

Seems to work

But

SELECT my_question AS 'Why you\'d like turtles?'
FROM...

Causes an exception, shouldn't either work?

The problem seems to be caused with the combination of "\'" and "?" if I remove the question mark the problem also goes away. It must trip some strange case when parsed.

@morozov
Copy link
Member

morozov commented Feb 19, 2025

The SQL parser supports MySQL and ANSI literal escaping syntaxes. It looks like MySQL (the database) supports both of them:

SELECT ''''; -- ANSI
SELECT '\''; -- MySQL

Both of the above queries yield a single quote on MySQL.

But the parser only supports the MySQL syntax with MySQL. A proper solution would be to rework the parser to support both syntaxes on MySQL. A workaround would be to use the MySQL syntax with MySQL.

If you need to run the same queries on various platforms and use the ANSI syntax, you can use a custom MySQL platform class that overrides createSQLParser() until this issue is fixed.

@morozov
Copy link
Member

morozov commented Feb 19, 2025

Could you try reproducing the issue with the ParserTest by adding a new element to this data provider?

public static function mySQLParametersProvider(): iterable
{

The following doesn't seem to fail the test:

        yield 'ANSI syntax' => [
            <<<'SQL'
SELECT my_question AS 'Why you\'d like turtles?'
SQL
            ,
            <<<'SQL'
SELECT my_question AS 'Why you\'d like turtles?'
SQL
            ,
        ];

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

No branches or pull requests

2 participants