packages/sql-semantics-mysql/src/Statement/Replication/Source/ChangeReplicationSource.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Replication\Source;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\MySql\Model\Rendering\Output;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Model\Statement\Statement;
13use SqlSemantics\Platform\MySql\Rules\Replication\Releases;
14use SqlSemantics\Platform\MySql\Rules\Replication\SourceSettings;
15use SqlSemantics\Platform\MySql\Statement\Literal\Text;
16use SqlSemantics\Platform\MySql\Statement\Replication\Terminology;
17
18/**
19 * `CHANGE REPLICATION SOURCE TO option = value, … [FOR CHANNEL 'name']`, written `CHANGE MASTER TO` in the legacy vocabulary.
20 *
21 * Mirrors SQLCOM_CHANGE_REPLICATION_SOURCE with its LEX_SOURCE_INFO: the
22 * options in written order, the same option possibly written more than once
23 * (the server keeps the last value). The vocabulary is the one of the
24 * release (MYSQL-REPLICATION-RELEASE-001); in 8.0 to 8.3 CHANGE MASTER and
25 * the MASTER_ options are read as their current synonyms. A channel needs
26 * MySQL 5.7 or later. Rule: MYSQL-CHANGE-SOURCE-001. Facts: the option checks
27 * of MYSQL-REPLICATION-SOURCE-001 and a line feed in the channel name. The
28 * statement uses no table and provides no declaration.
29 * Source: https://dev.mysql.com/doc/refman/8.4/en/change-replication-source-to.html,
30 * https://dev.mysql.com/doc/refman/5.7/en/change-master-to.html.
31 * Status: Implemented.
32 *
33 * @visibility public
34 * @example Reading the options of the legacy spelling as their current synonyms
35 *     $change = (new \SqlSemantics\Platform\MySql\Semantics('mysql-8.0.44'))->analyze("CHANGE MASTER TO MASTER_HOST = 'h', MASTER_PORT = 3307 FOR CHANNEL 'c'");
36 *     [$change->toString(), $change->statement->options[1]->kind] // => ["CHANGE REPLICATION SOURCE TO SOURCE_HOST = 'h', SOURCE_PORT = 3307 FOR CHANNEL 'c'", \SqlSemantics\Platform\MySql\Statement\Replication\Source\SourceOptionKind::Port]
37 */
38final class ChangeReplicationSource implements Statement
39{
40    use Snapshot;
41
42    /**
43     * @var list<SourceOption> The options in written order; at least one
44     */
45    public readonly array $options;
46
47    /**
48     * @param Terminology $terminology The vocabulary the statement is written in
49     * @param list<SourceOption> $options The options in written order; at least one, each in the vocabulary of the statement
50     * @param Text|null $channel The replication channel, when FOR CHANNEL is written
51     */
52    public function __construct(public readonly Terminology $terminology, array $options, public readonly ?Text $channel = null)
53    {
54        $this->options = Check::listOf($options, SourceOption::class, 'CHANGE REPLICATION SOURCE sets a list of options.', 1);
55        foreach ($this->options as $option) {
56            Check::input($option->terminology === $terminology, 'An option is written in the vocabulary of its statement.');
57        }
58    }
59
60    /**
61     * Checks the vocabulary of the release and derives the options and the channel.
62     */
63    public function deriveStatement(Derivation $derivation): void
64    {
65        $releases = new Releases();
66        $release = $derivation->context->profile->grammar;
67        Check::input($releases->source($release) === $this->terminology, 'CHANGE REPLICATION SOURCE is written in the vocabulary of the release.');
68        Check::input($this->channel === null || $release !== GrammarRelease::MySql5651, 'A replication channel needs MySQL 5.7 or later.');
69        $settings = new SourceSettings();
70        $settings->options($derivation, $this->options);
71        $settings->lineFeed($derivation, $this->channel);
72    }
73
74    /**
75     * Writes the request.
76     */
77    public function render(Output $out): void
78    {
79        $out->keyword('CHANGE');
80        if ($this->terminology === Terminology::Legacy) {
81            $out->keyword('MASTER');
82        } else {
83            $out->keyword('REPLICATION', 'SOURCE');
84        }
85        $out->keyword('TO')->list($this->options);
86        if ($this->channel !== null) {
87            $out->keyword('FOR', 'CHANNEL')->node($this->channel);
88        }
89    }
90}
91