public function Sql::query

Generate a query and a countquery from all of the information supplied to the object.

Parameters

$get_count: Provide a countquery if this is true, otherwise provide a normal query.

Overrides QueryPluginBase::query

1 call to Sql::query()
Sql::build in drupal/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php
Builds the necessary info to execute the query.

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php, line 1174
Definition of Drupal\views\Plugin\views\query\Sql.

Class

Sql
@todo.

Namespace

Drupal\views\Plugin\views\query

Code

public function query($get_count = FALSE) {

  // Check query distinct value.
  if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) {
    $base_field_alias = $this
      ->addField($this->view->storage
      ->get('base_table'), $this->view->storage
      ->get('base_field'));
    $this
      ->addGroupBy($base_field_alias);
    $distinct = TRUE;
  }

  /**
   * An optimized count query includes just the base field instead of all the fields.
   * Determine of this query qualifies by checking for a groupby or distinct.
   */
  if ($get_count && !$this->groupby) {
    foreach ($this->fields as $field) {
      if (!empty($field['distinct']) || !empty($field['function'])) {
        $this->get_count_optimized = FALSE;
        break;
      }
    }
  }
  else {
    $this->get_count_optimized = FALSE;
  }
  if (!isset($this->get_count_optimized)) {
    $this->get_count_optimized = TRUE;
  }
  $options = array();
  $target = 'default';
  $key = 'default';

  // Detect an external database and set the
  if (isset($this->view->base_database)) {
    $key = $this->view->base_database;
  }

  // Set the slave target if the slave option is set
  if (!empty($this->options['slave'])) {
    $target = 'slave';
  }

  // Go ahead and build the query.
  // db_select doesn't support to specify the key, so use getConnection directly.
  $query = Database::getConnection($target, $key)
    ->select($this->view->storage
    ->get('base_table'), $this->view->storage
    ->get('base_table'), $options)
    ->addTag('views')
    ->addTag('views_' . $this->view->storage
    ->id());

  // Add the tags added to the view itself.
  foreach ($this->tags as $tag) {
    $query
      ->addTag($tag);
  }
  if (!empty($distinct)) {
    $query
      ->distinct();
  }
  $joins = $where = $having = $orderby = $groupby = '';
  $fields = $distinct = array();

  // Add all the tables to the query via joins. We assume all LEFT joins.
  foreach ($this->table_queue as $table) {
    if (is_object($table['join'])) {
      $table['join']
        ->buildJoin($query, $table, $this);
    }
  }

  // Assemble the groupby clause, if any.
  $this->has_aggregate = FALSE;
  $non_aggregates = $this
    ->getNonAggregates();
  if (count($this->having)) {
    $this->has_aggregate = TRUE;
  }
  $groupby = array();
  if ($this->has_aggregate && (!empty($this->groupby) || !empty($non_aggregates))) {
    $groupby = array_unique(array_merge($this->groupby, $non_aggregates));
  }

  // Make sure each entity table has the base field added so that the
  // entities can be loaded.
  $entity_tables = $this
    ->getEntityTables();
  if ($entity_tables) {
    $params = array();
    if ($groupby) {

      // Handle grouping, by retrieving the minimum entity_id.
      $params = array(
        'function' => 'min',
      );
    }
    foreach ($entity_tables as $table_alias => $table) {
      $info = entity_get_info($table['entity_type']);
      $base_field = empty($table['revision']) ? $info['entity_keys']['id'] : $info['entity_keys']['revision'];
      $this
        ->addField($table_alias, $base_field, '', $params);
    }
  }

  // Add all fields to the query.
  $this
    ->compileFields($query);

  // Add groupby.
  if ($groupby) {
    foreach ($groupby as $field) {
      $query
        ->groupBy($field);
    }
    if (!empty($this->having) && ($condition = $this
      ->buildCondition('having'))) {
      $query
        ->havingCondition($condition);
    }
  }
  if (!$this->get_count_optimized) {

    // we only add the orderby if we're not counting.
    if ($this->orderby) {
      foreach ($this->orderby as $order) {
        if ($order['field'] == 'rand_') {
          $query
            ->orderRandom();
        }
        else {
          $query
            ->orderBy($order['field'], $order['direction']);
        }
      }
    }
  }
  if (!empty($this->where) && ($condition = $this
    ->buildCondition('where'))) {
    $query
      ->condition($condition);
  }

  // Add a query comment.
  if (!empty($this->options['query_comment'])) {
    $query
      ->comment($this->options['query_comment']);
  }

  // Add the query tags.
  if (!empty($this->options['query_tags'])) {
    foreach ($this->options['query_tags'] as $tag) {
      $query
        ->addTag($tag);
    }
  }

  // Add all query substitutions as metadata.
  $query
    ->addMetaData('views_substitutions', \Drupal::moduleHandler()
    ->invokeAll('views_query_substitutions', array(
    $this->view,
  )));
  return $query;
}