Explorar o código

Adding new constant in the Database class in order to error_log mysql errors see #4682

Julio Montoya %!s(int64=12) %!d(string=hai) anos
pai
achega
680d8608fa
Modificáronse 3 ficheiros con 61 adicións e 3 borrados
  1. 29 3
      main/inc/lib/database.lib.php
  2. 29 0
      main/inc/lib/database.mysqli.lib.php
  3. 3 0
      main/install/index.php

+ 29 - 3
main/inc/lib/database.lib.php

@@ -23,6 +23,9 @@ require_once 'database.constants.inc.php';
  * @package chamilo.database
  */
 class Database {
+    
+    /* Variable use only in the installation process to log errors. See the Database::query function */
+    static $log_queries = false;
 
     /*
         Accessor methods
@@ -680,10 +683,9 @@ class Database {
         //@todo remove this before the stable release
         
         //Check if the table contains a c_ (means a course id)
-        if (strpos($query, 'c_')) {      	
+        if (strpos($query, 'c_')) {	
         	//Check if the table contains inner joins 
-        	if (
-                    
+        	if (                    
                 strpos($query, 'allow_public_certificates') === false &&
                 strpos($query, 'DROP TABLE IF EXISTS') === false &&      
                 strpos($query, 'thematic_advance') === false &&  
@@ -798,6 +800,30 @@ class Database {
                 $info .= '</pre>';
                 echo $info;
             }
+            
+            if (isset(self::$log_queries) && self::$log_queries) {
+                error_log("----------------  SQL error ---------------- ");
+                error_log($query);
+                $info = 'FILE: ' .(empty($file) ? ' unknown ' : $file);
+                error_log($info);
+                $info = 'LINE: '.(empty($line) ? ' unknown ' : $line);
+                error_log($info);
+                
+                if (empty($type)) {
+                    if (!empty($function)) {
+                        $info = 'FUNCTION: ' . $function;
+                        error_log($info);
+                    }
+                } else {
+                    if (!empty($class) && !empty($function)) {
+                        $info .= 'CLASS: ' . $class;
+                        error_log($info);
+                        $info .= 'METHOD: ' . $function;
+                        error_log($info);
+                    }
+                }
+                error_log("---------------- end ----------------");
+            }
         }
         return $result;
     }

+ 29 - 0
main/inc/lib/database.mysqli.lib.php

@@ -25,6 +25,9 @@ require_once 'database.constants.inc.php';
  *	@package chamilo.library
  */
 class Database {
+    
+    /* Variable use only in the installation process to log errors. See the Database::query function */
+    static $log_queries = false;
 
     /*
         Accessor methods
@@ -660,6 +663,7 @@ class Database {
      */
     public static function query($query, $connection = null, $file = null, $line = null) {
         global $database_connection;
+        
         $result = @$database_connection->query($query);
         if ($database_connection->errno) {
             $backtrace = debug_backtrace(); // Retrieving information about the caller statement.
@@ -706,6 +710,31 @@ class Database {
                 $info .= '</pre>';
                 echo $info;
             }
+            
+            if (isset(self::$log_queries) && self::$log_queries) {
+                error_log("----------------  SQL error ---------------- ");
+                error_log($query);
+                $info = 'FILE: ' .(empty($file) ? ' unknown ' : $file);
+                error_log($info);
+                $info = 'LINE: '.(empty($line) ? ' unknown ' : $line);
+                error_log($info);
+                
+                if (empty($type)) {
+                    if (!empty($function)) {
+                        $info = 'FUNCTION: ' . $function;
+                        error_log($info);
+                    }
+                } else {
+                    if (!empty($class) && !empty($function)) {
+                        $info .= 'CLASS: ' . $class;
+                        error_log($info);
+                        $info .= 'METHOD: ' . $function;
+                        error_log($info);
+                    }
+                }
+                error_log("---------------- end ----------------");
+            }
+            
         }
         return $result;
     }

+ 3 - 0
main/install/index.php

@@ -45,6 +45,9 @@ require_once 'install.lib.php';
 require_once 'install.class.php';
 require_once 'i_database.class.php';
 
+// This value is use in database::query in order to prompt errors
+Database::$log_queries = true;
+
 // The function api_get_setting() might be called within the installation scripts.
 // We need to provide some limited support for it through initialization of the
 // global array-type variable $_setting.