From e43881e03f5cc91be9827d6918765207048493c0 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 24 Feb 2014 15:33:53 +0100 Subject: [PATCH] Better debug --- public_html/media/css/core.css | 8 -------- public_html/media/css/debug.css | 30 ++++++++++++++++++++++++++++++ src/Database.php | 32 +++++++++++++++++++++++--------- src/Views/debug.phtml | 24 ++++++++++++++++++++++++ src/Views/layout-normal.phtml | 13 ++++--------- 5 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 public_html/media/css/debug.css create mode 100644 src/Views/debug.phtml diff --git a/public_html/media/css/core.css b/public_html/media/css/core.css index 217df23f..0e062fbd 100644 --- a/public_html/media/css/core.css +++ b/public_html/media/css/core.css @@ -391,14 +391,6 @@ ul.tagit input { margin: -1px 0 0 0; } -pre.debug { - margin-left: 1em; - text-align: left; - color: black; - white-space: normal; - text-indent: -1em; -} - .spoiler:before, .spoiler:after { color: gray; diff --git a/public_html/media/css/debug.css b/public_html/media/css/debug.css new file mode 100644 index 00000000..a93f0a94 --- /dev/null +++ b/public_html/media/css/debug.css @@ -0,0 +1,30 @@ +div.debug { + background: #f5f5f5; + font-size: 90%; + margin: 1em 0; + padding: 1em; + color: black; + text-align: left; +} +div.debug pre { + margin-left: 1em; + white-space: normal; + text-indent: -1em; +} +div.debug pre.query { + color: maroon; +} +div.debug pre.bindings { + color: gray; +} +div.debug pre.bindings .value { + color: green; + font-weight: bold; + margin-right: 1em; +} +div.debug pre.query span { + background: rgba(255, 0, 0, 0.05); +} +div.debug pre.query span:hover { + background: white; +} diff --git a/src/Database.php b/src/Database.php index 974d5dc3..0a4a9cf4 100644 --- a/src/Database.php +++ b/src/Database.php @@ -2,7 +2,7 @@ class Database { protected static $pdo = null; - protected static $queries = []; + protected static $queryLogs = []; public static function connect($driver, $location, $user, $pass) { @@ -51,38 +51,52 @@ class Database return self::$pdo !== null; } - public static function exec(SqlStatement $stmt) + private static function execInternal(SqlStatement $stmt, $callback) { if (!self::connected()) throw new Exception('Database is not connected'); + $stmtPdo = self::convertStatement($stmt); try { + $timeStart = microtime(true); $stmtPdo->execute(); + $timeExec = microtime(true) - $timeStart; + + $timeStart = microtime(true); + $ret = $callback($stmtPdo); + $timeFetch = microtime(true) - $timeStart; } catch (Exception $e) { throw new Exception('Problem with ' . $stmt->getAsString() . ' execution (' . $e->getMessage() . ')'); } - self::$queries []= $stmt; - return $stmtPdo; + $queryLog = new StdClass(); + $queryLog->statement = $stmt; + $queryLog->timeExec = $timeExec; + $queryLog->timeFetch = $timeFetch; + self::$queryLogs []= $queryLog; + return $ret; + } + + public static function exec(SqlStatement $stmt) + { + return self::execInternal($stmt, function($stmtPdo) { }); } public static function fetchOne(SqlStatement $stmt) { - $stmtPdo = self::exec($stmt); - return $stmtPdo->fetch(); + return self::execInternal($stmt, function($stmtPdo) { return $stmtPdo->fetch(); }); } public static function fetchAll(SqlStatement $stmt) { - $stmtPdo = self::exec($stmt); - return $stmtPdo->fetchAll(); + return self::execInternal($stmt, function($stmtPdo) { return $stmtPdo->fetchAll(); }); } public static function getLogs() { - return self::$queries; + return self::$queryLogs; } public static function inTransaction() diff --git a/src/Views/debug.phtml b/src/Views/debug.phtml new file mode 100644 index 00000000..d49d2973 --- /dev/null +++ b/src/Views/debug.phtml @@ -0,0 +1,24 @@ + +
+ +
+ statement->getAsString(); + $query = str_replace('(', '(', $query); + $query = str_replace(')', ')', $query); + ?> +
+ +
' . $log->statement->getBindings()[$key] . '';
+				},
+				array_keys($log->statement->getBindings()))) ?>
+ + + + +
Execution:timeExec) ?>
Retrieval:timeFetch) ?>
+
+ +
diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index 15e5d219..a7f440bd 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -43,18 +43,13 @@ LayoutHelper::addScript('core.js'); Logs
- config->misc->debugQueries): ?> - getBindings() as $k => $v) - $bindings []= $k . '=' . $v; - printf('
%s [%s]
', htmlspecialchars($query->getAsString()), join(', ', $bindings)); - } ?> - + config->misc->debugQueries): ?> + renderFile('debug') ?> + +