packages/sql-semantics-postgres/src/Statement/Utility/Maintenance/Analyze.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
12use SqlSemantics\Platform\PostgreSql\Rules\Utility\MaintenanceFacts;
13use SqlSemantics\Platform\PostgreSql\Rules\Utility\OptionRules;
14
15/**
16 * `ANALYZE`: a request to collect statistics about tables.
17 *
18 * Rule: PG-ANALYZE-001. Mirrors PostgreSQL's `VacuumStmt` without
19 * `is_vacuumcmd`: the options in the order written and the tables with
20 * their columns, where no table stands for every table of the database.
21 * ANALYSE is the British spelling of the same command and is kept. The older
22 * syntax writes VERBOSE as a word. Facts: each table is resolved, and a
23 * listed column is looked up in a declared table. Diagnostics: an option
24 * ANALYZE does not know; a column the declared table does not have.
25 * Source: https://www.postgresql.org/docs/17/sql-analyze.html. Status: Implemented.
26 *
27 * @visibility public
28 * @example Reading an ANALYZE in the British spelling
29 * $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('ANALYSE VERBOSE t');
30 * [$operation->statement->keyword, $operation->toString()] // => [\SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\OptionKeyword::Analyse, 'ANALYSE VERBOSE t']
31 * @example Refusing a command keyword that is not a spelling of ANALYZE
32 * new \SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\Analyze(\SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\OptionKeyword::Format) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction
33 */
34final class Analyze implements Statement
35{
36 use Snapshot;
37
38 /**
39 * @var list<UtilityOption> The options in the order written
40 */
41 public readonly array $options;
42
43 /**
44 * @var list<MaintenanceTarget> The tables; none stands for every table
45 */
46 public readonly array $targets;
47
48 /**
49 * @param OptionKeyword $keyword The spelling of the command: ANALYZE or ANALYSE
50 * @param list<UtilityOption> $options The options in the order written
51 * @param OptionSyntax $syntax How the options are written
52 * @param list<MaintenanceTarget> $targets The tables; none stands for every table
53 */
54 public function __construct(public readonly OptionKeyword $keyword = OptionKeyword::Analyze, array $options = [], public readonly OptionSyntax $syntax = OptionSyntax::Words, array $targets = [])
55 {
56 Check::input($keyword !== OptionKeyword::Format, 'The command is spelled ANALYZE or ANALYSE.');
57 $this->options = Check::listOf($options, UtilityOption::class, 'The options of ANALYZE are utility options.');
58 $this->targets = Check::listOf($targets, MaintenanceTarget::class, 'The tables of ANALYZE are maintenance targets.');
59 Check::input((new OptionRules())->writable($this->options, $syntax, ['verbose']), 'The options cannot be written in that syntax.');
60 }
61
62 /**
63 * Resolves the tables and columns and reports what ANALYZE rejects.
64 */
65 public function deriveStatement(Derivation $derivation): void
66 {
67 (new OptionRules())->derive($derivation, 'ANALYZE', $this->options);
68 (new MaintenanceFacts())->targets($derivation, $this->targets);
69 }
70
71 /**
72 * Writes the request.
73 */
74 public function render(Output $out): void
75 {
76 $out->keyword($this->keyword->value);
77 (new OptionRules())->write($out, $this->options, $this->syntax);
78 $out->list($this->targets);
79 }
80}
81