Problem/Motivation

Static queries should have the field names bracket-encapsulated. In that way static queries cannot fail on a database, because a field name is a reserved keyword for the given database. For more info: https://www.drupal.org/node/2986894.

Proposed resolution

Bracket-encapsulated field names for all static queries located in the directory "core/lib/Drupal/Core" and its sub-directories, but not in the directory "core/lib/Drupal/Core/Database". Static queries can be found by doing a search for "->query(" and "->queryRange(" in the directory "core/lib/Drupal/Core" and its sub-directories, but not in the directory "core/lib/Drupal/Core/Database".

What needs to be changed:

From:
$connection->query('SELECT nid FROM {node} WHERE nid = :id', [':id' => 1']);

To:
$connection->query('SELECT [nid] FROM {node} WHERE [nid] = :id', [':id' => 1']);

Remaining tasks

TBD

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

TBD

Comments

daffie created an issue. See original summary.

rik-dev’s picture

Assigned: Unassigned » rik-dev
rik-dev’s picture

Status: Active » Needs review
StatusFileSize
new60.32 KB
rik-dev’s picture

StatusFileSize
new35.64 KB
matroskeen’s picture

StatusFileSize
new344.29 KB

@rik-dev, please take a look into task description:

located in the directory "core/lib/Drupal/Core" and its sub-directories, but not in the directory "core/lib/Drupal/Core/Database"

It seems that only a small part of your patch covers the actual scope of this task (see the screenshot).

daffie’s picture

Issue summary: View changes
Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Queue/DatabaseQueue.php
    @@ -100,7 +100,7 @@ protected function doCreateItem($data) {
    +      return (int) $this->connection->query('SELECT COUNT(item_id) FROM {' . static::TABLE_NAME . '} WHERE [name] = :name', [':name' => $this->name])
    

    The field "item_id" also needs square brackets.

  2. There are queries in "core/lib/Drupal/Core/Config/DatabaseStorage.php" that need to be changed.
  3. There are queries in "core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php" that need to be changed.
  4. There are queries in "core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php" that need to be changed.
  5. There are queries in "core/lib/Drupal/Core/Lock/DatabaseLockBackend.php" that need to be changed.
  6. There are queries in "core/lib/Drupal/Core/Queue/DatabaseQueue.php" that need to be changed.
  7. There are queries in "core/lib/Drupal/Core/Routing/RouteProvider.php" that need to be changed.
  8. There are queries in "core/lib/Drupal/Core/Test/EnvironmentCleaner.php" that need to be changed.
  9. There are queries in "core/lib/Drupal/Core/Extension/module.api.php" that need to be changed.
  10. There are queries in "core/lib/Drupal/Core/Form/form.api.php" that need to be changed.

Also is @Matroskeen right about his comment #5.

daffie’s picture

Category: Task » Bug report
Issue tags: +Bug Smash Initiative

The parent issue is a "bug report", therefore this issue is one too.

Lal_’s picture

StatusFileSize
new11.62 KB
new20.97 KB
Lal_’s picture

Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Needs work

The patch is now a lot better.

+++ b/core/lib/Drupal/Core/Queue/DatabaseQueue.php
@@ -100,7 +100,7 @@ protected function doCreateItem($data) {
+      return (int) $this->connection->query('SELECT COUNT[item_id] FROM {' . static::TABLE_NAME . '} WHERE [name] = :name', [':name' => $this->name])

This change is not correct. It should be COUNT([item_id]).

Still open are: #6.2, #6.5, #6.8, #6.9, #6.10.

Lal_’s picture

Status: Needs work » Needs review
StatusFileSize
new17.55 KB
new6.22 KB

@daffie made the changes

The last submitted patch, 8: 3152415-9.patch, failed testing. View results

daffie’s picture

Status: Needs review » Needs work

@Lal_: Thanks for working on the patch. It look good, only we have missed a query in "core/lib/Drupal/Core/Session/SessionHandler.php".

Lal_’s picture

Status: Needs work » Needs review
StatusFileSize
new18.26 KB
new602 bytes

I hope this is it @daffie

Status: Needs review » Needs work

The last submitted patch, 14: 3152415-14.patch, failed testing. View results

daffie’s picture

Status: Needs work » Reviewed & tested by the community

All fields are now encapsulated by square brackets.
All code changes look good.
The testbot is happy for all 3 by core supported databases.
For me it is RTBC.

Thank you for working on this issue: @Lal_, @rik-dev and @Matroskeen.

rik-dev’s picture

Assigned: rik-dev » Unassigned

  • catch committed 638f25e on 9.1.x
    Issue #3152415 by Lal_, rik-dev, Matroskeen, daffie: Bracket-...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 638f25e and pushed to 9.1.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.