Display reformated queries in SQL backtrace (imported from promo2003.polytechnique...
[platal.git] / classes / XDB.php
index 2a4c505..df77e82 100644 (file)
@@ -21,6 +21,8 @@
 
 class XDB
 {
+    var $_trace_data = array();
+
     // {{{ function _prepare
 
     function _prepare($args) {
@@ -30,6 +32,60 @@ 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;
+
+        if ($globals->debug & 1) {
+            $_res = mysql_query("EXPLAIN $query");
+            $explain = array();
+            while ($row = @mysql_fetch_assoc($_res)) {
+                $explain[] = $row;
+            }
+            $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain);
+            @mysql_free_result($_res);
+        }
+
+        $res = mysql_query($query);
+
+        if ($globals->debug & 1) {
+            $trace_data['error'] = mysql_error();
+            $GLOBALS['XDB::trace_data'][] = $trace_data;
+        }
+
+        return $res;
+    }
+
+    // }}}
     // {{{ function query
 
     function &query()
@@ -40,9 +96,9 @@ class XDB
     // }}}
     // {{{ function execute()
 
-    function execute() {
-        global $globals;
-        return $globals->db->query(XDB::_prepare(func_get_args()));
+    function execute()
+    {
+        return XDB::_query(XDB::_prepare(func_get_args()));
     }
 
     // }}}
@@ -99,6 +155,11 @@ class XDB
     }
 
     // }}}
+
+    function trace_format(&$page, $template = 'database-debug.tpl') {
+        $page->assign('trace_data', $GLOBALS['XDB::trace_data']);
+        return $page->fetch($template);
+    }
 }
 
 class XOrgDBResult
@@ -112,12 +173,7 @@ class XOrgDBResult
 
     function XOrgDBResult($query)
     {
-        global $globals;
-        if (strpos($query, 'SQL_CALC_FOUND_ROWS') === false) {
-            $this->_res = $globals->db->query($query);
-        } else {
-            $this->_res = mysql_query($query);
-        }
+        $this->_res = XDB::_query($query);
     }
 
     // }}}
@@ -229,7 +285,7 @@ class XOrgDBResult
     // }}}
 }
 
-class XOrgDBIterator extends XOrgIterator
+class XOrgDBIterator
 {
     // {{{ properties