function views_plugin_query_default::build_condition

Construct the "WHERE" or "HAVING" part of the query.

As views has to wrap the conditions from arguments with AND, a special group is wrapped around all conditions. This special group has the ID 0. There is other code in filters which makes sure that the group IDs are higher than zero.

Parameters

string $where: Either 'where' or 'having'.

1 call to views_plugin_query_default::build_condition()
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 1143

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

public function build_condition($where = 'where') {
    $has_condition = FALSE;
    $has_arguments = FALSE;
    $has_filter = FALSE;
    $main_group = db_and();
    $filter_group = $this->group_operator == 'OR' ? db_or() : db_and();
    foreach ($this->{$where} as $group => $info) {
        if (!empty($info['conditions'])) {
            $sub_group = $info['type'] == 'OR' ? db_or() : db_and();
            foreach ($info['conditions'] as $clause) {
                // DBTNG doesn't support to add the same subquery twice to the main
                // query and the count query, so clone the subquery to have two
                // instances of the same object.
                // @see http://drupal.org/node/1112854
                if (is_object($clause['value']) && $clause['value'] instanceof SelectQuery) {
                    $clause['value'] = clone $clause['value'];
                }
                if ($clause['operator'] == 'formula') {
                    $has_condition = TRUE;
                    $sub_group->where($clause['field'], $clause['value']);
                }
                else {
                    $has_condition = TRUE;
                    $sub_group->condition($clause['field'], $clause['value'], $clause['operator']);
                }
            }
            // Add the item to the filter group.
            if ($group != 0) {
                $has_filter = TRUE;
                $filter_group->condition($sub_group);
            }
            else {
                $has_arguments = TRUE;
                $main_group->condition($sub_group);
            }
        }
    }
    if ($has_filter) {
        $main_group->condition($filter_group);
    }
    if (!$has_arguments && $has_condition) {
        return $filter_group;
    }
    if ($has_arguments && $has_condition) {
        return $main_group;
    }
}