You are here

public function Select::addExpression in Drupal driver for SQL Server and SQL Azure 3.0.x

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php \Drupal\Driver\Database\sqlsrv\Select::addExpression()
  2. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php \Drupal\Driver\Database\sqlsrv\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.

Overriden with an aditional exclude parameter that tells not to include this expression (by default) in the select list.

Drupal expects the AVG() function to return a decimal number. SQL Server will return the FLOOR instead. We multiply the expression by 1.0 to force a cast inside the AVG function. `AVG(m.id)` becomes `AVG(m.id * 1.0)`.

Parameters

string $expression: The expression string. May contain placeholders.

string $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.

mixed $arguments: Any placeholder arguments needed for this expression.

bool $exclude: If set to TRUE, this expression will not be added to the select list. Useful when you want to reuse expressions in the WHERE part.

bool $expand: If this expression will be expanded as a CROSS_JOIN so it can be consumed from other parts of the query. TRUE by default. It attempts to detect expressions that cannot be cross joined (aggregates).

Return value

string The unique alias that was assigned for this expression.

Overrides Select::addExpression

2 calls to Select::addExpression()
Select::orderRandom in drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php
Override of SelectQuery::orderRandom() for SQL Server.
Select::preExecute in drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php
Generic preparation and validation for a SELECT query.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php, line 65

Class

Select

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function addExpression($expression, $alias = NULL, $arguments = [], $exclude = FALSE, $expand = TRUE) {
  $sub_expression = $expression;
  $replacement_expression = '';
  while (strlen($sub_expression) > 5 && ($pos1 = stripos($sub_expression, 'AVG(')) !== FALSE) {
    $pos2 = $this
      ->findParenMatch($sub_expression, $pos1 + 3);
    $inner = substr($sub_expression, $pos1 + 4, $pos2 - 4 - $pos1);
    $replacement_expression .= substr($sub_expression, 0, $pos1 + 4) . '(' . $inner . ') * 1.0)';
    if (strlen($sub_expression) > $pos2 + 1) {
      $sub_expression = substr($sub_expression, $pos2 + 1);
    }
    else {
      $sub_expression = '';
    }
  }
  $replacement_expression .= $sub_expression;
  $alias = parent::addExpression($replacement_expression, $alias, $arguments);
  $this->expressions[$alias]['exclude'] = $exclude;
  $this->expressions[$alias]['expand'] = $expand;
  return $alias;
}