packages/sql-semantics-postgres/src/Statement/Catalog/Publication/Subscription/AlterSubscriptionOptions.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\Subscription;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Contract\NameUse;
9use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
14use SqlSemantics\Platform\PostgreSql\Rules\Catalog\ClauseFacts;
15use SqlSemantics\Platform\PostgreSql\Statement\Option\Definition;
16
17/**
18 * A request to set parameters of a subscription, or to skip a remote transaction: `ALTER SUBSCRIPTION name { SET | SKIP } ( ... )`.
19 *
20 * Rule: PG-SUBSCRIPTION-002. Mirrors `AlterSubscriptionStmt` of kind
21 * ALTER_SUBSCRIPTION_OPTIONS or ALTER_SUBSCRIPTION_SKIP.
22 * Source: https://www.postgresql.org/docs/17/sql-altersubscription.html. Status: Implemented.
23 *
24 * @visibility public
25 * @example Skipping a transaction
26 * $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("ALTER SUBSCRIPTION s SKIP (lsn = '0/14C0378')");
27 * $operation->statement->action // => \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\Subscription\SubscriptionOptionAction::Skip
28 */
29final class AlterSubscriptionOptions implements Statement
30{
31 use Snapshot;
32
33 /**
34 * @var non-empty-list<Definition> The parameters
35 */
36 public readonly array $options;
37
38 /**
39 * @param Name $name The subscription name
40 * @param SubscriptionOptionAction $action Whether parameters are set or a transaction is skipped
41 * @param list<Definition> $options The parameters; at least one
42 */
43 public function __construct(public readonly Name $name, public readonly SubscriptionOptionAction $action, array $options)
44 {
45 $this->options = Check::listOf($options, Definition::class, 'ALTER SUBSCRIPTION names at least one parameter.', 1);
46 }
47
48 /**
49 * Derives the parameter values.
50 */
51 public function deriveStatement(Derivation $derivation): void
52 {
53 (new ClauseFacts())->derive($derivation, $this->options);
54 }
55
56 /**
57 * Writes the request.
58 */
59 public function render(Output $out): void
60 {
61 $out->keyword('ALTER', 'SUBSCRIPTION')->name($this->name, NameUse::Column)->keyword($this->action->value)->symbol('(')->list($this->options)->symbol(')');
62 }
63}
64