Skip to content

Commit d22f42f

Browse files
authored
Do not rely on DB::connection() to get information in query collector (#1779)
Instead directly use the query's own connection object. This makes it possible to log queries for custom non-sql connections into debugbar relatively easily. Related: ed37d1a This commit introduced the usage of DB::connection(). Resolves: #1778
1 parent 58fe15b commit d22f42f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/DataCollector/QueryCollector.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function addQuery($query)
196196
'time' => $time,
197197
'memory' => $this->lastMemoryUsage ? memory_get_usage(false) - $this->lastMemoryUsage : 0,
198198
'source' => $source,
199-
'connection' => $query->connection->getName(),
199+
'connection' => $query->connection,
200200
'driver' => $query->connection->getConfig('driver'),
201201
'hints' => ($this->showHints && !$limited) ? $hints : null,
202202
'show_copy' => $this->showCopyButton,
@@ -459,7 +459,7 @@ public function collectTransactionEvent($event, $connection)
459459
'time' => 0,
460460
'memory' => 0,
461461
'source' => $source,
462-
'connection' => $connection->getName(),
462+
'connection' => $connection,
463463
'driver' => $connection->getConfig('driver'),
464464
'hints' => null,
465465
'show_copy' => false,
@@ -496,7 +496,7 @@ public function collect()
496496
$totalTime += $query['time'];
497497
$totalMemory += $query['memory'];
498498

499-
$connectionName = DB::connection($query['connection'])->getDatabaseName();
499+
$connectionName = $query['connection']->getDatabaseName();
500500
if (str_ends_with($connectionName, '.sqlite')) {
501501
$connectionName = $this->normalizeFilePath($connectionName);
502502
}
@@ -526,9 +526,9 @@ public function collect()
526526
'explain' => $this->explainQuery && $canExplainQuery ? [
527527
'url' => route('debugbar.queries.explain'),
528528
'driver' => $query['driver'],
529-
'connection' => $query['connection'],
529+
'connection' => $query['connection']->getName(),
530530
'query' => $query['query'],
531-
'hash' => (new Explain())->hash($query['connection'], $query['query'], $query['bindings']),
531+
'hash' => (new Explain())->hash($query['connection']->getName(), $query['query'], $query['bindings']),
532532
] : null,
533533
];
534534
}
@@ -630,9 +630,9 @@ public function getWidgets()
630630
private function getSqlQueryToDisplay(array $query): string
631631
{
632632
$sql = $query['query'];
633-
if ($query['type'] === 'query' && $this->renderSqlWithParams && DB::connection($query['connection'])->getQueryGrammar() instanceof \Illuminate\Database\Query\Grammars\Grammar && method_exists(DB::connection($query['connection'])->getQueryGrammar(), 'substituteBindingsIntoRawSql')) {
633+
if ($query['type'] === 'query' && $this->renderSqlWithParams && $query['connection']->getQueryGrammar() instanceof \Illuminate\Database\Query\Grammars\Grammar && method_exists($query['connection']->getQueryGrammar(), 'substituteBindingsIntoRawSql')) {
634634
try {
635-
$sql = DB::connection($query['connection'])->getQueryGrammar()->substituteBindingsIntoRawSql($sql, $query['bindings'] ?? []);
635+
$sql = $query['connection']->getQueryGrammar()->substituteBindingsIntoRawSql($sql, $query['bindings'] ?? []);
636636
return $this->getDataFormatter()->formatSql($sql);
637637
} catch (\Throwable $e) {
638638
// Continue using the old substitute

0 commit comments

Comments
 (0)