From 9814721e8ddee4e26cc58cd47301e5d741048a22 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 19 Mar 2008 10:36:47 -0400
Subject: [PATCH] Enable SQL logging (set 'sql_debug' config param to true); Switch to emulated prepare mode for better performance

---
 program/include/rcube_db.inc   |   12 +++++++++++-
 program/include/main.inc       |    1 +
 program/include/rcube_mdb2.inc |   33 +++++++++++++++++++++++++++++++--
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/program/include/main.inc b/program/include/main.inc
index f3d0e26..247cdf1 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -74,6 +74,7 @@
   
   $DB = new $dbclass($CONFIG['db_dsnw'], $CONFIG['db_dsnr'], $CONFIG['db_persistent']);
   $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
+  $DB->set_debug((bool)$CONFIG['sql_debug']);
   $DB->db_connect('w');
 
   // use database for storing session data
diff --git a/program/include/rcube_db.inc b/program/include/rcube_db.inc
index 4c3e9fc..63c6759 100644
--- a/program/include/rcube_db.inc
+++ b/program/include/rcube_db.inc
@@ -153,8 +153,18 @@
     $this->db_handle = $this->dsn_connect($dsn);
     $this->db_connected = $this->db_handle ? TRUE : FALSE;
     }
+
+
+  /**
+   * Activate/deactivate debug mode
+   * (not implemented)
+   */
+  function set_debug($dbg = true)
+  {
     
-    
+  }
+
+
   /**
    * Getter for error state
    *
diff --git a/program/include/rcube_mdb2.inc b/program/include/rcube_mdb2.inc
index 63d156a..98eaf7e 100644
--- a/program/include/rcube_mdb2.inc
+++ b/program/include/rcube_mdb2.inc
@@ -48,6 +48,7 @@
   var $db_handle = 0;         // Connection handle
   var $db_error = false;
   var $db_error_msg = '';
+  var $debug_mode = false;
 
   var $a_query_results = array('dummy');
   var $last_res_id = 0;
@@ -94,8 +95,11 @@
   function dsn_connect($dsn)
     {
     // Use persistent connections if available
-    $dbh = MDB2::connect($dsn,
-      array('persistent' => $this->db_pconn,
+    $dbh = MDB2::connect($dsn, array(
+        'emulate_prepared' => true,
+        'persistent' => $this->db_pconn,
+        'debug' => $this->debug_mode,
+        'debug_handler' => 'mdb2_debug_handler',
         'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL));
 
     if (MDB2::isError($dbh))
@@ -155,6 +159,18 @@
     $this->db_connected = true;
     }
 
+
+  /**
+   * Activate/deactivate debug mode
+   *
+   * @param boolean True if SQL queries should be logged
+   */
+  function set_debug($dbg = true)
+  {
+    $this->debug_mode = $dbg;
+    if ($this->db_connected)
+      $this->db_handle->setOption('debug', $dbg);
+  }
 
     
   /**
@@ -569,4 +585,17 @@
 
   }  // end class rcube_db
 
+
+/* this is our own debug handler for the MDB2 connection */
+function mdb2_debug_handler(&$db, $scope, $message, $context = array())
+{
+  if ($scope != 'prepare')
+  {
+    $debug_output = $scope . '('.$db->db_index.'): ';
+    $debug_output .= $message . $db->getOption('log_line_break');
+    write_log('sqllog', $debug_output);
+  }
+}
+
+
 ?>

--
Gitblit v1.9.1