-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ui): share database EXPLAIN markup across debugger adapters. #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PHPForge\Debug\Panel\Db; | ||
|
|
||
| use PHPForge\Debug\Helper\Dump; | ||
| use UIAwesome\Html\Flow\{Div, P, Pre}; | ||
| use UIAwesome\Html\Heading\H1; | ||
| use UIAwesome\Html\Phrasing\Em; | ||
| use UIAwesome\Html\Table\{Table, Tbody, Td, Th, Thead, Tr}; | ||
|
|
||
| use function array_keys; | ||
| use function array_values; | ||
| use function is_scalar; | ||
|
|
||
| /** | ||
| * Renders a database EXPLAIN plan with the shared debugger table contract. | ||
| */ | ||
| final class DbExplainRenderer | ||
| { | ||
| /** | ||
| * Renders the explained SQL statement and its driver-specific result rows. | ||
| * | ||
| * @param string $query SQL statement passed to EXPLAIN. | ||
| * @param array<array-key, array<array-key, mixed>> $results Driver result rows. | ||
| */ | ||
| public static function render(string $query, array $results): string | ||
| { | ||
| return self::renderPlan($query, $results, null); | ||
| } | ||
|
|
||
| /** | ||
| * Renders a failed EXPLAIN attempt without escaping the shared panel contract. | ||
| */ | ||
| public static function renderError(string $query, string $message): string | ||
| { | ||
| return self::renderPlan($query, [], $message); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<array-key, array<array-key, mixed>> $results Driver result rows. | ||
| */ | ||
| private static function renderPlan(string $query, array $results, string|null $error): string | ||
| { | ||
| $resultList = array_values($results); | ||
|
|
||
| $columns = $resultList === [] ? [] : array_keys($resultList[0]); | ||
|
|
||
| $children = [ | ||
| H1::tag() | ||
| ->class('yii-debug-explain-title') | ||
| ->content('EXPLAIN'), | ||
| ]; | ||
|
|
||
| if ($query !== '') { | ||
| $children[] = Pre::tag() | ||
| ->class('yii-debug-explain-query') | ||
| ->html(SqlHighlighter::highlight($query)); | ||
| } | ||
|
|
||
| if ($error !== null) { | ||
| $children[] = P::tag() | ||
| ->class('yii-debug-explain-empty') | ||
| ->content("EXPLAIN failed: {$error}"); | ||
| } elseif ($results === []) { | ||
| $children[] = P::tag() | ||
| ->class('yii-debug-explain-empty') | ||
| ->content('EXPLAIN returned no rows.'); | ||
| } else { | ||
| $headerCells = []; | ||
|
|
||
| foreach ($columns as $column) { | ||
| $headerCells[] = Th::tag()->scope('col')->content((string) $column); | ||
| } | ||
|
|
||
| $bodyRows = []; | ||
|
|
||
| foreach ($results as $row) { | ||
| $cells = []; | ||
|
|
||
| foreach ($columns as $column) { | ||
| $value = $row[$column] ?? null; | ||
| $cells[] = $value === null | ||
| ? Td::tag()->html(Em::tag()->content('NULL')) | ||
| : Td::tag()->content(is_scalar($value) ? (string) $value : Dump::export($value)); | ||
| } | ||
|
|
||
| $bodyRows[] = Tr::tag()->html(...$cells); | ||
| } | ||
|
|
||
| $children[] = Div::tag() | ||
| ->class('yii-debug-explain-scroll') | ||
| ->html( | ||
| Table::tag() | ||
| ->class('yii-debug-table yii-debug-explain-table') | ||
| ->html( | ||
| Thead::tag()->html(Tr::tag()->html(...$headerCells)), | ||
| Tbody::tag()->html(...$bodyRows), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| return Div::tag() | ||
| ->class('yii-debug-explain') | ||
| ->html(...$children) | ||
| ->render(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PHPForge\Debug\Tests\Panel\Db; | ||
|
|
||
| use PHPForge\Debug\Panel\Db\DbExplainRenderer; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * Unit tests for {@see DbExplainRenderer} covering empty, failed, and tabular EXPLAIN result markup. | ||
| */ | ||
| #[Group('panel')] | ||
| #[Group('db')] | ||
| final class DbExplainRendererTest extends TestCase | ||
| { | ||
| public function testRenderEmptyPlan(): void | ||
| { | ||
| self::assertSame( | ||
| <<<'HTML' | ||
| <div class="yii-debug-explain"> | ||
| <h1 class="yii-debug-explain-title"> | ||
| EXPLAIN | ||
| </h1><p class="yii-debug-explain-empty"> | ||
| EXPLAIN returned no rows. | ||
| </p> | ||
| </div> | ||
| HTML, | ||
| DbExplainRenderer::render('', []), | ||
| 'Empty plans must keep the shared title and empty-state contract.', | ||
| ); | ||
| } | ||
|
|
||
| public function testRenderError(): void | ||
| { | ||
| self::assertSame( | ||
| <<<'HTML' | ||
| <div class="yii-debug-explain"> | ||
| <h1 class="yii-debug-explain-title"> | ||
| EXPLAIN | ||
| </h1><pre class="yii-debug-explain-query"> | ||
| <span class="yii-debug-sql-kw">SELECT</span> <span class="yii-debug-sql-num">1</span> | ||
| </pre><p class="yii-debug-explain-empty"> | ||
| EXPLAIN failed: database <unavailable> | ||
| </p> | ||
| </div> | ||
| HTML, | ||
| DbExplainRenderer::renderError('SELECT 1', 'database <unavailable>'), | ||
| 'Failed plans must preserve the query and escape the driver error exactly.', | ||
| ); | ||
| } | ||
|
|
||
| public function testRenderQueryAndResultTable(): void | ||
| { | ||
| self::assertSame( | ||
| <<<'HTML' | ||
| <div class="yii-debug-explain"> | ||
| <h1 class="yii-debug-explain-title"> | ||
| EXPLAIN | ||
| </h1><pre class="yii-debug-explain-query"> | ||
| <span class="yii-debug-sql-kw">SELECT</span> * <span class="yii-debug-sql-kw">FROM</span> users | ||
| </pre><div class="yii-debug-explain-scroll"> | ||
| <table class="yii-debug-table yii-debug-explain-table"> | ||
| <thead> | ||
| <tr> | ||
| <th scope="col"> | ||
| id | ||
| </th><th scope="col"> | ||
| detail | ||
| </th> | ||
| </tr> | ||
| </thead><tbody> | ||
| <tr> | ||
| <td> | ||
| 1 | ||
| </td><td> | ||
| SCAN users | ||
| </td> | ||
| </tr><tr> | ||
| <td> | ||
| <em>NULL</em> | ||
| </td><td> | ||
| SEARCH users | ||
| </td> | ||
| </tr><tr> | ||
| <td> | ||
| 2 | ||
| </td><td> | ||
| [ | ||
| 'operator' => '<scan>', | ||
| ] | ||
| </td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| </div> | ||
| HTML, | ||
| DbExplainRenderer::render( | ||
| 'SELECT * FROM users', | ||
| [ | ||
| ['id' => 1, 'detail' => 'SCAN users'], | ||
| ['id' => null, 'detail' => 'SEARCH users'], | ||
| ['id' => 2, 'detail' => ['operator' => '<scan>']], | ||
| ], | ||
| ), | ||
| 'A populated plan must render scalar, null, and escaped non-scalar cells exactly.', | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.