packages/sql-semantics-mysql/src/Lowering/Type/StringTypeRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Type;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Model\Lowering\Form;
11use SqlSemantics\Platform\MySql\Statement\Type\Binary;
12use SqlSemantics\Platform\MySql\Statement\Type\Character;
13use SqlSemantics\Platform\MySql\Statement\Type\Enumeration;
14use SqlSemantics\Platform\MySql\Statement\Type\Kind\BinaryKind;
15use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
16use SqlSemantics\Platform\MySql\Statement\Type\Kind\EnumerationKind;
17use SqlSemantics\Platform\MySql\Statement\Type\TypeName;
18
19/**
20 * Lowers the character string, binary string and enumeration type productions.
21 *
22 * Rule: MYSQL-STRING-TYPE-001. Scope: the string alternatives of type, and
23 * char, nchar, varchar, nvarchar. Each production names its kind and the
24 * positions of its length and character set attribute. The spellings of the
25 * national types are one kind each; `CHAR VARYING` and `LONG CHAR VARYING`
26 * are kinds of their own. Constructs: Character, Binary, Enumeration.
27 * Terminates: every child is a strict subtree.
28 * Source: https://dev.mysql.com/doc/refman/8.4/en/string-type-syntax.html.
29 * Status: Implemented.
30 *
31 * @visibility SqlSemantics\Platform\MySql
32 */
33final class StringTypeRule
34{
35    /**
36     * The character type productions: kind, position of the length, position of the character set attribute.
37     */
38    private const CHARACTERS = [
39        'type: char field_length opt_binary' => [CharacterKind::Char, 1, 2], 'type: char opt_binary' => [CharacterKind::Char, null, 1],
40        'type: CHAR_SYM field_length opt_charset_with_opt_binary' => [CharacterKind::Char, 1, 2],
41        'type: CHAR_SYM opt_charset_with_opt_binary' => [CharacterKind::Char, null, 1],
42        'type: TINYTEXT opt_binary' => [CharacterKind::TinyText, null, 1], 'type: TINYTEXT_SYN opt_charset_with_opt_binary' => [CharacterKind::TinyText, null, 1],
43        'type: TEXT_SYM opt_field_length opt_binary' => [CharacterKind::Text, 1, 2],
44        'type: TEXT_SYM opt_field_length opt_charset_with_opt_binary' => [CharacterKind::Text, 1, 2],
45        'type: MEDIUMTEXT opt_binary' => [CharacterKind::MediumText, null, 1],
46        'type: MEDIUMTEXT_SYM opt_charset_with_opt_binary' => [CharacterKind::MediumText, null, 1],
47        'type: LONGTEXT opt_binary' => [CharacterKind::LongText, null, 1], 'type: LONGTEXT_SYM opt_charset_with_opt_binary' => [CharacterKind::LongText, null, 1],
48        'type: LONG_SYM opt_binary' => [CharacterKind::Long, null, 1], 'type: LONG_SYM opt_charset_with_opt_binary' => [CharacterKind::Long, null, 1],
49    ];
50
51    /**
52     * The VARCHAR productions written through the rule varchar: whether LONG precedes, position of the length, position of the attribute.
53     */
54    private const VARYING = [
55        'type: varchar field_length opt_binary' => [false, 1, 2], 'type: varchar field_length opt_charset_with_opt_binary' => [false, 1, 2],
56        'type: LONG_SYM varchar opt_binary' => [true, null, 2], 'type: LONG_SYM varchar opt_charset_with_opt_binary' => [true, null, 2],
57    ];
58
59    /**
60     * The spellings of the rule varchar, by whether they are written `CHAR VARYING`.
61     */
62    private const VARCHAR = ['varchar: char VARYING' => true, 'varchar: VARCHAR' => false, 'varchar: CHAR_SYM VARYING' => true, 'varchar: VARCHAR_SYM' => false];
63
64    /**
65     * The national type productions: kind, position of the length, position of the BINARY attribute.
66     */
67    private const NATIONALS = [
68        'type: nchar field_length opt_bin_mod' => [CharacterKind::Char, 1, 2], 'type: nchar opt_bin_mod' => [CharacterKind::Char, null, 1],
69        'type: nvarchar field_length opt_bin_mod' => [CharacterKind::VarChar, 1, 2],
70    ];
71
72    /**
73     * The spellings of the CHAR, NCHAR and NVARCHAR keywords, which are one kind each.
74     */
75    private const KEYWORDS = [
76        'char: CHAR_SYM' => true, 'nchar: NCHAR_SYM' => true, 'nchar: NATIONAL_SYM CHAR_SYM' => true, 'nvarchar: NATIONAL_SYM VARCHAR' => true,
77        'nvarchar: NVARCHAR_SYM' => true, 'nvarchar: NCHAR_SYM VARCHAR' => true, 'nvarchar: NATIONAL_SYM CHAR_SYM VARYING' => true,
78        'nvarchar: NCHAR_SYM VARYING' => true, 'nvarchar: NATIONAL_SYM VARCHAR_SYM' => true, 'nvarchar: NCHAR_SYM VARCHAR_SYM' => true,
79    ];
80
81    /**
82     * The binary type productions: kind and position of the length.
83     */
84    private const BINARIES = [
85        'type: BINARY field_length' => [BinaryKind::Binary, 1], 'type: BINARY' => [BinaryKind::Binary, null],
86        'type: BINARY_SYM field_length' => [BinaryKind::Binary, 1], 'type: BINARY_SYM' => [BinaryKind::Binary, null],
87        'type: VARBINARY field_length' => [BinaryKind::VarBinary, 1], 'type: VARBINARY_SYM field_length' => [BinaryKind::VarBinary, 1],
88        'type: TINYBLOB' => [BinaryKind::TinyBlob, null], 'type: TINYBLOB_SYM' => [BinaryKind::TinyBlob, null],
89        'type: BLOB_SYM opt_field_length' => [BinaryKind::Blob, 1], 'type: MEDIUMBLOB' => [BinaryKind::MediumBlob, null],
90        'type: MEDIUMBLOB_SYM' => [BinaryKind::MediumBlob, null], 'type: LONGBLOB' => [BinaryKind::LongBlob, null],
91        'type: LONGBLOB_SYM' => [BinaryKind::LongBlob, null], 'type: LONG_SYM VARBINARY' => [BinaryKind::LongVarBinary, null],
92        'type: LONG_SYM VARBINARY_SYM' => [BinaryKind::LongVarBinary, null],
93    ];
94
95    /**
96     * The ENUM and SET productions.
97     */
98    private const ENUMERATIONS = [
99        'type: ENUM ( string_list ) opt_binary' => EnumerationKind::Enum, 'type: SET ( string_list ) opt_binary' => EnumerationKind::Set,
100        'type: ENUM_SYM ( string_list ) opt_charset_with_opt_binary' => EnumerationKind::Enum,
101        'type: SET_SYM ( string_list ) opt_charset_with_opt_binary' => EnumerationKind::Set,
102    ];
103
104    /**
105     * @param Lowering $lowering The lowering this rule belongs to
106     * @param TypePartRule $parts The rules of lengths and attributes
107     */
108    public function __construct(private readonly Lowering $lowering, private readonly TypePartRule $parts)
109    {
110    }
111
112    /**
113     * Lowers a string or enumeration type production, or answers null for another kind of type.
114     *
115     * @throws ImplementationGap When a part has no rule
116     */
117    public function type(Form $form): ?TypeName
118    {
119        if (isset(self::CHARACTERS[$form->signature])) {
120            [$kind, $length, $charset] = self::CHARACTERS[$form->signature];
121            if ($form->signature === 'type: char field_length opt_binary' || $form->signature === 'type: char opt_binary') {
122                $this->keyword($form->node(0));
123            }
124
125            return new Character($kind, $length === null ? null : $this->parts->length($form->node($length)), false, $this->parts->charset($form->node($charset)));
126        }
127        if (isset(self::BINARIES[$form->signature])) {
128            [$kind, $length] = self::BINARIES[$form->signature];
129
130            return new Binary($kind, $length === null ? null : $this->parts->length($form->node($length)));
131        }
132        if (isset(self::ENUMERATIONS[$form->signature])) {
133            return new Enumeration(self::ENUMERATIONS[$form->signature], $this->lowering->literals->texts($form->node(2)), $this->parts->charset($form->node(4)));
134        }
135
136        return $this->varying($form) ?? $this->national($form);
137    }
138
139    /**
140     * Lowers a VARCHAR type written through the rule varchar, or answers null for another kind of type.
141     *
142     * @throws ImplementationGap When a part has no rule
143     */
144    public function varying(Form $form): ?Character
145    {
146        if (!isset(self::VARYING[$form->signature])) {
147            return null;
148        }
149        [$long, $length, $charset] = self::VARYING[$form->signature];
150        $keyword = $this->lowering->productions->form($form->node($long ? 1 : 0));
151        $spelledVarying = self::VARCHAR[$keyword->signature] ?? throw ImplementationGap::production($keyword);
152        if ($keyword->signature === 'varchar: char VARYING') {
153            $this->keyword($keyword->node(0));
154        }
155        $kind = match (true) {
156            $long && $spelledVarying => CharacterKind::LongCharVarying,
157            $long => CharacterKind::LongVarChar,
158            $spelledVarying => CharacterKind::CharVarying,
159            default => CharacterKind::VarChar,
160        };
161
162        return new Character($kind, $length === null ? null : $this->parts->length($form->node($length)), false, $this->parts->charset($form->node($charset)));
163    }
164
165    /**
166     * Lowers a national character type, or answers null for another kind of type.
167     *
168     * @throws ImplementationGap When a part has no rule
169     */
170    public function national(Form $form): ?Character
171    {
172        if (!isset(self::NATIONALS[$form->signature])) {
173            return null;
174        }
175        [$kind, $length, $binary] = self::NATIONALS[$form->signature];
176        $this->keyword($form->node(0));
177
178        return new Character($kind, $length === null ? null : $this->parts->length($form->node($length)), true, $this->parts->nationalAttribute($form->node($binary)));
179    }
180
181    /**
182     * Confirms that a node is one of the spellings of CHAR, NCHAR or NVARCHAR.
183     *
184     * @throws ImplementationGap When the production has no rule
185     */
186    public function keyword(Node $keyword): void
187    {
188        $form = $this->lowering->productions->form($keyword);
189        if (!isset(self::KEYWORDS[$form->signature])) {
190            throw ImplementationGap::production($form);
191        }
192    }
193}
194