You are here

function views_plugin_query_default::condition_sql in Views (for Drupal 7) 6.3

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

$where: 'where' or 'having'.

1 call to views_plugin_query_default::condition_sql()
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 906
views_plugin_query_default.inc Defines the default query object which builds SQL to execute using the Drupal database API.

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

function condition_sql($where = 'where') {
  $clauses = array();

  // Respekt the special group, which is always AND.
  $has_arguments = FALSE;
  $clauses_main = array();
  $has_filter = FALSE;
  foreach ($this->{$where} as $group => $info) {
    if (empty($info['clauses'])) {
      continue;
    }
    $clause = implode(") " . $info['type'] . " (", $info['clauses']);
    if (count($info['clauses']) > 1) {
      $clause = '(' . $clause . ')';
    }
    if ($group == 0) {
      $has_arguments = TRUE;
      $clauses_main[] = $clause;
    }
    else {
      $has_filter = TRUE;
      $clauses[] = $clause;
    }
  }
  $keyword = drupal_strtoupper($where);
  if ($has_filter) {
    if (count($clauses) > 1) {
      $filter_group = "(" . implode(")\n    " . $this->group_operator . ' (', array_filter($clauses)) . ")\n";
    }
    else {
      $filter_group = array_shift($clauses) . "\n";
    }
    $clauses_main[] = $filter_group;
  }
  if ($clauses_main) {
    if (count($clauses_main) > 1) {
      return "{$keyword} " . "(" . implode(")\n    " . 'AND' . ' (', array_filter($clauses_main)) . ")\n";
    }
    else {
      return "{$keyword} " . array_shift($clauses_main) . "\n";
    }
  }
  return "";
}