Check query parameter type for the interger type in select queries and for entity type tables only

Created on 22 January 2023, almost 2 years ago
Updated 20 February 2024, 9 months ago

Problem/Motivation

MongoDB needs query condition parameters to be of the correct type. For instance when you create a new node entity and it has the ID = 4. You can with a relational database query the node table by doing:

$nid = '4';
$result = \Drupal:database()->select('node', 'n')
  ->fields('n', ['nid'])
  ->condition('nid', $nid)
  ->execute()
  ->fetchField();

With MongoDB the value of $result will be NULL. For MongoDB the integer 4 is not the same as the string value '4'.

Proposed resolution

Give parameter values in query conditions the correct typehint. With the correct typehint the query will work for relational databases and for MongoDB.

$nid = '4';
$result = \Drupal:database()->select('node', 'n')
  ->fields('n', ['nid'])
  ->condition('nid', (int) $nid)
  ->execute()
  ->fetchField();

To test if this is done correctly we can throw a deprecation message and replace it with an exception in the next major version when the query condition parameter is of the wrong type. Whe should do thids only in testing. The query condition parameter type checking will take up to much time in production. We can add a new variable to the class Drupal\Core\Database\Connection and name it inTesting with a default value of FALSE. When we are in testing we change the value to TRUE. A new Select query is started from the Connection class and we can give the class Select also an extra variable called inTesting which is set on construction. When not set it will default to FALSE. When we execute a query we than also do the check query condition parameters of being of the right type. We have the value of the class variable Drupal\Core\Database\Query\Select::tables and we have entity table schema in \Drupal::keyValue('entity.storage_schema.sql'). If we combine them we should have enough info to check for the correct query condition parameter types.

Remaining tasks

TBD

User interface changes

None

API changes

: Throw a deprecation message and in a new major version an exception when a query condition parameter is of the wrong type.

Data model changes

None

Release notes snippet

TBD

📌 Task
Status

Active

Version

11.0 🔥

Component
Database 

Last updated 3 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇳🇱Netherlands daffie

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024