Skip to content

Commit 9ec8bbb

Browse files
authored
Refactor old laravel query code (#1518)
1 parent 9f00dcd commit 9ec8bbb

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

src/DataCollector/QueryCollector.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,9 @@ public function startMemoryUsage()
139139

140140
/**
141141
*
142-
* @param string $query
143-
* @param array $bindings
144-
* @param float $time
145-
* @param \Illuminate\Database\Connection $connection
142+
* @param \Illuminate\Database\Events\QueryExecuted $query
146143
*/
147-
public function addQuery($query, $bindings, $time, $connection)
144+
public function addQuery($query)
148145
{
149146
$this->queryCount++;
150147

@@ -154,24 +151,24 @@ public function addQuery($query, $bindings, $time, $connection)
154151

155152
$limited = $this->softLimit && $this->queryCount > $this->softLimit;
156153

157-
154+
$sql = (string) $query->sql;
158155
$explainResults = [];
159-
$time = $time / 1000;
156+
$time = $query->time / 1000;
160157
$endTime = microtime(true);
161158
$startTime = $endTime - $time;
162-
$hints = $this->performQueryAnalysis($query);
159+
$hints = $this->performQueryAnalysis($sql);
163160

164161
$pdo = null;
165162
try {
166-
$pdo = $connection->getPdo();
163+
$pdo = $query->connection->getPdo();
167164
} catch (\Throwable $e) {
168165
// ignore error for non-pdo laravel drivers
169166
}
170-
$bindings = $connection->prepareBindings($bindings);
167+
$bindings = $query->connection->prepareBindings($query->bindings);
171168

172169
// Run EXPLAIN on this query (if needed)
173-
if (!$limited && $this->explainQuery && $pdo && preg_match('/^\s*(' . implode('|', $this->explainTypes) . ') /i', $query)) {
174-
$statement = $pdo->prepare('EXPLAIN ' . $query);
170+
if (!$limited && $this->explainQuery && $pdo && preg_match('/^\s*(' . implode('|', $this->explainTypes) . ') /i', $sql)) {
171+
$statement = $pdo->prepare('EXPLAIN ' . $sql);
175172
$statement->execute($bindings);
176173
$explainResults = $statement->fetchAll(\PDO::FETCH_CLASS);
177174
}
@@ -199,7 +196,7 @@ public function addQuery($query, $bindings, $time, $connection)
199196
}
200197
}
201198

202-
$query = preg_replace($regex, addcslashes($binding, '$'), $query, 1);
199+
$sql = preg_replace($regex, addcslashes($binding, '$'), $sql, 1);
203200
}
204201
}
205202

@@ -213,16 +210,16 @@ public function addQuery($query, $bindings, $time, $connection)
213210
}
214211

215212
$this->queries[] = [
216-
'query' => $query,
213+
'query' => $sql,
217214
'type' => 'query',
218215
'bindings' => !$limited ? $this->getDataFormatter()->escapeBindings($bindings) : null,
219216
'start' => $startTime,
220217
'time' => $time,
221218
'memory' => $this->lastMemoryUsage ? memory_get_usage(false) - $this->lastMemoryUsage : 0,
222219
'source' => $source,
223220
'explain' => $explainResults,
224-
'connection' => $connection->getDatabaseName(),
225-
'driver' => $connection->getConfig('driver'),
221+
'connection' => $query->connection->getDatabaseName(),
222+
'driver' => $query->connection->getConfig('driver'),
226223
'hints' => ($this->showHints && !$limited) ? $hints : null,
227224
'show_copy' => $this->showCopyButton,
228225
];

src/LaravelDebugbar.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,20 +371,15 @@ function ($level, $message = null, $context = null) use ($logger) {
371371

372372
try {
373373
$db->listen(
374-
function (\Illuminate\Database\Events\QueryExecuted $query) use ($db, $queryCollector) {
374+
function (\Illuminate\Database\Events\QueryExecuted $query) {
375375
if (!app(static::class)->shouldCollect('db', true)) {
376376
return; // Issue 776 : We've turned off collecting after the listener was attached
377377
}
378378

379-
$bindings = $query->bindings;
380-
$time = $query->time;
381-
$connection = $query->connection;
382-
$query = $query->sql;
383-
384379
//allow collecting only queries slower than a specified amount of milliseconds
385380
$threshold = app('config')->get('debugbar.options.db.slow_threshold', false);
386-
if (!$threshold || $time > $threshold) {
387-
$queryCollector->addQuery((string)$query, $bindings, $time, $connection);
381+
if (!$threshold || $query->time > $threshold) {
382+
$this['queries']->addQuery($query);
388383
}
389384
}
390385
);

tests/DataCollector/QueryCollectorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Barryvdh\Debugbar\Tests\DataCollector;
44

55
use Barryvdh\Debugbar\Tests\TestCase;
6+
use Illuminate\Database\Events\QueryExecuted;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Support\Arr;
89

@@ -18,12 +19,12 @@ public function testItReplacesQuestionMarksBindingsCorrectly()
1819

1920
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
2021
$collector = debugbar()->getCollector('queries');
21-
$collector->addQuery(
22+
$collector->addQuery(new QueryExecuted(
2223
"SELECT ('[1, 2, 3]'::jsonb ?? ?) as a, ('[4, 5, 6]'::jsonb ??| ?) as b, 'hello world ? example ??' as c",
2324
[3, '{4}'],
2425
0,
2526
$this->app['db']->connection()
26-
);
27+
));
2728

2829
tap($collector->collect(), function (array $collection) {
2930
$this->assertEquals(1, $collection['nb_statements']);
@@ -44,12 +45,12 @@ public function testDollarBindingsArePresentedCorrectly()
4445

4546
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
4647
$collector = debugbar()->getCollector('queries');
47-
$collector->addQuery(
48+
$collector->addQuery(new QueryExecuted(
4849
"SELECT a FROM b WHERE c = ? AND d = ? AND e = ?",
4950
['$10', '$2y$10_DUMMY_BCRYPT_HASH', '$_$$_$$$_$2_$3'],
5051
0,
5152
$this->app['db']->connection()
52-
);
53+
));
5354

5455
tap(Arr::first($collector->collect()['statements']), function (array $statement) {
5556
$this->assertEquals(

0 commit comments

Comments
 (0)