function views_plugin_query_default::compile_fields

Build fields array.

1 call to views_plugin_query_default::compile_fields()
views_plugin_query_default::query in plugins/views_plugin_query_default.inc
Generate a query and a countquery from all of the information supplied to the object.

File

plugins/views_plugin_query_default.inc, line 1199

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

public function compile_fields($fields_array, $query) {
    $non_aggregates = array();
    foreach ($fields_array as $field) {
        $string = '';
        if (!empty($field['table'])) {
            $string .= $field['table'] . '.';
        }
        $string .= $field['field'];
        $fieldname = !empty($field['alias']) ? $field['alias'] : $string;
        if (!empty($field['distinct'])) {
            throw new Exception("Column-level distinct is not supported anymore.");
        }
        if (!empty($field['count'])) {
            // Retained for compatibility.
            $field['function'] = 'count';
            // It seems there's no way to abstract the table+column reference
            // without adding a field, aliasing, and then using the alias.
        }
        if (!empty($field['function'])) {
            $info = $this->get_aggregation_info();
            if (!empty($info[$field['function']]['method']) && function_exists($info[$field['function']]['method'])) {
                $string = $info[$field['function']]['method']($field['function'], $string);
                $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
                $query->addExpression($string, $fieldname, $placeholders);
            }
            $this->has_aggregate = TRUE;
        }
        elseif (empty($field['table'])) {
            if (Database::getConnection()->databaseType() != 'pgsql') {
                $non_aggregates[] = $fieldname;
            }
            elseif (!in_array($fieldname, $non_aggregates)) {
                $non_aggregates[] = $fieldname;
            }
            $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
            $query->addExpression($string, $fieldname, $placeholders);
        }
        elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
            // d7cx: This code was there, apparently needed for PostgreSQL
            // @code
            // $string = db_driver() == 'pgsql' ? "FIRST($string)" : $string;
            // @endcode
            if (Database::getConnection()->databaseType() == 'pgsql' && !in_array($string, $non_aggregates)) {
                $non_aggregates[] = $string;
            }
            $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
        }
        elseif (empty($field['aggregate'])) {
            if (Database::getConnection()->databaseType() != 'pgsql') {
                $non_aggregates[] = $fieldname;
            }
            elseif (!in_array($string, $non_aggregates)) {
                $non_aggregates[] = $string;
            }
            $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
        }
        // @todo Remove this old code.
        if (!empty($field['distinct']) && empty($field['function'])) {
            $distinct[] = $string;
        }
        else {
            $fields[] = $string;
        }
        if ($this->get_count_optimized) {
            // We only want the first field in this case.
            break;
        }
    }
    return array(
        $non_aggregates,
    );
}