Display reformated queries in SQL backtrace (imported from promo2003.polytechnique...
authorx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sat, 12 Aug 2006 21:03:44 +0000 (21:03 +0000)
committerx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sat, 12 Aug 2006 21:03:44 +0000 (21:03 +0000)
I personally find this more readable

git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@807 839d8a87-29fc-0310-9880-83ba4fa771e5

classes/XDB.php
templates/database-debug.tpl

index 905d025..df77e82 100644 (file)
@@ -32,6 +32,35 @@ class XDB
     }
 
     // }}}
+    // {{{ function _reformatQuery
+
+    function _reformatQuery($query)
+    {
+        $query  = preg_split("/\n\\s*/", $query);
+        $length = 0;
+        foreach ($query as $line) {
+            if (preg_match('/^([A-Z]+( (JOIN|BY))?) /', $line, $matches)
+                && $matches[1] != 'AND' && $matches[2] != 'OR') {
+                $length = max($length, strlen($matches[1]));
+            }
+        }
+        $res = '';
+        foreach ($query as $line) {
+            $local = -2;
+            if (preg_match('/^([A-Z]+(?: (?:JOIN|BY))?) +(.*)/', $line, $matches)
+                && $matches[1] != 'AND' && $matches[2] != 'OR') {
+                $local = strlen($matches[1]);
+                $line  = $matches[1] . '  ' . $matches[2];
+            }
+            $local   = $length - $local;
+            $res    .= str_repeat(' ', $local) . $line . "\n";
+            $length += 2 * (substr_count($line, '(') - substr_count($line, ')'));
+        }
+        return $res;
+    }
+
+    // }}}
+    // {{{ function _query
 
     function _query($query) {
         global $globals;
@@ -42,7 +71,7 @@ class XDB
             while ($row = @mysql_fetch_assoc($_res)) {
                 $explain[] = $row;
             }
-            $trace_data = array('query' => $query, 'explain' => $explain);
+            $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain);
             @mysql_free_result($_res);
         }
 
@@ -56,6 +85,7 @@ class XDB
         return $res;
     }
 
+    // }}}
     // {{{ function query
 
     function &query()
index 9e015d3..bf6d610 100644 (file)
@@ -25,9 +25,9 @@
 <table class="bicol" style="width: 75%; font-family: monospace; font-size: smaller; margin-left:2px; margin-top: 3px;">
   <tr class="impair">
     <td>
-      <strong>QUERY:</strong><br />
-      {$query.query|nl2br}
-      <br />
+      <strong>QUERY:</strong>
+      <pre style="padding: 0; margin: 0;">{$query.query}</pre>
+      <br/>
     </td>
   </tr>
   {if $query.error}