From 42d3a5e9f84f6006bde0eee67b2221e354db2dfa Mon Sep 17 00:00:00 2001 From: Savetheinternet Date: Sat, 8 Oct 2011 19:05:59 +1100 Subject: [PATCH] debug: show individual query execution times --- inc/database.php | 54 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/inc/database.php b/inc/database.php index 014c2e1e..83f7f0cc 100644 --- a/inc/database.php +++ b/inc/database.php @@ -5,6 +5,36 @@ exit; } + class PreparedQueryDebug { + protected $query; + + public function __construct($query) { + global $pdo; + $this->query = $pdo->prepare($query); + } + public function __call($function, $args) { + global $config, $debug; + + if($config['debug'] && $function == 'execute') { + $start = microtime(true); + } + + $return = call_user_func_array(Array($this->query, $function), $args); + + if($config['debug'] && $function == 'execute') { + $time = round((microtime(true) - $start) * 1000, 2) . 'ms'; + + $debug['sql'][] = Array( + 'query' => $this->query->queryString, + 'rows' => $this->query->rowCount(), + 'time' => '~' . $time + ); + } + + return $return; + } + } + function sql_open() { global $pdo, $config; if($pdo) return true; @@ -31,22 +61,32 @@ function prepare($query) { global $pdo, $debug, $config; - if($config['debug']) { - $debug['sql'][] = $query; - } sql_open(); + + if($config['debug']) + return new PreparedQueryDebug($query); return $pdo->prepare($query); } function query($query) { global $pdo, $debug, $config; - if($config['debug']) { - $debug['sql'][] = $query; - } sql_open(); - return $pdo->query($query); + + if($config['debug']) { + $start = microtime(true); + $query = $pdo->query($query); + $time = round((microtime(true) - $start) * 1000, 2) . 'ms'; + $debug['sql'][] = Array( + 'query' => $query->queryString, + 'rows' => $query->rowCount(), + 'time' => '~' . $time + ); + return $query; + } else { + return $pdo->query($query); + } } function db_error($PDOStatement=null) {