From fc69fd6293e4e9d0f0181bb56219e9b3b3c4dd1f Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Wed, 22 Sep 2021 14:28:00 +0200 Subject: [PATCH] fix: support explain with pgsql (#1207) * fix: support explain with pgsql * fix: record driver in transactions --- src/DataCollector/QueryCollector.php | 31 +++++++++++++++++++++------- src/LaravelDebugbar.php | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/DataCollector/QueryCollector.php b/src/DataCollector/QueryCollector.php index 898cd0c0c..c2ee9bffa 100644 --- a/src/DataCollector/QueryCollector.php +++ b/src/DataCollector/QueryCollector.php @@ -185,6 +185,7 @@ public function addQuery($query, $bindings, $time, $connection) 'source' => $source, 'explain' => $explainResults, 'connection' => $connection->getDatabaseName(), + 'driver' => $connection->getConfig('driver'), 'hints' => $this->showHints ? $hints : null, 'show_copy' => $this->showCopyButton, ]; @@ -453,6 +454,7 @@ public function collectTransactionEvent($event, $connection) 'source' => $source, 'explain' => [], 'connection' => $connection->getDatabaseName(), + 'driver' => $connection->getConfig('driver'), 'hints' => null, 'show_copy' => false, ]; @@ -493,14 +495,27 @@ public function collect() ]; // Add the results from the explain as new rows - foreach ($query['explain'] as $explain) { - $statements[] = [ - 'sql' => " - EXPLAIN # {$explain->id}: `{$explain->table}` ({$explain->select_type})", - 'type' => 'explain', - 'params' => $explain, - 'row_count' => $explain->rows, - 'stmt_id' => $explain->id, - ]; + if ($query['driver'] === 'pgsql') { + $explainer = trim(implode("\n", array_map(function ($explain) { + return $explain->{'QUERY PLAN'}; + }, $query['explain']))); + + if ($explainer) { + $statements[] = [ + 'sql' => " - EXPLAIN: {$explainer}", + 'type' => 'explain', + ]; + } + } else { + foreach ($query['explain'] as $explain) { + $statements[] = [ + 'sql' => " - EXPLAIN # {$explain->id}: `{$explain->table}` ({$explain->select_type})", + 'type' => 'explain', + 'params' => $explain, + 'row_count' => $explain->rows, + 'stmt_id' => $explain->id, + ]; + } } } diff --git a/src/LaravelDebugbar.php b/src/LaravelDebugbar.php index 4c69f06c2..61e168e7a 100644 --- a/src/LaravelDebugbar.php +++ b/src/LaravelDebugbar.php @@ -1137,7 +1137,7 @@ protected function addServerTimingHeaders(Response $response) $headers = []; foreach ($collector->collect()['measures'] as $k => $m) { - $headers[] = sprintf('app;desc="%s";dur=%F', str_replace('"', "'", $m['label']), $m['duration'] * 1000); + $headers[] = sprintf('app;desc="%s";dur=%F', str_replace(array("\n", "\r"), ' ', str_replace('"', "'", $m['label'])), $m['duration'] * 1000); } $response->headers->set('Server-Timing', $headers, false);