packages/sql-semantics-mysql/src/Lowering/Replication/LogRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Replication;
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\Model\Statement\Statement;
12use SqlSemantics\Platform\MySql\Statement\Replication\Group\Credential;
13use SqlSemantics\Platform\MySql\Statement\Replication\Group\CredentialOption;
14use SqlSemantics\Platform\MySql\Statement\Replication\Group\StartGroupReplication;
15use SqlSemantics\Platform\MySql\Statement\Replication\Group\StopGroupReplication;
16use SqlSemantics\Platform\MySql\Statement\Replication\Log\BinlogEvent;
17use SqlSemantics\Platform\MySql\Statement\Replication\Log\PurgeLogsBefore;
18use SqlSemantics\Platform\MySql\Statement\Replication\Log\PurgeLogsTo;
19
20/**
21 * Lowers PURGE BINARY LOGS, BINLOG and START and STOP GROUP_REPLICATION.
22 *
23 * Rule: MYSQL-REPLICATION-LOG-001. Scope: purge, purge_options, purge_option,
24 * binlog_base64_event, group_replication, group_replication_start,
25 * opt_group_replication_start_options, group_replication_start_options,
26 * group_replication_start_option, group_replication_user,
27 * group_replication_password, group_replication_plugin_auth. The word
28 * MASTER or BINARY before LOGS is confirmed by the utility family, which owns
29 * master_or_binary; the two are synonyms. Constructs: PurgeLogsTo,
30 * PurgeLogsBefore, BinlogEvent, StartGroupReplication, StopGroupReplication,
31 * CredentialOption. Terminates: the option list is flattened iteratively.
32 * Source: https://dev.mysql.com/doc/refman/8.4/en/purge-binary-logs.html,
33 * https://dev.mysql.com/doc/refman/8.4/en/binlog.html,
34 * https://dev.mysql.com/doc/refman/8.4/en/start-group-replication.html.
35 * Status: Implemented.
36 *
37 * @visibility SqlSemantics\Platform\MySql\Lowering\Replication
38 */
39final class LogRule
40{
41    /**
42     * The group replication option productions, by the option.
43     */
44    private const CREDENTIALS = [
45        'group_replication_user: USER EQ TEXT_STRING_sys_nonewline' => Credential::User,
46        'group_replication_password: PASSWORD EQ TEXT_STRING_sys_nonewline' => Credential::Password,
47        'group_replication_plugin_auth: DEFAULT_AUTH_SYM EQ TEXT_STRING_sys_nonewline' => Credential::DefaultAuth,
48    ];
49
50    /**
51     * The productions that pass a group replication option on to their only child.
52     */
53    private const FORWARD = [
54        'group_replication_start_option: group_replication_user' => true, 'group_replication_start_option: group_replication_password' => true,
55        'group_replication_start_option: group_replication_plugin_auth' => true,
56    ];
57
58    /**
59     * @param Lowering $lowering The lowering this rule belongs to
60     */
61    public function __construct(private readonly Lowering $lowering)
62    {
63    }
64
65    /**
66     * Lowers PURGE.
67     *
68     * @throws ImplementationGap When a production has no rule
69     */
70    public function purge(Form $form): Statement
71    {
72        if ($form->signature !== 'purge: PURGE purge_options') {
73            throw ImplementationGap::production($form);
74        }
75        $options = $this->lowering->form($form->node(1));
76        if ($options->signature === 'purge_options: master_or_binary LOGS_SYM purge_option') {
77            $this->lowering->utility->binaryLogsWord($options->node(0));
78        } elseif ($options->signature !== 'purge_options: BINARY_SYM LOGS_SYM purge_option') {
79            throw ImplementationGap::production($options);
80        }
81        $option = $this->lowering->form($options->node(2));
82
83        return match ($option->signature) {
84            'purge_option: TO_SYM TEXT_STRING_sys' => new PurgeLogsTo($this->lowering->literals->text($option->node(1))),
85            'purge_option: BEFORE_SYM expr' => new PurgeLogsBefore($this->lowering->expressions->expression($option->node(1))),
86            default => throw ImplementationGap::production($option),
87        };
88    }
89
90    /**
91     * Lowers BINLOG.
92     *
93     * @throws ImplementationGap When the production has no rule
94     */
95    public function binlog(Form $form): BinlogEvent
96    {
97        if ($form->signature !== 'binlog_base64_event: BINLOG_SYM TEXT_STRING_sys') {
98            throw ImplementationGap::production($form);
99        }
100
101        return new BinlogEvent($this->lowering->literals->text($form->node(1)));
102    }
103
104    /**
105     * Lowers START or STOP GROUP_REPLICATION.
106     *
107     * @throws ImplementationGap When a production has no rule
108     */
109    public function group(Form $form): Statement
110    {
111        return match ($form->signature) {
112            'group_replication: START_SYM GROUP_REPLICATION' => new StartGroupReplication(),
113            'group_replication: STOP_SYM GROUP_REPLICATION' => new StopGroupReplication(),
114            'group_replication: group_replication_start opt_group_replication_start_options' => $this->start($form),
115            default => throw ImplementationGap::production($form),
116        };
117    }
118
119    /**
120     * Lowers START GROUP_REPLICATION with its options.
121     *
122     * @throws ImplementationGap When a production has no rule
123     */
124    public function start(Form $form): StartGroupReplication
125    {
126        $start = $this->lowering->form($form->node(0));
127        if ($start->signature !== 'group_replication_start: START_SYM GROUP_REPLICATION') {
128            throw ImplementationGap::production($start);
129        }
130        $list = $this->lowering->form($form->node(1));
131        if ($list->signature === 'opt_group_replication_start_options:') {
132            return new StartGroupReplication();
133        }
134        if ($list->signature !== 'opt_group_replication_start_options: group_replication_start_options') {
135            throw ImplementationGap::production($list);
136        }
137        $options = [];
138        $spine = ['group_replication_start_options: group_replication_start_option', 'group_replication_start_options: group_replication_start_options , group_replication_start_option'];
139        foreach ((new Spine($this->lowering))->items($list->node(0), $spine) as $item) {
140            $options[] = $this->option($item);
141        }
142
143        return new StartGroupReplication($options);
144    }
145
146    /**
147     * Lowers one option of START GROUP_REPLICATION.
148     *
149     * @throws ImplementationGap When a production has no rule
150     */
151    public function option(Node $option): CredentialOption
152    {
153        $form = $this->lowering->form($option);
154        if (isset(self::FORWARD[$form->signature])) {
155            $form = $this->lowering->form($form->node(0));
156        }
157        $credential = self::CREDENTIALS[$form->signature] ?? throw ImplementationGap::production($form);
158
159        return new CredentialOption($credential, $this->lowering->literals->text($form->node(2)));
160    }
161}
162