You are here

public function Select::addExpression in Drupal 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select::addExpression()
  2. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Select.php \Drupal\Core\Database\Driver\pgsql\Select::addExpression()
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Select.php \Drupal\Core\Database\Driver\pgsql\Select::addExpression()

Adds an expression to the list of "fields" to be SELECTed.

An expression can be any arbitrary string that is valid SQL. That includes various functions, which may in some cases be database-dependent. This method makes no effort to correct for database-specific functions.

Parameters

$expression: The expression string. May contain placeholders.

$alias: The alias for this expression. If not specified, one will be generated automatically in the form "expression_#". The alias will be checked for uniqueness, so the requested alias may not be the alias that is assigned in all cases.

$arguments: Any placeholder arguments needed for this expression.

Return value

The unique alias that was assigned for this expression.

Overrides Select::addExpression

1 call to Select::addExpression()
Select::orderRandom in core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
Orders the result set by a random value.

File

core/lib/Drupal/Core/Database/Driver/pgsql/Select.php, line 117

Class

Select
PostgreSQL implementation of \Drupal\Core\Database\Query\Select.

Namespace

Drupal\Core\Database\Driver\pgsql

Code

public function addExpression($expression, $alias = NULL, $arguments = []) {
  if (empty($alias)) {
    $alias = 'expression';
  }

  // This implements counting in the same manner as the parent method.
  $alias_candidate = $alias;
  $count = 2;
  while (!empty($this->expressions[$alias_candidate])) {
    $alias_candidate = $alias . '_' . $count++;
  }
  $alias = $alias_candidate;
  $this->expressions[$alias] = [
    'expression' => $expression,
    'alias' => $this->connection
      ->escapeAlias($alias_candidate),
    'arguments' => $arguments,
  ];
  return $alias;
}