You are here

function view::execute in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 6.3 includes/view.inc \view::execute()
  2. 7.3 includes/view.inc \view::execute()

Execute the view's query.

1 call to view::execute()
view::render in includes/view.inc
Render this view for display.

File

includes/view.inc, line 740
view.inc Provides the view object type and associated methods.

Class

view
An object to contain all of the data to generate a view, plus the member functions to build the view query, execute the query and render the output.

Code

function execute($display_id = NULL) {
  if (empty($this->built)) {
    if (!$this
      ->build($display_id)) {
      return FALSE;
    }
  }
  if (!empty($this->executed)) {
    return TRUE;
  }

  // Let modules modify the view just prior to executing it.
  foreach (module_implements('views_pre_execute') as $module) {
    $function = $module . '_views_pre_execute';
    $function($this);
  }
  $query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field, array(
    'view' => &$this,
  ));
  $count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field, array(
    'view' => &$this,
  ));
  $args = $this->build_info['query_args'];
  vpr($query);

  // Check for already-cached results.
  if (!empty($this->live_preview)) {
    $cache = FALSE;
  }
  else {
    $cache = $this->display_handler
      ->get_cache_plugin();
  }
  if ($cache && $cache
    ->cache_get('results')) {
    $this
      ->synchronize_pager();
    vpr('Used cached results');
  }
  else {
    $items = array();
    if ($query) {
      $replacements = $this
        ->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);
        }
      }

      // Allow for a view to query an external database.
      if (isset($this->base_database)) {
        db_set_active($this->base_database);
        $external = TRUE;
      }
      $start = views_microtime();
      if (!empty($this->pager['items_per_page'])) {

        // We no longer use pager_query() here because pager_query() does not
        // support an offset. This is fine as we don't actually need pager
        // query; we've already been doing most of what it does, and we
        // just need to do a little more playing with globals.
        if (!empty($this->pager['use_pager']) || !empty($this->get_total_rows)) {
          $this->total_rows = db_result(db_query($count_query, $args)) - $this->pager['offset'];
        }
        $this
          ->synchronize_pager();
        $offset = $this->pager['current_page'] * $this->pager['items_per_page'] + $this->pager['offset'];
        $result = db_query_range($query, $args, $offset, $this->pager['items_per_page']);
      }
      else {
        $result = db_query($query, $args);
      }
      $this->result = array();
      while ($item = db_fetch_object($result)) {
        $this->result[] = $item;
      }

      // If we already know how many items we have even if we did not run the
      // count query, go ahead and set that value:
      if (empty($this->pager['items_per_page'])) {
        $this->total_rows = count($this->result);
      }
      if (!empty($external)) {
        db_set_active();
      }
      $this->execute_time = views_microtime() - $start;
    }
    if ($cache) {
      $cache
        ->cache_set('results');
    }
  }

  // Let modules modify the view just after executing it.
  foreach (module_implements('views_post_execute') as $module) {
    $function = $module . '_views_post_execute';
    $function($this);
  }
  $this->executed = TRUE;
}