You are here

public function Select::preExecute in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php \Drupal\Driver\Database\sqlsrv\Select::preExecute()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php \Drupal\Driver\Database\sqlsrv\Select::preExecute()

Override for SelectQuery::preExecute().

Ensure that all the fields in ORDER BY and GROUP BY are part of the main query.

Overrides Select::preExecute

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php, line 114
Definition of Drupal\Driver\Database\sqlsrv\Select

Class

Select

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function preExecute(DatabaseSelectInterface $query = NULL) {

  // If no query object is passed in, use $this.
  if (!isset($query)) {
    $query = $this;
  }

  // Only execute this once.
  if ($this
    ->isPrepared()) {
    return TRUE;
  }

  // Execute standard pre-execution first.
  parent::preExecute($query);
  if ($this->distinct || $this->group) {

    // When the query is DISTINCT or contains GROUP BY fields, all the fields
    // in the GROUP BY and ORDER BY clauses must appear in the returned
    // columns.
    $columns = $this->order + array_flip($this->group);
    $counter = 0;
    foreach ($columns as $field => $dummy) {
      $found = FALSE;
      foreach ($this->fields as $f) {
        if (!isset($f['table']) || !isset($f['field'])) {
          continue;
        }
        $alias = "{$f['table']}.{$f['field']}";
        if ($alias == $field) {
          $found = TRUE;
          break;
        }
      }
      if (!isset($this->fields[$field]) && !isset($this->expressions[$field]) && !$found) {
        $alias = '_field_' . $counter++;
        $this
          ->addExpression($field, $alias, array(), FALSE, FALSE);
        $this->queryOptions['sqlsrv_drop_columns'][] = $alias;
      }
    }

    // The other way round is also true, if using aggregates, all the fields in the SELECT
    // must be present in the GROUP BY.
    if (!empty($this->group)) {
      foreach ($this->fields as $field) {
        $spec = $field['table'] . '.' . $field['field'];
        $alias = $field['alias'];
        if (!isset($this->group[$spec]) && !isset($this->group[$alias])) {
          $this->group[$spec] = $spec;
        }
      }
    }

    // More over, GROUP BY columns cannot use aliases, so expand them to
    // their full expressions.
    foreach ($this->group as $key => &$group_field) {

      // Expand an alias on a field.
      if (isset($this->fields[$group_field])) {
        $field = $this->fields[$group_field];
        $group_field = (isset($field['table']) ? $this->connection
          ->escapeTable($field['table']) . '.' : '') . $this->connection
          ->escapeField($field['field']);
      }
      else {
        if (isset($this->expressions[$group_field])) {
          $expression = $this->expressions[$group_field];
          $group_field = $expression['expression'];

          // If the expression has arguments, we now
          // have duplicate placeholders. Run as insecure.
          if (is_array($expression['arguments'])) {
            $this->queryOptions['insecure'] = TRUE;
          }
        }
      }
    }
  }
  return $this->prepared;
}