You are here

public function Select::preExecute 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::preExecute()
  2. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php \Drupal\Driver\Database\sqlsrv\Select::preExecute()

Generic preparation and validation for a SELECT query.

Return value

TRUE if the validation was successful, FALSE if not.

Overrides Select::preExecute

File

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

Class

Select

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function preExecute(SelectInterface $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, [], 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']);
      }
      elseif (isset($this->expressions[$group_field])) {
        $expression = $this->expressions[$group_field];
        $group_field = $expression['expression'];
      }
    }
  }
  $this->queryOptions['emulate_prepares'] = TRUE;
  return $this->prepared;
}