summaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
authorSavetheinternet <savetheinternet@tinyboard.org>2011-10-08 19:05:59 +1100
committerSavetheinternet <savetheinternet@tinyboard.org>2011-10-08 19:05:59 +1100
commit42d3a5e9f84f6006bde0eee67b2221e354db2dfa (patch)
tree82ab468a555eb631801cd2ecca812f25a5efca17 /inc
parent7360381b41ee359717437a3cf3b4d2379c3726d0 (diff)
debug: show individual query execution times
Diffstat (limited to 'inc')
-rw-r--r--inc/database.php54
1 files 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) {