packages/sql-semantics-postgres/src/Lowering/Leaf/Flags.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Lowering\Leaf;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
9use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\PostgreSql\Statement\Option\AddOrDrop;
11use SqlSemantics\Platform\PostgreSql\Statement\Option\DropBehavior;
12use SqlSemantics\Platform\PostgreSql\Statement\Option\NormalForm;
13use SqlSemantics\Platform\PostgreSql\Statement\Option\XmlOption;
14
15/**
16 * Lowers the optional keywords several families share.
17 *
18 * Rule: PG-FLAG-001. Scope: `opt_or_replace`, `opt_concurrently`,
19 * `opt_with`, `opt_as`, `opt_table`, `opt_column`, `opt_default`,
20 * `opt_nowait`, `opt_procedural`, `opt_set_data`, `opt_drop_behavior`,
21 * `add_drop`, `document_or_content`, `unicode_normal_form`. A flag is whether
22 * its keywords are written; a choice is an enum case. The noise words among
23 * them (`opt_with`, `opt_as`, `opt_table`, `opt_column`, `opt_procedural`,
24 * `opt_set_data`) are listed with their citations in the leaf noise table,
25 * and a command does not keep them. Source: the synopsis of each command in
26 * https://www.postgresql.org/docs/17/sql-commands.html. Status: Implemented.
27 *
28 * @visibility SqlSemantics
29 */
30final class Flags
31{
32 /**
33 * Whether each flag production writes its keywords.
34 */
35 private const PRESENT = [
36 'opt_or_replace: OR REPLACE' => true, 'opt_or_replace:' => false,
37 'opt_concurrently: CONCURRENTLY' => true, 'opt_concurrently:' => false,
38 'opt_with: WITH' => true, 'opt_with: WITH_LA' => true, 'opt_with:' => false,
39 'opt_as: AS' => true, 'opt_as:' => false,
40 'opt_table: TABLE' => true, 'opt_table:' => false,
41 'opt_column: COLUMN' => true, 'opt_column:' => false,
42 'opt_default: DEFAULT' => true, 'opt_default:' => false,
43 'opt_nowait: NOWAIT' => true, 'opt_nowait:' => false,
44 'opt_procedural: PROCEDURAL' => true, 'opt_procedural:' => false,
45 'opt_set_data: SET DATA_P' => true, 'opt_set_data:' => false,
46 ];
47
48 /**
49 * @param Lowering $lowering The hub
50 */
51 public function __construct(private readonly Lowering $lowering)
52 {
53 }
54
55 /**
56 * Tells whether an optional keyword is written: `opt_or_replace`, `opt_concurrently`, `opt_with`, `opt_as`, `opt_table`, `opt_column`, `opt_default`, `opt_nowait`, `opt_procedural` or `opt_set_data`.
57 *
58 * @throws ImplementationGap When the production has no rule
59 */
60 public function present(Node $flag): bool
61 {
62 $form = $this->lowering->productions->form($flag);
63
64 return self::PRESENT[$form->signature] ?? throw ImplementationGap::production($form);
65 }
66
67 /**
68 * Lowers `opt_drop_behavior`; no keyword is null.
69 *
70 * @throws ImplementationGap When the production has no rule
71 */
72 public function dropBehavior(Node $behavior): ?DropBehavior
73 {
74 $form = $this->lowering->productions->form($behavior);
75
76 return match ($form->signature) {
77 'opt_drop_behavior: CASCADE' => DropBehavior::Cascade,
78 'opt_drop_behavior: RESTRICT' => DropBehavior::Restrict,
79 'opt_drop_behavior:' => null,
80 default => throw ImplementationGap::production($form),
81 };
82 }
83
84 /**
85 * Lowers `add_drop`.
86 *
87 * @throws ImplementationGap When the production has no rule
88 */
89 public function addOrDrop(Node $choice): AddOrDrop
90 {
91 $form = $this->lowering->productions->form($choice);
92
93 return match ($form->signature) {
94 'add_drop: ADD_P' => AddOrDrop::Add,
95 'add_drop: DROP' => AddOrDrop::Drop,
96 default => throw ImplementationGap::production($form),
97 };
98 }
99
100 /**
101 * Lowers `document_or_content`.
102 *
103 * @throws ImplementationGap When the production has no rule
104 */
105 public function xmlOption(Node $option): XmlOption
106 {
107 $form = $this->lowering->productions->form($option);
108
109 return match ($form->signature) {
110 'document_or_content: DOCUMENT_P' => XmlOption::Document,
111 'document_or_content: CONTENT_P' => XmlOption::Content,
112 default => throw ImplementationGap::production($form),
113 };
114 }
115
116 /**
117 * Lowers `unicode_normal_form`.
118 *
119 * @throws ImplementationGap When the production has no rule
120 */
121 public function normalForm(Node $form): NormalForm
122 {
123 $production = $this->lowering->productions->form($form);
124
125 return match ($production->signature) {
126 'unicode_normal_form: NFC' => NormalForm::Nfc,
127 'unicode_normal_form: NFD' => NormalForm::Nfd,
128 'unicode_normal_form: NFKC' => NormalForm::Nfkc,
129 'unicode_normal_form: NFKD' => NormalForm::Nfkd,
130 default => throw ImplementationGap::production($production),
131 };
132 }
133}
134