You are here

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

Same name and namespace in other branches
  1. 7.3 plugins/views_plugin_query_default.inc \views_plugin_query_default::execute()

Executes the query and fills the associated view object with according values.

Values to set: $view->result, $view->total_rows, $view->execute_time, $view->current_page.

Overrides views_plugin_query::execute

File

plugins/views_plugin_query_default.inc, line 1152
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 execute(&$view) {
  $external = FALSE;

  // Whether this query will run against an external database.
  $query = $this->final_query;
  $count_query = $this->count_query;
  $args = $this->query_args;
  vpr($query);
  $items = array();
  if ($query) {
    $replacements = $this->view
      ->substitutions();
    $query = str_replace(array_keys($replacements), $replacements, $query);
    $count_query = 'SELECT COUNT(*) FROM (' . str_replace(array_keys($replacements), $replacements, $count_query) . ') count_alias';
    if (is_array($args)) {
      foreach ($args as $id => $arg) {
        $args[$id] = str_replace(array_keys($replacements), $replacements, $arg);
      }
    }

    // Detect an external database.
    if (isset($view->base_database)) {
      $this
        ->db_set_active($view->base_database);
      $external = TRUE;
    }
    $start = views_microtime();
    if ($this->pager
      ->use_count_query() || !empty($view->get_total_rows)) {
      $this->pager
        ->execute_count_query($count_query, $args);
    }

    // Let the pager modify the query to add limits.
    $this->pager
      ->pre_execute($query, $args);
    if (!empty($this->limit) || !empty($this->offset)) {

      // We can't have an offset without a limit, so provide a very large limit instead.
      $limit = intval(!empty($this->limit) ? $this->limit : 999999);
      $offset = intval(!empty($this->offset) ? $this->offset : 0);
      $result = $this
        ->db_query_range($query, $args, $offset, $limit);
    }
    else {
      $result = $this
        ->db_query($query, $args);
    }
    $view->result = array();
    while ($item = $this
      ->db_fetch_object($result)) {
      $view->result[] = $item;
    }
    $this->pager
      ->post_execute($view->result);
    if ($this->pager
      ->use_pager()) {
      $view->total_rows = $this->pager
        ->get_total_items();
    }
    if ($external) {
      $this
        ->db_set_active();
    }
  }
  else {
    $start = views_microtime();
  }
  $view->execute_time = views_microtime() - $start;
}