packages/sql-semantics-mysql/src/Statement/Routine/Program/Cursor/OpenCursor.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Routine\Program\Cursor;
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\Name;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Rules\Routine\CursorFacts;
13use SqlSemantics\Platform\MySql\Rules\Routine\ProgramScope;
14use SqlSemantics\Platform\MySql\Statement\Routine\Program\ProgramStatement;
15
16/**
17 * OPEN: opens a declared cursor.
18 *
19 * The facts follow MYSQL-PROGRAM-CURSORS-001.
20 * Source: https://dev.mysql.com/doc/refman/8.4/en/open.html.
21 *
22 * @visibility public
23 * @example Reading the cursor an OPEN names
24 *     (new \SqlSemantics\Platform\MySql\Semantics())->analyze('CREATE PROCEDURE p() OPEN c') // throws \SqlSemantics\Platform\MySql\Model\Diagnostic\SemanticException: Undefined CURSOR: c
25 */
26final class OpenCursor implements ProgramStatement
27{
28    use Snapshot;
29
30    /**
31     * @param Name $cursor The cursor name
32     */
33    public function __construct(public readonly Name $cursor)
34    {
35    }
36
37    /**
38     * Checks that the cursor is declared.
39     */
40    public function deriveProgram(Derivation $derivation, ProgramScope $scope): void
41    {
42        (new CursorFacts())->cursor($this->cursor, $derivation, $scope);
43    }
44
45    /**
46     * Writes the statement.
47     */
48    public function render(Output $out): void
49    {
50        $out->keyword('OPEN')->name($this->cursor, NameUse::Identifier);
51    }
52}
53