Skip to content

Commit

Permalink
fix: support explain with pgsql (#1207)
Browse files Browse the repository at this point in the history
* fix: support explain with pgsql

* fix: record driver in transactions
  • Loading branch information
jaulz committed Sep 22, 2021
1 parent d3ba4f6 commit fc69fd6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down Expand Up @@ -453,6 +454,7 @@ public function collectTransactionEvent($event, $connection)
'source' => $source,
'explain' => [],
'connection' => $connection->getDatabaseName(),
'driver' => $connection->getConfig('driver'),
'hints' => null,
'show_copy' => false,
];
Expand Down Expand Up @@ -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,
];
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fc69fd6

Please sign in to comment.