You are here

function views_data_export_plugin_query_default_batched::execute in Views data export 6.3

Same name and namespace in other branches
  1. 7.4 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_query_default_batched::execute()
  2. 7 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_query_default_batched::execute()
  3. 7.3 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_query_default_batched::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_default::execute

File

plugins/views_data_export_plugin_display_export.inc, line 756
Contains the bulk export display plugin.

Class

views_data_export_plugin_query_default_batched

Code

function execute(&$view) {
  $display_handler =& $view->display_handler;
  $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();
  $start = 0;
  if ($query) {
    $replacements = module_invoke_all('views_query_substitutions', $view);
    $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)) {
      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);

    // The $query is final and ready to go, we are going to redirect it to
    // become an insert into our table, sneaky!
    // Our query will look like:
    // CREATE TABLE {idx} SELECT @row := @row + 1 AS weight_alias, cl.* FROM
    // (-query-) AS cl, (SELECT @row := 0) AS r
    // We do some magic to get the row count.
    $display_handler->batched_execution_state->sandbox['weight_field_alias'] = $display_handler
      ->_weight_alias_create($view);

    // Views can construct queries that have fields with aliases longer than
    // 64 characters, which will cause problems when creating the table to
    // insert them into. So we hash the aliases down to make sure they are
    // unique.
    $display_handler->batched_execution_state->sandbox['field_aliases'] = $display_handler
      ->field_aliases_create($view);
    $select_aliases = array();
    foreach ($display_handler->batched_execution_state->sandbox['field_aliases'] as $hash => $alias) {
      $select_aliases[] = "cl.{$alias} AS {$hash}";
    }
    $insert_query = 'CREATE TABLE {' . $display_handler
      ->index_tablename() . '} SELECT @row := @row + 1 AS ' . $display_handler->batched_execution_state->sandbox['weight_field_alias'] . ', ' . implode(', ', $select_aliases) . ' FROM (' . $query . ') AS cl, (SELECT @row := 0) AS r';
    db_query($insert_query, $args);

    // Now create an index for the weight field, otherwise the queries on the
    // index will take a long time to execute.
    $ret = array();
    db_add_unique_key($ret, $display_handler
      ->index_tablename(), $display_handler->batched_execution_state->sandbox['weight_field_alias'], array(
      $display_handler->batched_execution_state->sandbox['weight_field_alias'],
    ));
    $this->pager
      ->post_execute($view->result);
    if ($this->pager
      ->use_pager()) {
      $view->total_rows = $this->pager
        ->get_total_items();
    }
    if ($external) {
      db_set_active();
    }
  }
  $view->execute_time = views_microtime() - $start;
}