1 views_plugin_query_default.inc views_plugin_query_default::query($get_count = FALSE)

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 views_plugin_query::query

File

core/modules/views/plugins/views_plugin_query_default.inc, line 1241
Defines the default query object.

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

function query($get_count = FALSE) {
  // Check query distinct value.
  if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) {
    if ($this->pure_distinct === FALSE) {
      $base_field_alias = $this->add_field($this->base_table, $this->base_field);
      $this->add_groupby($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.
   */
  $fields_array = $this->fields;
  if ($get_count && !$this->groupby) {
    foreach ($fields_array 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 replica target if the replica option is set
  if (!empty($this->options['replica'])) {
    $target = 'replica';
  }

  // 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->base_table, $this->base_table, $options)
    ->addTag('views')
    ->addTag('views_' . $this->view->name);

  // 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']->build_join($query, $table, $this);
    }
  }

  $this->has_aggregate = FALSE;
  $non_aggregates = array();

  list($non_aggregates) = $this->compile_fields($fields_array, $query);

  if (count($this->having)) {
    $this->has_aggregate = TRUE;
  }
  elseif (!$this->has_aggregate) {
    // Allow 'GROUP BY' even no aggregation function has been set.
    $this->has_aggregate = $this->view->display_handler->get_option('group_by');
  }
  if ($this->has_aggregate && (!empty($this->groupby) || !empty($non_aggregates))) {
    $groupby = array_unique(array_merge($this->groupby, $non_aggregates));
    foreach ($groupby as $field) {
      $query->groupBy($field);
    }
    if (!empty($this->having) && $condition = $this->build_condition('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->build_condition('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', module_invoke_all('views_query_substitutions', $this));

  if (!$get_count) {
    if (!empty($this->limit) || !empty($this->offset)) {
      // We can't have an offset without a limit, so provide a very large
      // limit instead.
      $limit = intval(!empty($this->limit) ? $this->limit : 999999);
      $offset = intval(!empty($this->offset) ? $this->offset : 0);
      $query->range($offset, $limit);
    }
  }

  return $query;
}