]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14679 dml/native_mysqli improvements
authorskodak <skodak>
Sat, 18 Oct 2008 22:35:42 +0000 (22:35 +0000)
committerskodak <skodak>
Sat, 18 Oct 2008 22:35:42 +0000 (22:35 +0000)
lib/dml/moodle_database.php
lib/dml/mysqli_native_moodle_database.php

index 8539eba44818dd9de4460621c5c97dcc740de36e..4a53a50f3880da0ba39988d106d9622b0dc5d679 100644 (file)
@@ -250,7 +250,26 @@ abstract class moodle_database {
      * @param mixed $obj optional library specific object
      */
     protected function report_error($sql, array $params=null, $obj=null) {
-        debugging($this->get_last_error() .'<br /><br />'. s($sql).'<br /> ['.var_export($params, true).']');
+        debugging($this->get_last_error() .'<br /><br />'. s($sql).'<br />['.s(var_export($params, true)).']');
+    }
+
+    /**
+     * Print sql debug info
+     * @param string $sql query which caused problems
+     * @param array $params optional query parameters
+     * @param mixed $obj optional library specific object
+     */
+    protected function print_debug($sql, array $params=null, $obj=null) {
+        if (!$this->get_debug()) {
+            return;
+        }
+        //TODO: detect CLI mode and skip s() ;-)
+        echo "<hr />\n";
+        echo s($sql)."\n";
+        if (!is_null($params)) {
+            echo "[".s(var_export($params, true))."]\n";
+        }
+        echo "<hr />\n";
     }
 
     /**
index a1ca0489498170ba79a00e3b17f63b3c7d5acc83..3c0cbd2dffe8a60ad5a74a4cbf86e0be6d5cce2f 100644 (file)
@@ -9,7 +9,8 @@ require_once($CFG->libdir.'/dml/mysqli_native_moodle_recordset.php');
  */
 class mysqli_native_moodle_database extends moodle_database {
 
-    protected $mysqli=null;
+    protected $mysqli = null;
+    protected $debug  = false;
 
     /**
      * Detects if all needed PHP stuff installed.
@@ -324,11 +325,10 @@ class mysqli_native_moodle_database extends moodle_database {
 
     /**
      * Enable/disable very detailed debugging
-     * TODO: do we need levels?
      * @param bool $state
      */
     public function set_debug($state) {
-        //TODO
+        $this->debug = $state;
     }
 
     /**
@@ -336,12 +336,11 @@ class mysqli_native_moodle_database extends moodle_database {
      * @return bool $state
      */
     public function get_debug() {
-        //TODO
+        return $this->debug;
     }
 
     /**
      * Enable/disable detailed sql logging
-     * TODO: do we need levels?
      * @param bool $state
      */
     public function set_logging($state) {
@@ -354,6 +353,8 @@ class mysqli_native_moodle_database extends moodle_database {
      * @return bool success
      */
     public function change_database_structure($sql) {
+        $this->writes++;
+        $this->print_debug($sql);
         return ($this->mysqli->query($sql) === TRUE);
     }
 
@@ -401,7 +402,7 @@ class mysqli_native_moodle_database extends moodle_database {
         $rawsql = $this->emulate_bound_params($sql, $params);
 
         $this->writes++;
-
+        $this->print_debug($sql, $params);
         $result = $this->mysqli->query($rawsql);
 
         if ($result === false) {
@@ -433,19 +434,20 @@ class mysqli_native_moodle_database extends moodle_database {
      * @return mixed an moodle_recorset object, or false if an error occured.
      */
     public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
-        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
-        $rawsql = $this->emulate_bound_params($sql, $params);
-
         if ($limitfrom or $limitnum) {
             $limitfrom = (int)$limitfrom;
             $limitnum  = (int)$limitnum;
             if ($limitnum < 1) {
                 $limitnum = "18446744073709551615";
             }
-            $rawsql .= " LIMIT $limitfrom, $limitnum";
+            $sql .= " LIMIT $limitfrom, $limitnum";
         }
 
+        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
+        $rawsql = $this->emulate_bound_params($sql, $params);
+
         $this->reads++;
+        $this->print_debug($sql, $params);
         // no MYSQLI_USE_RESULT here, it would block write ops on affected tables
         $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
 
@@ -475,19 +477,20 @@ class mysqli_native_moodle_database extends moodle_database {
      * @return mixed an array of objects, or empty array if no records were found, or false if an error occured.
      */
     public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
-        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
-        $rawsql = $this->emulate_bound_params($sql, $params);
-
         if ($limitfrom or $limitnum) {
             $limitfrom = (int)$limitfrom;
             $limitnum  = (int)$limitnum;
             if ($limitnum < 1) {
                 $limitnum = "18446744073709551615";
             }
-            $rawsql .= " LIMIT $limitfrom, $limitnum";
+            $sql .= " LIMIT $limitfrom, $limitnum";
         }
 
+        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
+        $rawsql = $this->emulate_bound_params($sql, $params);
+
         $this->reads++;
+        $this->print_debug($sql, $params);
         $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
 
         if ($result === false) {
@@ -519,6 +522,7 @@ class mysqli_native_moodle_database extends moodle_database {
         $rawsql = $this->emulate_bound_params($sql, $params);
 
         $this->reads++;
+        $this->print_debug($sql, $params);
         $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
 
         if ($result === false) {
@@ -563,8 +567,6 @@ class mysqli_native_moodle_database extends moodle_database {
             return false;
         }
 
-        $this->writes++;
-
         $fields = implode(',', array_keys($params));
         $qms    = array_fill(0, count($params), '?');
         $qms    = implode(',', $qms);
@@ -572,6 +574,8 @@ class mysqli_native_moodle_database extends moodle_database {
         $sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($qms)";
         $rawsql = $this->emulate_bound_params($sql, $params);
 
+        $this->writes++;
+        $this->print_debug($sql, $params);
         $result = $this->mysqli->query($rawsql);
 
         if ($result === false) {
@@ -689,8 +693,6 @@ class mysqli_native_moodle_database extends moodle_database {
             return false;
         }
 
-        $this->writes++;
-
         $sets = array();
         foreach ($params as $field=>$value) {
             $sets[] = "$field = ?";
@@ -702,6 +704,8 @@ class mysqli_native_moodle_database extends moodle_database {
         $sql = "UPDATE {$this->prefix}$table SET $sets WHERE id=?";
         $rawsql = $this->emulate_bound_params($sql, $params);
 
+        $this->writes++;
+        $this->print_debug($sql, $params);
         $result = $this->mysqli->query($rawsql);
 
         if ($result === false) {
@@ -781,6 +785,7 @@ class mysqli_native_moodle_database extends moodle_database {
         $rawsql = $this->emulate_bound_params($sql, $params);
 
         $this->writes++;
+        $this->print_debug($sql, $params);
         $result = $this->mysqli->query($rawsql);
 
         if ($result === false) {
@@ -809,6 +814,7 @@ class mysqli_native_moodle_database extends moodle_database {
         $rawsql = $this->emulate_bound_params($sql, $params);
 
         $this->writes++;
+        $this->print_debug($sql, $params);
         $result = $this->mysqli->query($rawsql);
 
         if ($result === false) {