Add a number addExpression specific functions

Created on 27 June 2024, 2 months ago
Updated 16 July 2024, about 2 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->addExpressionConstant('MIN(some_field)');
  $max = $query->addExpressionField('MAX(some_field)');
  $coalesce = $query->addExpression('COALESCE(some_field1, some_field2, ...)');
  $field = $query->addExpressionMax('some_field');
  $constant = $query->addExpressionMin('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.

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

Active

Version

11.0 🔥

Component
Database 

Last updated 1 minute 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

Production build 0.71.5 2024