From 37eca2ae7f1e6664391c8117a5816c9d2f440101 Mon Sep 17 00:00:00 2001
From: Dylan Tack <git@dylan-tack.net>
Date: Tue, 11 Feb 2014 22:18:07 -0800
Subject: [PATCH] Issue #2191287 by grendzy: Fix reporting of additional
 queries

---
 includes/admin.inc | 11 ++++++-----
 includes/view.inc  | 52 ++++++++--------------------------------------------
 2 files changed, 14 insertions(+), 49 deletions(-)

diff --git a/includes/admin.inc b/includes/admin.inc
index 1f247bd..458cd61 100644
--- a/includes/admin.inc
+++ b/includes/admin.inc
@@ -202,17 +202,18 @@ function views_ui_preview($view, $display_id, $args = array()) {
               }
             }
           }
-          $rows['query'][] = array('<strong>' . t('Query') . '</strong>', '<pre>' . check_plain(strtr($query, $quoted)) . '</pre>');
           if (!empty($view->additional_queries)) {
-            $queries = '<strong>' . t('These queries were run during view rendering:') . '</strong>';
             foreach ($view->additional_queries as $query) {
               if ($queries) {
                 $queries .= "\n";
               }
-              $queries .= t('[@time ms]', array('@time' => intval($query[1] * 100000) / 100)) . ' ' . $query[0];
+              $queries .= t('<strong>' . '[@time ms]' . '</strong>', array('@time' => round($query['time'] * 1000, 2))) . ' ' . $query['query'];
             }
 
-            $rows['query'][] = array('<strong>' . t('Other queries') . '</strong>', '<pre>' . $queries . '</pre>');
+            $rows['query'][] = array('<strong>' . t('Queries') . '</strong>', '<pre>' . $queries . '</pre>');
+          }
+          else {
+            $rows['query'][] = array('<strong>' . t('Query') . '</strong>', '<pre>' . check_plain(strtr($query, $quoted)) . '</pre>');
           }
         }
         if ($show_info) {
@@ -4888,7 +4889,7 @@ function views_ui_admin_settings_basic() {
 
   $form['live_preview']['views_show_additional_queries'] = array(
     '#type' => 'checkbox',
-    '#title' => t('Show other queries run during render during live preview'),
+    '#title' => t('Show all queries run during live preview'),
     '#description' => t("Drupal has the potential to run many queries while a view is being rendered. Checking this box will display every query run during view render as part of the live preview."),
     '#default_value' => variable_get('views_show_additional_queries', FALSE),
     '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
diff --git a/includes/view.inc b/includes/view.inc
index d8c0c1f..fca0342 100644
--- a/includes/view.inc
+++ b/includes/view.inc
@@ -1189,6 +1189,10 @@ class view extends views_db_object {
    *   Return the output of the rendered view or NULL if something failed in the process.
    */
   function render($display_id = NULL) {
+    if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
+      $this->start_query_capture();
+    }
+
     $this->execute($display_id);
 
     // Check to see if the build failed.
@@ -1202,9 +1206,6 @@ class view extends views_db_object {
     drupal_theme_initialize();
 
     $start = microtime(TRUE);
-    if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
-      $this->start_query_capture();
-    }
 
     $exposed_form = $this->display_handler->get_plugin('exposed_form');
     $exposed_form->pre_render($this->result);
@@ -1698,28 +1699,11 @@ class view extends views_db_object {
 
   /**
    * Set up query capturing.
-   *
-   * db_query() stores the queries that it runs in global $queries,
-   * bit only if dev_query is set to true. In this case, we want
-   * to temporarily override that setting if it's not and we
-   * can do that without forcing a db rewrite by just manipulating
-   * $conf. This is kind of evil but it works.
    */
   function start_query_capture() {
-    global $conf, $queries;
-    if (empty($conf['dev_query'])) {
-      $this->fix_dev_query = TRUE;
-      $conf['dev_query'] = TRUE;
-    }
-
-    // Record the last query key used; anything already run isn't
-    // a query that we are interested in.
-    $this->last_query_key = NULL;
-
-    if (!empty($queries)) {
-      $keys = array_keys($queries);
-      $this->last_query_key = array_pop($keys);
-    }
+    @include_once DRUPAL_ROOT . '/includes/database/log.inc';
+    $this->dbLogKey = uniqid('views', TRUE);  // Use a unique log key in case views are nested, e.g. viewfield or other shenanigans
+    Database::startLog($this->dbLogKey);
   }
 
   /**
@@ -1728,27 +1712,7 @@ class view extends views_db_object {
    * @see view::start_query_capture()
    */
   function end_query_capture() {
-    global $conf, $queries;
-    if (!empty($this->fix_dev_query)) {
-      $conf['dev_query'] = FALSE;
-    }
-
-    // make a copy of the array so we can manipulate it with array_splice.
-    $temp = $queries;
-
-    // Scroll through the queries until we get to our last query key.
-    // Unset anything in our temp array.
-    if (isset($this->last_query_key)) {
-      while (list($id, $query) = each($queries)) {
-        if ($id == $this->last_query_key) {
-          break;
-        }
-
-        unset($temp[$id]);
-      }
-    }
-
-    $this->additional_queries = $temp;
+    $this->additional_queries = Database::getLog($this->dbLogKey);
   }
 
   /**
-- 
1.8.5.4

