packages/sql-semantics-postgres/src/Statement/Table/Index/AlterStatistics.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Table\Index;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
9use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
11use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
12use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
13
14/**
15 * A request to change the statistics target of extended statistics.
16 *
17 * Mirrors PostgreSQL's `AlterStatsStmt` (`defnames`, `stxstattarget`, `missing_ok`). PostgreSQL 17 accepts
18 * DEFAULT, which is the target -1 of earlier releases.
19 * Source: https://www.postgresql.org/docs/17/sql-alterstatistics.html.
20 *
21 * @visibility public
22 * @example Changing a statistics target
23 * $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('ALTER STATISTICS IF EXISTS s SET STATISTICS 100');
24 * $statement->toString() // => 'ALTER STATISTICS IF EXISTS s SET STATISTICS 100'
25 */
26final class AlterStatistics implements Statement
27{
28 use Snapshot;
29
30 /**
31 * @param DottedName $name The statistics object
32 * @param SignedNumber|null $target The target; null for DEFAULT
33 * @param bool $ifExists Whether IF EXISTS is written
34 */
35 public function __construct(
36 public readonly DottedName $name,
37 public readonly ?SignedNumber $target,
38 public readonly bool $ifExists = false,
39 ) {
40 }
41
42 /**
43 * Derives nothing: the statement names a catalog object.
44 */
45 public function deriveStatement(Derivation $derivation): void
46 {
47 }
48
49 /**
50 * Writes the statement.
51 */
52 public function render(Output $out): void
53 {
54 $out->keyword('ALTER', 'STATISTICS');
55 if ($this->ifExists) {
56 $out->keyword('IF', 'EXISTS');
57 }
58 $out->node($this->name)->keyword('SET', 'STATISTICS');
59 if ($this->target === null) {
60 $out->keyword('DEFAULT');
61 } else {
62 $out->node($this->target);
63 }
64 }
65}
66