packages/sql-semantics-mysql/src/Rules/Routine/CursorFacts.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Rules\Routine;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
9use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramProblem;
10use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramRule;
11
12/**
13 * Derives the cursor statements OPEN, FETCH and CLOSE.
14 *
15 * Rule: MYSQL-PROGRAM-CURSORS-001. The cursor must be declared in an
16 * enclosing block (ER_SP_CURSOR_MISMATCH), and every target of FETCH must
17 * be a parameter or local variable in scope (ER_SP_UNDECLARED_VAR). Whether
18 * the cursor is open, and whether the number of targets equals the number
19 * of columns of the cursor query, is checked only when the program runs.
20 * Terminates: the lists are finite.
21 * Source: https://dev.mysql.com/doc/refman/8.4/en/cursors.html.
22 * Status: Implemented.
23 *
24 * @visibility SqlSemantics\Platform\MySql
25 */
26final class CursorFacts
27{
28 /**
29 * Reports a cursor that is not declared around the statement.
30 */
31 public function cursor(Name $cursor, Derivation $derivation, ProgramScope $scope): void
32 {
33 if (!$scope->holds($scope->cursors, $cursor)) {
34 $derivation->report(new ProgramProblem(ProgramRule::UndefinedCursor, $cursor->value));
35 }
36 }
37
38 /**
39 * Reports every name that is no parameter or local variable in scope.
40 *
41 * @param list<Name> $variables
42 */
43 public function variables(array $variables, Derivation $derivation, ProgramScope $scope): void
44 {
45 foreach ($variables as $variable) {
46 if (!$scope->variable($variable)) {
47 $derivation->report(new ProgramProblem(ProgramRule::UndeclaredVariable, $variable->value));
48 }
49 }
50 }
51}
52