packages/sql-semantics-mysql/src/Statement/Utility/Show/Program/ShowCreateProcedure.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Utility\Show\Program;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Model\Statement\Statement;
13use SqlSemantics\Platform\MySql\Rules\Utility\Report;
14use SqlSemantics\Platform\MySql\Rules\Utility\ShowFacts;
15
16/**
17 * SHOW CREATE PROCEDURE: the statement that creates a stored procedure.
18 *
19 * Rule: MYSQL-SHOW-CREATEPROCEDURE-001. Stored programs are not part of a context, so the name is not
20 * resolved. The columns are those of the layout of MYSQL-SHOW-ROWS-001.
21 * Terminates: a fixed layout.
22 * Source: https://dev.mysql.com/doc/refman/8.4/en/show-create-procedure.html.
23 * Status: Implemented.
24 *
25 * @visibility public
26 * @example Reading the statement
27 *     $show = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SHOW CREATE PROCEDURE shop.p');
28 *     [$show->statement->name->schema?->value, $show->toString()] // => ['shop', 'SHOW CREATE PROCEDURE shop.p']
29 */
30final class ShowCreateProcedure implements Statement
31{
32    use Snapshot;
33
34    /**
35     * @param QualifiedName $name The name with its optional database
36     */
37    public function __construct(public readonly QualifiedName $name)
38    {
39    }
40
41    /**
42     * Derives the rows.
43     */
44    public function deriveStatement(Derivation $derivation): void
45    {
46        (new ShowFacts())->rows($derivation, Report::CreateProcedure);
47    }
48
49    /**
50     * Writes the statement.
51     */
52    public function render(Output $out): void
53    {
54        $out->keyword('SHOW', 'CREATE', 'PROCEDURE');
55        if ($this->name->schema !== null) {
56            $out->name($this->name->schema, NameUse::Qualifier)->symbol('.');
57        }
58        $out->name($this->name->name, NameUse::Routine);
59    }
60}
61