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

When building queries, DBAL doesn't consider reserved keywords #3152

Closed
morozov opened this issue May 14, 2018 · 7 comments
Closed

When building queries, DBAL doesn't consider reserved keywords #3152

morozov opened this issue May 14, 2018 · 7 comments

Comments

@morozov
Copy link
Member

morozov commented May 14, 2018

Bug Report

When building queries, DBAL doesn't consider reserved keywords

Q A
BC Break no
Version 2.7.1

Summary

Assuming 'FILE' is a reserved keyword on the Oracle platform, I can create a table with the 'file' column using the schema manager but I can't execute an INSERT or UPDATE statement using the Connection API. I also cannot SELECT from the table using QueryBuilder.

How to reproduce

$conn = \Doctrine\DBAL\DriverManager::getConnection([
    'driver' => 'oci8',
    'host' => 'localhost',
    'user' => 'system',
    'password' => 'oracle',
    'dbname' => 'XE',
]);

$table = new Doctrine\DBAL\Schema\Table('test_table');
$table->addColumn('file', 'string');

$conn->getSchemaManager()
    ->createTable($table);

$conn->insert('test_table', ['file' => 'foo']);

// ORA-00928: missing SELECT keyword

$conn->createQueryBuilder()
    ->select('file')
    ->from('test_table')
    ->execute()
    ->fetchAll();

// ORA-00936: missing expression

Expected behavior

The keywords are handled consistently by the Schema Manager and Query Builder.

@morozov
Copy link
Member Author

morozov commented May 14, 2018

The same is reproducible with 'select' as the column name on other platforms.

@morozov
Copy link
Member Author

morozov commented May 15, 2018

@Ocramius we can use this issue as a starting point for discussion of whether/how identifiers should be quoted in the DBAL.

@leoqbc
Copy link

leoqbc commented May 22, 2018

Hi guys I'm facing the same problem in MYSQL, I have a field called 'desc':

$conn->insert('my_table', [
    'desc' => 'Hello',
    ...
]);

The "desc" word throws a SyntaxException.

My solution was to put mysql quote as array parameter like this:

$conn->insert('my_table', [
    '`desc`' => 'Hello', // `desc`
    ...
]);

May it can be a parcial solution, can we discuss a fix??

Best.

@morozov
Copy link
Member Author

morozov commented May 23, 2018

@leoqbc we haven't given enough thought to this issue. Long story short, even if it's relatively easy to fix in Connection::insert() and ::update(), it's impossible to fix it in QueryBuilder w/o breaking its API.

Therefore, the only valid solution right now is to quote identifiers manually (e.g. by using Connection::quoteIdentifier()).

@leoqbc
Copy link

leoqbc commented May 23, 2018

Make sense, as I imagined, not easy to patch...

Well Connection::quoteIdentifier() worked fine for me.

Maybe it can be refactored in future, since It breaks SOLID's SRP IMHO, but I dont know the complete API.

Thanks @morozov and good luck on this.

@morozov
Copy link
Member Author

morozov commented Jul 19, 2021

Closing as the issue is by design. The builder accepts SQL expressions as column names, so the consumer of the API is responsible for building proper SQL.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants