获取视图执行的实际查询


23

我需要找出由某个查询执行的SQL查询。在配置视图时,“视图”模块可以显示SQL,但显然查询不是在所有情况下都可以运行的实际查询
我知道Devel模块可以显示数据库查询,但是devel无法显示实际查询,除非在每个查询中单击关联的“ A”链接,并且有数百个查询

我如何找出视图执行的实际查询?该视图显示为块。

Answers:


40

您必须使用hook_views_pre_execute,并已安装Devel dpq来查看SQL字符串:

function hook_views_pre_execute(&$view) {
  dpq($view->build_info['query']);
}

谢谢。它在hook api页面中读取到“现在已完全构建查询,但尚未通过db_rewrite_sql运行该查询。” 这是否意味着在运行之前,其他挂钩可能会重写sql?然后,我不会在所有情况下都得到实际的查询。
jjei 2012年

2
pre_render可能是最好的,但是我不确定在大多数情况下查询是否会有很大不同。
Countzero 2012年

实际上,我认为某些情况下Views中存在错误,我刚刚发布了一个问题drupal.org/node/1845772
Sean Bannister

1
如果在视图设置中启用了SQL查询输出,则$ view-> build_info ['query']似乎与视图显示的查询相同。
Johnathan Elmore

这没用。示例SELECT users.uid AS uid,users.created AS,users_created,users.language AS users_language,users.mail AS users_mail,users.name AS users_name,'user'AS field_data_field_first_name_user_entity_type,'user'AS field_data_field_last_name_user_entity_type_user_user_date_ir {users}个用户所在的位置((((users.status <>'0')AND(users.created> 1441641600))))ORDER BY users_created DESC LIMIT 20 OFFSET 0我有“ field_data_field_first_name_user_entity_type”,它是用户字段,不会给出正确的结果没有任何改动。
Marko Blazekovic


2

无需补丁或挂钩。

// Run the view.
$view = views_get_view('frontpage');
$view->set_display('page');
$view->pre_execute();
$view->execute();

/* Magic Below Here */
// Get query from the view.
$query = $view->query->query();

// Format SelectQueryInterface into a string.
$string = (string) $query;

// Replace arguments.
$arguments = $query->arguments();
if (!empty($arguments) && is_array($arguments)) {
  foreach ($arguments as $placeholder => &$value) {
    if (is_string($value)) {
      $value = "'$value'";
    }
  }
  $string = strtr($string, $arguments);
}

// Format the query string for more readable output.
$string = str_replace(array(' {', "\n{"), ' ', $string);
$string = str_replace(array('} ', "}\n"), ' AS ', $string);
$string = str_replace(', ', ",\n  ", $string);
$string = str_replace(' AND ', "\n  AND ", $string);
$string = str_replace(' ON ', "\n  ON ", $string);
$string = str_replace('SELECT ', "SELECT\n  ", $string);
$string = str_replace('ORDER BY ', "ORDER BY\n  ", $string);

// echo $string;
echo str_replace('  ', '&nbsp;&nbsp;', nl2br($string));

将此作为输出

SELECT
  node.sticky AS node_sticky,
  node.created AS node_created,
  node.nid AS nid,
  'frontpage:page' AS view_name
FROM  node AS node
WHERE (( (node.promote <> 0)
  AND (node.status = 1) ))
ORDER BY
  node_sticky DESC,
  node_created DESC
LIMIT 10 OFFSET 0

抱歉,我的输出与众不同:SELECT node.nid AS nid, 'node' AS field_data_field_name_node_entity_type, 'node' AS field_data_field_surname_node_entity_type, ecc ...
Leo

你能帮助我吗?
狮子座

1
@Leo我需要更多有关您尝试对其进行查看的视图的信息。输出是未更改的开箱即用的首页视图;听起来您已经更改了首页视图的配置,所以SQL当然会有所不同。
mikeytown2 '18 / 10/15

谢谢你回答我,也许我将创建一个问题,并插入下面的链接,为了不垃圾邮件这个答案
狮子座

问题的链接,我希望我清楚drupal.stackexchange.com/questions/270994/…–
Leo

1

请尝试以下补丁:

--- a/sites/all/modules/views/plugins/views_plugin_query_default.inc
+++ b/sites/all/modules/views/plugins/views_plugin_query_default.inc
@@ -1393,6 +1393,19 @@ class views_plugin_query_default extends     views_plugin_query {
           $query->range($offset, $limit);
         }

+        $query_string = (string)$query;
+        $query_string = str_replace('{', '', $query_string);
+        $query_string = str_replace('}', '', $query_string);
+        $query_params = $query->getArguments();
+        foreach($query_params as $placeholder => $value) {
+          if(!is_numeric($value)) {
+            $query_string = str_replace($placeholder, "'$value'",    $query_string);
+          }
+          else {
+            $query_string = str_replace($placeholder, $value, $query_string);
+          }
+        }
+        drupal_set_message($query_string);
         $result = $query->execute();

         $view->result = array();

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.