function views_plugin_query_default::query

Same name in other branches
  1. 7.x-3.x plugins/views_plugin_query_default.inc \views_plugin_query_default::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 views_plugin_query::query

1 call to views_plugin_query_default::query()
views_plugin_query_default::build in plugins/views_plugin_query_default.inc
Builds the necessary info to execute the query.

File

plugins/views_plugin_query_default.inc, line 1025

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)) {
        $base_field_alias = $this->add_field($this->base_table, $this->base_field, NULL, array(
            'distinct' => TRUE,
        ));
        $this->add_groupby($base_field_alias);
    }
    
    /**
     * 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'])) {
                $get_count_optimized = FALSE;
                break;
            }
        }
    }
    else {
        $get_count_optimized = FALSE;
    }
    if (!isset($get_count_optimized)) {
        $get_count_optimized = TRUE;
    }
    $joins = $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";
        }
    }
    list($distinct, $fields, $non_aggregates) = $this->compile_fields($fields_array);
    if (count($this->having)) {
        $this->has_aggregate = TRUE;
    }
    if ($this->has_aggregate && (!empty($this->groupby) || !empty($non_aggregates))) {
        $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 orderby if we're not counting, because of performance reasons.
        if ($this->orderby) {
            $orderby = "ORDER BY " . implode(', ', $this->orderby) . "\n";
        }
    }
    $where = $this->condition_sql();
    $query = "SELECT " . implode(",\n", array_merge($distinct, $fields)) . "\n FROM {" . $this->base_table . "} {$this->base_table} \n{$joins} {$where} {$groupby} {$having} {$orderby}";
    $replace = array(
        '>' => '>',
        '&lt;' => '<',
    );
    $query = strtr($query, $replace);
    return $query;
}