diff --git a/db.class.php b/db.class.php
index 7de0fc4..d0cdd17 100644
--- a/db.class.php
+++ b/db.class.php
@@ -19,7 +19,6 @@
class DB
{
- public static $debug = false;
public static $insert_id = 0;
public static $num_rows = 0;
public static $affected_rows = 0;
@@ -36,6 +35,7 @@ class DB
public static $port = null;
public static $encoding = 'latin1';
public static $queryMode = 'queryAllRows';
+ public static $success_handler = false;
public static $error_handler = 'meekrodb_error_handler';
public static $throw_exception_on_error = false;
@@ -53,8 +53,8 @@ class DB
return $mysql;
}
- public static function debugMode() {
- DB::$debug = true;
+ public static function debugMode($handler = true) {
+ DB::$success_handler = $handler;
}
public static function insertId() { return DB::$insert_id; }
@@ -69,11 +69,6 @@ class DB
if (! $db->select_db($dbName)) die("unable to set db to $dbName");
DB::$current_db = $dbName;
DB::$current_db_limit = $limit;
-
- if (DB::$debug) {
- if ($limit) echo "Setting DB to $dbName for $limit queries
\n";
- else echo "Setting DB to $dbName for $limit queries
\n";
- }
}
@@ -309,9 +304,9 @@ class DB
$db = DB::get();
- if (DB::$debug) $starttime = microtime(true);
+ if (DB::$success_handler) $starttime = microtime(true);
$result = $db->query($sql, $is_buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
- if (DB::$debug) $runtime = microtime(true) - $starttime;
+ if (DB::$success_handler) $runtime = microtime(true) - $starttime;
if (!$sql || $error = DB::checkError()) {
if (function_exists(DB::$error_handler)) {
@@ -325,10 +320,14 @@ class DB
$e = new MeekroDBException($error, $sql);
throw $e;
}
- } else if (DB::$debug) {
+ } else if (DB::$success_handler) {
$runtime = sprintf('%f', $runtime * 1000);
- $sqlShow = "$sql (" . ($is_buffered ? 'MYSQLI_STORE_RESULT' : 'MYSQLI_USE_RESULT') . ")";
- echo "QUERY: $sqlShow [$runtime ms]
\n";
+ $success_handler = function_exists(DB::$success_handler) ? DB::$success_handler : 'meekrodb_debugmode_handler';
+
+ call_user_func($success_handler, array(
+ 'query' => $sql,
+ 'runtime' => $runtime
+ ));
}
DB::$queryResult = $result;
@@ -539,4 +538,13 @@ function meekrodb_error_handler($params) {
die;
}
+function meekrodb_debugmode_handler($params) {
+ echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]";
+ if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
+ echo "\n";
+ } else {
+ echo "
\n";
+ }
+}
+
?>
diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php
index 934acb9..25f01f0 100644
--- a/simpletest/BasicTest.php
+++ b/simpletest/BasicTest.php
@@ -5,6 +5,11 @@ function new_error_callback($params) {
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
}
+function my_debug_handler($params) {
+ global $debug_callback_worked;
+ if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
+}
+
class BasicTest extends SimpleTest {
function __construct() {
@@ -136,8 +141,17 @@ class BasicTest extends SimpleTest {
$exception_was_caught = 2;
}
$this->assert($exception_was_caught === 2);
+ }
+
+ function test_7_debugmode_handler() {
+ global $debug_callback_worked;
+ DB::debugMode('my_debug_handler');
+ DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
+ $this->assert($debug_callback_worked === 1);
+
+ DB::debugMode(false);
}
}