From e6ae0a2f6c0df5b35b32b1c8d36dacf00a412060 Mon Sep 17 00:00:00 2001 From: Najdanovic Ivan Date: Thu, 7 Jan 2021 13:52:34 +0100 Subject: [PATCH] DebugToolbar - Handle Query display in Query class --- system/Database/Query.php | 52 +++++++++++++++++ system/Debug/Toolbar/Collectors/Database.php | 59 ++------------------ 2 files changed, 58 insertions(+), 53 deletions(-) diff --git a/system/Database/Query.php b/system/Database/Query.php index 5cbea7e37ac1..6891ebe1c9a7 100644 --- a/system/Database/Query.php +++ b/system/Database/Query.php @@ -473,6 +473,58 @@ protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, i //-------------------------------------------------------------------- + /** + * Returns string to display in debug toolbar + * + * @return string + */ + public function debugToolbarDisplay(): string + { + // Key words we want bolded + static $highlight = [ + 'SELECT', + 'DISTINCT', + 'FROM', + 'WHERE', + 'AND', + 'LEFT JOIN', + 'RIGHT JOIN', + 'JOIN', + 'ORDER BY', + 'GROUP BY', + 'LIMIT', + 'INSERT', + 'INTO', + 'VALUES', + 'UPDATE', + 'OR ', + 'HAVING', + 'OFFSET', + 'NOT IN', + 'IN', + 'LIKE', + 'NOT LIKE', + 'COUNT', + 'MAX', + 'MIN', + 'ON', + 'AS', + 'AVG', + 'SUM', + '(', + ')', + ]; + + $sql = $this->getQuery(); + + foreach ($highlight as $term) + { + $sql = str_replace($term, '' . $term . '', $sql); + } + + return $sql; + } + /** * Return text representation of the query * diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 1027557a8b8b..a6c4458a9814 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -57,7 +57,7 @@ class Database extends BaseCollector * The query instances that have been collected * through the DBQuery Event. * - * @var array + * @var Query[] */ protected static $queries = []; @@ -138,59 +138,12 @@ protected function formatTimelineData(): array */ public function display(): array { - // Key words we want bolded - $highlight = [ - 'SELECT', - 'DISTINCT', - 'FROM', - 'WHERE', - 'AND', - 'LEFT JOIN', - 'RIGHT JOIN', - 'JOIN', - 'ORDER BY', - 'GROUP BY', - 'LIMIT', - 'INSERT', - 'INTO', - 'VALUES', - 'UPDATE', - 'OR ', - 'HAVING', - 'OFFSET', - 'NOT IN', - 'IN', - 'LIKE', - 'NOT LIKE', - 'COUNT', - 'MAX', - 'MIN', - 'ON', - 'AS', - 'AVG', - 'SUM', - '(', - ')', - ]; - - $data = [ - 'queries' => [], - ]; - - foreach (static::$queries as $query) - { - $sql = $query->getQuery(); - - foreach ($highlight as $term) - { - $sql = str_replace($term, "{$term}", $sql); - } - - $data['queries'][] = [ - 'duration' => ($query->getDuration(5) * 1000) . ' ms', - 'sql' => $sql, + $data['queries'] = array_map(function (Query $query) { + return [ + 'duration' => ((float) $query->getDuration(5) * 1000) . ' ms', + 'sql' => $query->debugToolbarDisplay(), ]; - } + }, static::$queries); return $data; }