packages/sql-semantics-postgres/src/Statement/Table/Partition/AttachPartition.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Table\Partition;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
9use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\PostgreSql\Rules\Table\Targets;
12use SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\AlterCommand;
13use SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ParentTable;
14
15/**
16 * ATTACH PARTITION: makes a table a partition of the altered partitioned table.
17 *
18 * Mirrors `AT_AttachPartition` with a `PartitionCmd`. The partition is resolved and is the relation fact of
19 * its node; the bound values see no column.
20 * Source: https://www.postgresql.org/docs/17/sql-altertable.html.
21 *
22 * @visibility public
23 * @example Attaching a partition
24 * $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('ALTER TABLE p ATTACH PARTITION p1 FOR VALUES IN (1)');
25 * $statement->toString() // => 'ALTER TABLE p ATTACH PARTITION p1 FOR VALUES IN (1)'
26 */
27final class AttachPartition implements AlterCommand
28{
29 use Snapshot;
30
31 /**
32 * @param ParentTable $partition The table that becomes a partition
33 * @param PartitionBound $bound Its bound
34 */
35 public function __construct(public readonly ParentTable $partition, public readonly PartitionBound $bound)
36 {
37 }
38
39 /**
40 * Resolves the partition and derives the bound.
41 */
42 public function deriveClause(Derivation $derivation, Environment $environment): void
43 {
44 $derivation->target($this->partition, (new Targets())->resolve($derivation, $this->partition->name));
45 $this->bound->deriveClause($derivation, $environment);
46 }
47
48 /**
49 * Writes the action.
50 */
51 public function render(Output $out): void
52 {
53 $out->keyword('ATTACH', 'PARTITION')->node($this->partition)->node($this->bound);
54 }
55}
56