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 QueryPluginBase::execute
function execute(ViewExecutable $view) {
$external = FALSE;
// Whether this query will run against an external database.
$query = $view->build_info['query'];
$count_query = $view->build_info['count_query'];
$query
->addMetaData('view', $view);
$count_query
->addMetaData('view', $view);
if (empty($this->options['disable_sql_rewrite'])) {
$base_table_data = views_fetch_data($this->view->storage
->get('base_table'));
if (isset($base_table_data['table']['base']['access query tag'])) {
$access_tag = $base_table_data['table']['base']['access query tag'];
$query
->addTag($access_tag);
$count_query
->addTag($access_tag);
}
}
$items = array();
if ($query) {
$additional_arguments = module_invoke_all('views_query_substitutions', $view);
// Count queries must be run through the preExecute() method.
// If not, then hook_query_node_access_alter() may munge the count by
// adding a distinct against an empty query string
// (e.g. COUNT DISTINCT(1) ...) and no pager will return.
// See pager.inc > PagerDefault::execute()
// http://api.drupal.org/api/drupal/includes--pager.inc/function/PagerDefault::execute/7
// See http://drupal.org/node/1046170.
$count_query
->preExecute();
// Build the count query.
$count_query = $count_query
->countQuery();
// Add additional arguments as a fake condition.
// XXX: this doesn't work... because PDO mandates that all bound arguments
// are used on the query. TODO: Find a better way to do this.
if (!empty($additional_arguments)) {
// $query->where('1 = 1', $additional_arguments);
// $count_query->where('1 = 1', $additional_arguments);
}
$start = microtime(TRUE);
try {
if ($view->pager
->use_count_query() || !empty($view->get_total_rows)) {
$view->pager
->execute_count_query($count_query);
}
// Let the pager modify the query to add limits.
$view->pager
->pre_execute($query);
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);
$query
->range($offset, $limit);
}
$result = $query
->execute();
$view->result = array();
foreach ($result as $item) {
$view->result[] = $item;
}
$view->pager
->postExecute($view->result);
if ($view->pager
->use_count_query() || !empty($view->get_total_rows)) {
$view->total_rows = $view->pager
->get_total_items();
}
// Load all entities contained in the results.
$this
->load_entities($view->result);
} catch (DatabaseExceptionWrapper $e) {
$view->result = array();
if (!empty($view->live_preview)) {
drupal_set_message($e
->getMessage(), 'error');
}
else {
throw new DatabaseExceptionWrapper(format_string('Exception in @human_name[@view_name]: @message', array(
'@human_name' => $view->storage
->getHumanName(),
'@view_name' => $view->storage
->get('name'),
'@message' => $e
->getMessage(),
)));
}
}
}
else {
$start = microtime(TRUE);
}
$view->execute_time = microtime(TRUE) - $start;
}