Add a number addExpression specific functions

Created on 27 June 2024, 6 months ago
Updated 21 September 2024, 3 months ago

Problem/Motivation

MongoDB does not support SQL strings. Therefor it cannot support the method addExpression($expression). MongoDB can support operation specific expressions.

What MongoDB cannot support:

  $query = \Drupal::database()->select('some_table');
  $count = $query->addExpression('COUNT(some_field)');
  $count_all = $query->addExpression('COUNT(*)');
  $count_distinct = $query->addExpression('COUNT(DISTINCT(some_field))');
  $sum = $query->addExpression('SUM(some_field)');
  $min = $query->addExpression('MIN(some_field)');
  $max = $query->addExpression('MAX(some_field)');
  $coalesce = $query->addExpression('COALESCE(some_field1, some_field2, ...)');
  $field = $query->addExpression('some_field');
  $constant = $query->addExpression('some_value');

What MongoDB can support:

  $query = \Drupal::database()->select('some_table');
  $count = $query->addExpressionCount('some_field');
  $count_all = $query->addExpressionCountAll();
  $count_distinct = $query->addExpressionCountDistinct('some_field');
  $sum = $query->addExpressionSum('some_field');
  $min = $query->addExpressionMin('some_field');
  $max = $query->addExpressionMax('some_field');
  $coalesce = $query->addExpressionCoalesce( array of some fields);
  $field = $query->addExpressionField('some_field');
  $contant = $query->addExpressionContant('some_value');

Proposed resolution

Add the new methods to the Database API and use them as much as possible in core. All the new methods are just wrappers on the original method $query->expression(). Every new wrapper method is used in core at least once, most of them are used a lot. Therefore no extra testing is needed.

Remaining tasks

Do it.

User interface changes

None

API changes

The following methods are added:

  • Select::addExpressionCount(string $field, ?string $alias = NULL)
  • Select::addExpressionCountAll(?string $alias = NULL)
  • Select::addExpressionCountDistinct(string $field, ?string $alias = NULL)
  • Select::addExpressionSum(string $field, ?string $alias = NULL)
  • Select::addExpressionMin(string $field, ?string $alias = NULL)
  • Select::addExpressionMax(string $field, ?string $alias = NULL)
  • Select::addExpressionCoalesce(array $fields, ?string $alias = NULL)
  • Select::addExpressionField(string $field, ?string $alias = NULL)
  • Select::addExpressionConstant(string $constant, ?string $alias = NULL)

Data model changes

None

Release notes snippet

TBD

📌 Task
Status

RTBC

Version

11.0 🔥

Component
Database  →

Last updated about 19 hours ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇳🇱Netherlands daffie

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024