Skip to content

Commit e518f58

Browse files
authored
Hide explain when empty for sqlite (#1565)
* Hide explain when empty for sqlite * Update QueryCollectorRuntimeDatabaseTest.php
1 parent a8c3ec8 commit e518f58

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

src/DataCollector/QueryCollector.php

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -531,62 +531,64 @@ public function collect()
531531
'connection' => $query['connection'],
532532
];
533533

534-
// Add the results from the EXPLAIN as new rows
535-
if ($query['driver'] === 'pgsql') {
536-
$explainer = trim(implode("\n", array_map(function ($explain) {
537-
return $explain->{'QUERY PLAN'};
538-
}, $query['explain'])));
534+
if ($query['explain']) {
535+
// Add the results from the EXPLAIN as new rows
536+
if ($query['driver'] === 'pgsql') {
537+
$explainer = trim(implode("\n", array_map(function ($explain) {
538+
return $explain->{'QUERY PLAN'};
539+
}, $query['explain'])));
540+
541+
if ($explainer) {
542+
$statements[] = [
543+
'sql' => " - EXPLAIN: {$explainer}",
544+
'type' => 'explain',
545+
];
546+
}
547+
} elseif ($query['driver'] === 'sqlite') {
548+
$vmi = '<table style="margin:-5px -11px !important;width: 100% !important">';
549+
$vmi .= "<thead><tr>
550+
<td>Address</td>
551+
<td>Opcode</td>
552+
<td>P1</td>
553+
<td>P2</td>
554+
<td>P3</td>
555+
<td>P4</td>
556+
<td>P5</td>
557+
<td>Comment</td>
558+
</tr></thead>";
559+
560+
foreach ($query['explain'] as $explain) {
561+
$vmi .= "<tr>
562+
<td>{$explain->addr}</td>
563+
<td>{$explain->opcode}</td>
564+
<td>{$explain->p1}</td>
565+
<td>{$explain->p2}</td>
566+
<td>{$explain->p3}</td>
567+
<td>{$explain->p4}</td>
568+
<td>{$explain->p5}</td>
569+
<td>{$explain->comment}</td>
570+
</tr>";
571+
}
539572

540-
if ($explainer) {
541-
$statements[] = [
542-
'sql' => " - EXPLAIN: {$explainer}",
543-
'type' => 'explain',
544-
];
545-
}
546-
} elseif ($query['driver'] === 'sqlite') {
547-
$vmi = '<table style="margin:-5px -11px !important;width: 100% !important">';
548-
$vmi .= "<thead><tr>
549-
<td>Address</td>
550-
<td>Opcode</td>
551-
<td>P1</td>
552-
<td>P2</td>
553-
<td>P3</td>
554-
<td>P4</td>
555-
<td>P5</td>
556-
<td>Comment</td>
557-
</tr></thead>";
558-
559-
foreach ($query['explain'] as $explain) {
560-
$vmi .= "<tr>
561-
<td>{$explain->addr}</td>
562-
<td>{$explain->opcode}</td>
563-
<td>{$explain->p1}</td>
564-
<td>{$explain->p2}</td>
565-
<td>{$explain->p3}</td>
566-
<td>{$explain->p4}</td>
567-
<td>{$explain->p5}</td>
568-
<td>{$explain->comment}</td>
569-
</tr>";
570-
}
573+
$vmi .= '</table>';
571574

572-
$vmi .= '</table>';
573-
574-
$statements[] = [
575-
'sql' => " - EXPLAIN:",
576-
'type' => 'explain',
577-
'params' => [
578-
'Virtual Machine Instructions' => $vmi,
579-
]
580-
];
581-
} else {
582-
foreach ($query['explain'] as $explain) {
583575
$statements[] = [
584-
'sql' => " - EXPLAIN # {$explain->id}: `{$explain->table}` ({$explain->select_type})",
576+
'sql' => " - EXPLAIN:",
585577
'type' => 'explain',
586-
'params' => $explain,
587-
'row_count' => $explain->rows,
588-
'stmt_id' => $explain->id,
578+
'params' => [
579+
'Virtual Machine Instructions' => $vmi,
580+
]
589581
];
582+
} else {
583+
foreach ($query['explain'] as $explain) {
584+
$statements[] = [
585+
'sql' => " - EXPLAIN # {$explain->id}: `{$explain->table}` ({$explain->select_type})",
586+
'type' => 'explain',
587+
'params' => $explain,
588+
'row_count' => $explain->rows,
589+
'stmt_id' => $explain->id,
590+
];
591+
}
590592
}
591593
}
592594
}

tests/DataCollector/QueryCollectorRuntimeDatabaseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testCollectsQueriesFromRuntimeConnections()
4747
tap($collector->collect(), function (array $collection) {
4848
$this->assertEquals(1, $collection['nb_statements']);
4949

50-
self::assertSame('SELECT 1', $collection['statements'][2]['sql']);
50+
self::assertSame('SELECT 1', $collection['statements'][1]['sql']);
5151
});
5252
}
5353

@@ -78,7 +78,7 @@ public function testCollectsQueriesFromRuntimeConnectionsWithoutConnectUsing()
7878
tap($collector->collect(), function (array $collection) {
7979
$this->assertEquals(1, $collection['nb_statements']);
8080

81-
self::assertSame('SELECT 1', $collection['statements'][2]['sql']);
81+
self::assertSame('SELECT 1', $collection['statements'][1]['sql']);
8282
});
8383
}
8484
}

0 commit comments

Comments
 (0)