packages/sql-semantics-mysql/src/Statement/Routine/Condition/Diagnostics/StatementDiagnostics.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics;
6
7use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Statement\Node;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11
12/**
13 * The statement information GET DIAGNOSTICS reads: the number of conditions and the affected-rows count.
14 *
15 * Source: https://dev.mysql.com/doc/refman/8.4/en/get-diagnostics.html.
16 *
17 * @visibility public
18 * @example Counting the items read
19 *     $get = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('GET DIAGNOSTICS @n = NUMBER');
20 *     count($get->statement->information->items) // => 1
21 */
22final class StatementDiagnostics implements Node
23{
24    use Snapshot;
25
26    /**
27     * @var non-empty-list<InformationItem> The assignments in written order
28     */
29    public readonly array $items;
30
31    /**
32     * @param list<InformationItem> $items The assignments; at least one, each of a statement information item
33     */
34    public function __construct(array $items)
35    {
36        $this->items = Check::listOf($items, InformationItem::class, 'GET DIAGNOSTICS reads at least one item.', 1);
37        foreach ($this->items as $item) {
38            Check::input($item->item instanceof StatementItemName, 'Statement information holds statement information items.');
39        }
40    }
41
42    /**
43     * Writes the assignments.
44     */
45    public function render(Output $out): void
46    {
47        $out->list($this->items);
48    }
49}
50