packages/sql-semantics-mysql/src/Statement/Routine/Characteristic/RoutineLanguage.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Routine\Characteristic;
6
7use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11
12/**
13 * The LANGUAGE characteristic of a stored routine: SQL, or from MySQL 8.1 the name of an external language.
14 *
15 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-procedure.html,
16 * https://dev.mysql.com/doc/refman/9.1/en/create-procedure.html.
17 *
18 * @visibility public
19 * @example Reading the language of a routine
20 *     $alter = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('ALTER FUNCTION f LANGUAGE SQL');
21 *     $alter->statement->characteristics[0]->external // => null
22 */
23final class RoutineLanguage implements Characteristic
24{
25    use Snapshot;
26
27    /**
28     * @param Name|null $external The name of the external language; null for the keyword SQL
29     */
30    public function __construct(public readonly ?Name $external = null)
31    {
32    }
33
34    /**
35     * Writes LANGUAGE and the keyword SQL or the language name.
36     */
37    public function render(Output $out): void
38    {
39        $out->keyword('LANGUAGE');
40        if ($this->external === null) {
41            $out->keyword('SQL');
42        } else {
43            $out->name($this->external, NameUse::Identifier);
44        }
45    }
46}
47