packages/sql-semantics-mysql/src/Statement/Replication/Replica/StopReplica.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Replication\Replica;
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 * `STOP REPLICA [threads] [FOR CHANNEL 'name']`, or STOP SLAVE.
20 *
21 * Mirrors SQLCOM_REPLICA_STOP: the thread flags in written order (none means
22 * both threads). The spelling SLAVE or REPLICA is kept
23 * (MYSQL-REPLICATION-RELEASE-001). A channel needs MySQL 5.7 or later.
24 * Rule: MYSQL-STOP-REPLICA-001. Facts: a line feed in the channel name is a
25 * RefusedSetting diagnostic.
26 * Source: https://dev.mysql.com/doc/refman/8.4/en/stop-replica.html.
27 * Status: Implemented.
28 *
29 * @visibility public
30 * @example Stopping the applier thread of a channel
31 *     (new \SqlSemantics\Platform\MySql\Semantics('mysql-5.7.44'))->analyze("stop slave sql_thread for channel 'c'")->toString() // => "STOP SLAVE SQL_THREAD FOR CHANNEL 'c'"
32 */
33final class StopReplica implements Statement
34{
35    use Snapshot;
36
37    /**
38     * @var list<ReplicaThread> The threads in written order; empty for both
39     */
40    public readonly array $threads;
41
42    /**
43     * @param Terminology $terminology The spelling: STOP SLAVE or STOP REPLICA
44     * @param list<ReplicaThread> $threads The threads in written order; empty for both
45     * @param Text|null $channel The replication channel, when FOR CHANNEL is written
46     */
47    public function __construct(public readonly Terminology $terminology, array $threads = [], public readonly ?Text $channel = null)
48    {
49        $this->threads = Check::listOf($threads, ReplicaThread::class, 'STOP REPLICA names a list of threads.');
50    }
51
52    /**
53     * Checks the release and reports a refused channel name.
54     */
55    public function deriveStatement(Derivation $derivation): void
56    {
57        $release = $derivation->context->profile->grammar;
58        Check::input((new Releases())->replica($release, $this->terminology), 'STOP ' . ($this->terminology === Terminology::Legacy ? 'SLAVE' : 'REPLICA') . ' is not a statement of this release.');
59        Check::input($this->channel === null || $release !== GrammarRelease::MySql5651, 'A replication channel needs MySQL 5.7 or later.');
60        (new SourceSettings())->lineFeed($derivation, $this->channel);
61    }
62
63    /**
64     * Writes the request.
65     */
66    public function render(Output $out): void
67    {
68        $out->keyword('STOP', $this->terminology === Terminology::Legacy ? 'SLAVE' : 'REPLICA');
69        foreach ($this->threads as $position => $thread) {
70            if ($position > 0) {
71                $out->symbol(',');
72            }
73            $out->keyword($thread->value);
74        }
75        if ($this->channel !== null) {
76            $out->keyword('FOR', 'CHANNEL')->node($this->channel);
77        }
78    }
79}
80