packages/sql-semantics-mysql/src/Lowering/Server/Transaction/XaRule.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Server\Transaction;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\AnalysisException;
10use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
11use SqlSemantics\Platform\MySql\Model\Lowering\Form;
12use SqlSemantics\Platform\MySql\Model\Statement\Statement;
13use SqlSemantics\Platform\MySql\Rules\Server\Magnitudes;
14use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
15use SqlSemantics\Platform\MySql\Statement\Literal\Text;
16use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaCommit;
17use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaEnd;
18use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaEndOption;
19use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaPrepare;
20use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaRecover;
21use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaRollback;
22use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaStart;
23use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\XaStartOption;
24use SqlSemantics\Platform\MySql\Statement\Server\Transaction\Xa\Xid;
25
26/**
27 * Lowers the XA transaction statements.
28 *
29 * Rule: MYSQL-XA-001. Scope: xa, xid, begin_or_start, opt_join_or_resume,
30 * opt_one_phase, opt_suspend, opt_migrate (5.6), opt_convert_xid (5.7 and
31 * later). BEGIN and START are synonyms in XA START (ServerNoise). A global
32 * transaction identifier or branch qualifier longer than 64 bytes, and a
33 * format identifier above 2^63-1, are syntax errors the server raises in the
34 * `xid` action (MYSQL_YYABORT_UNLESS) and are rejected as such. Constructs:
35 * XaStart, XaEnd, XaPrepare, XaCommit, XaRollback, XaRecover, Xid.
36 * Terminates: every child is a strict subtree.
37 * Source: https://dev.mysql.com/doc/refman/8.4/en/xa-statements.html.
38 * Status: Implemented.
39 *
40 * @visibility SqlSemantics\Platform\MySql\Lowering\Server
41 */
42final class XaRule
43{
44 /**
45 * The option productions, by the option they select; null for an absent option.
46 */
47 private const OPTIONS = [
48 'opt_join_or_resume:' => null, 'opt_join_or_resume: JOIN_SYM' => XaStartOption::Join, 'opt_join_or_resume: RESUME_SYM' => XaStartOption::Resume,
49 'opt_suspend:' => null, 'opt_suspend: SUSPEND_SYM' => XaEndOption::Suspend,
50 'opt_suspend: SUSPEND_SYM FOR_SYM MIGRATE_SYM' => XaEndOption::SuspendForMigrate,
51 ];
52
53 /**
54 * The flag productions, by whether they write the words.
55 */
56 private const FLAGS = [
57 'opt_one_phase:' => false, 'opt_one_phase: ONE_SYM PHASE_SYM' => true, 'opt_convert_xid:' => false, 'opt_convert_xid: CONVERT_SYM XID_SYM' => true,
58 'begin_or_start: BEGIN_SYM' => true, 'begin_or_start: START_SYM' => true,
59 ];
60
61 /**
62 * @param Lowering $lowering The lowering this rule belongs to
63 */
64 public function __construct(private readonly Lowering $lowering)
65 {
66 }
67
68 /**
69 * Lowers an XA statement: a node of `xa`.
70 *
71 * @throws ImplementationGap When a production has no rule
72 * @throws AnalysisException When an identifier part exceeds its limit
73 */
74 public function statement(Form $form): Statement
75 {
76 return match ($form->signature) {
77 'xa: XA_SYM begin_or_start xid opt_join_or_resume' => $this->start($form),
78 'xa: XA_SYM END xid opt_suspend' => new XaEnd($this->xid($form->node(2)), $this->endOption($form->node(3))),
79 'xa: XA_SYM PREPARE_SYM xid' => new XaPrepare($this->xid($form->node(2))),
80 'xa: XA_SYM COMMIT_SYM xid opt_one_phase' => new XaCommit($this->xid($form->node(2)), $this->flag($form->node(3))),
81 'xa: XA_SYM ROLLBACK_SYM xid' => new XaRollback($this->xid($form->node(2))),
82 'xa: XA_SYM RECOVER_SYM' => new XaRecover(),
83 'xa: XA_SYM RECOVER_SYM opt_convert_xid' => new XaRecover($this->flag($form->node(2))),
84 default => throw ImplementationGap::production($form),
85 };
86 }
87
88 /**
89 * Lowers XA START or XA BEGIN.
90 *
91 * @throws ImplementationGap When a production has no rule
92 * @throws AnalysisException When an identifier part exceeds its limit
93 */
94 public function start(Form $form): XaStart
95 {
96 $this->flag($form->node(1));
97 $option = $this->option($form->node(3));
98
99 return new XaStart($this->xid($form->node(2)), $option instanceof XaStartOption ? $option : null);
100 }
101
102 /**
103 * Lowers the option of XA END: a node of `opt_suspend`.
104 *
105 * @throws ImplementationGap When a production has no rule
106 */
107 public function endOption(Node $option): ?XaEndOption
108 {
109 $form = $this->lowering->form($option);
110 if ($form->signature === 'opt_suspend: SUSPEND_SYM opt_migrate') {
111 $migrate = $this->lowering->form($form->node(1));
112
113 return match ($migrate->signature) {
114 'opt_migrate:' => XaEndOption::Suspend,
115 'opt_migrate: FOR_SYM MIGRATE_SYM' => XaEndOption::SuspendForMigrate,
116 default => throw ImplementationGap::production($migrate),
117 };
118 }
119 $lowered = $this->option($option);
120
121 return $lowered instanceof XaEndOption ? $lowered : null;
122 }
123
124 /**
125 * Lowers an optional option: a node of `opt_join_or_resume` or `opt_suspend`.
126 *
127 * @throws ImplementationGap When a production has no rule
128 */
129 public function option(Node $option): XaStartOption|XaEndOption|null
130 {
131 $form = $this->lowering->form($option);
132 if (!array_key_exists($form->signature, self::OPTIONS)) {
133 throw ImplementationGap::production($form);
134 }
135
136 return self::OPTIONS[$form->signature];
137 }
138
139 /**
140 * Lowers a flag: a node of `opt_one_phase`, `opt_convert_xid` or `begin_or_start`.
141 *
142 * @throws ImplementationGap When a production has no rule
143 */
144 public function flag(Node $flag): bool
145 {
146 $form = $this->lowering->form($flag);
147
148 return self::FLAGS[$form->signature] ?? throw ImplementationGap::production($form);
149 }
150
151 /**
152 * Lowers an XA transaction identifier: a node of `xid`.
153 *
154 * @throws ImplementationGap When a production has no rule
155 * @throws AnalysisException When a part exceeds its limit
156 */
157 public function xid(Node $xid): Xid
158 {
159 $form = $this->lowering->form($xid);
160 $literals = $this->lowering->literals;
161 [$transaction, $branch, $format] = match ($form->signature) {
162 'xid: text_string' => [$literals->text($form->node(0)), null, null],
163 'xid: text_string , text_string' => [$literals->text($form->node(0)), $literals->text($form->node(2)), null],
164 'xid: text_string , text_string , ulong_num' => [$literals->text($form->node(0)), $literals->text($form->node(2)), $this->lowering->numbers->numeral($form->node(4))],
165 default => throw ImplementationGap::production($form),
166 };
167 $this->limits($transaction, $branch, $format);
168
169 return new Xid($transaction, $branch, $format);
170 }
171
172 /**
173 * Rejects identifier parts the server rejects while it parses.
174 *
175 * @throws AnalysisException When a part exceeds its limit
176 */
177 public function limits(Text $transaction, ?Text $branch, ?Numeral $format): void
178 {
179 $magnitudes = new Magnitudes();
180 if ($magnitudes->bytes($transaction) > 64 || ($branch !== null && $magnitudes->bytes($branch) > 64)) {
181 throw new AnalysisException('Syntax error: an XA transaction identifier part holds at most 64 bytes.');
182 }
183 if ($format !== null && !$magnitudes->format($format, $this->lowering->profile->grammar)) {
184 throw new AnalysisException('Syntax error: an XA format identifier is at most 9223372036854775807.');
185 }
186 }
187}
188