function views_query::query

2.x query.inc views_query::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.

File

includes/query.inc, line 893
query.inc Defines the query object which is the underlying layer in a View.

Class

views_query
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 (!empty($this->fields[$this->base_field])) {
      $this->fields[$this->base_field]['distinct'] = TRUE;
      $this->add_groupby($this->base_field);
    }
  }

  /**
   * 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'])) {
        $get_count_optimized = FALSE;
        break;
      }
    }
  }
  else {
    $get_count_optimized = FALSE;
  }
  if (!isset($get_count_optimized)) {
    $get_count_optimized = TRUE;
  }

  $joins = $fields = $where = $having = $orderby = $groupby = '';
  // 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'])) {
      $joins .= $table['join']->join($table, $this) . "\n";
    }
  }

  $has_aggregate = FALSE;
  $non_aggregates = array();

  foreach ($fields_array as $field) {
    if ($fields) {
      $fields .= ",\n   ";
    }
    $string = '';
    if (!empty($field['table'])) {
      $string .= $field['table'] . '.';
    }
    $string .= $field['field'];

    // store for use with non-aggregates below
    $fieldname = (!empty($field['alias']) ? $field['alias'] : $string);

    if (!empty($field['distinct'])) {
      $string = "DISTINCT($string)";
    }
    if (!empty($field['count'])) {
      $string = "COUNT($string)";
      $has_aggregate = TRUE;
    }
    else if (!empty($field['aggregate'])) {
      $has_aggregate = TRUE;
    }
    elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
      $string = $GLOBALS['db_type'] == 'pgsql' ? "FIRST($string)" : $string;
    }
    else {
      $non_aggregates[] = $fieldname;
    }
    if ($field['alias']) {
      $string .= " AS $field[alias]";
    }
    $fields .= $string;

    if ($get_count_optimized) {
      // We only want the first field in this case.
      break;
    }
  }

  if ($has_aggregate || $this->groupby) {
    $groupby = "GROUP BY " . implode(', ', array_unique(array_merge($this->groupby, $non_aggregates))) . "\n";
    if ($this->having) {
      $having = $this->condition_sql('having');
    }
  }

  if (!$get_count_optimized) {
    // we only add the groupby if we're not counting.
    if ($this->orderby) {
      $orderby = "ORDER BY " . implode(', ', $this->orderby) . "\n";
    }
  }

  $where = $this->condition_sql();

  $query = "SELECT $fields\n FROM {" . $this->base_table . "} $this->base_table \n$joins $where $groupby $having $orderby";

  $replace = array('&gt;' => '>', '&lt;' => '<');
  $query = strtr($query, $replace);

  return $query;
}