packages/sql-semantics-postgres/tests/Unit/Lowering/Leaf/NamesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Leaf;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Lexer\Token;
11use SqlParser\PostgreSql\PostgreSqlParser;
12use SqlSemantics\Platform\PostgreSql\Lowering\Leaf\Names;
13use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
15use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
16use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\AnalysisException;
17use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
19use SqlSemantics\Platform\PostgreSql\Platform;
20
21#[CoversClass(Names::class)]
22#[Small]
23final class NamesTest extends TestCase
24{
25 public function testNameDecodesAnIdentifierAndRecordsIt(): void
26 {
27 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
28 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 AS "Foo"');
29 $name = $lowering->names->name($tree->find('ColLabel')[0]);
30 self::assertSame('Foo', $name->value);
31 self::assertSame([$name], $lowering->leaves->all());
32 }
33
34 public function testNameReadsAKeywordInEveryNamePosition(): void
35 {
36 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
37 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 between');
38 self::assertSame('between', $lowering->names->name($tree->find('BareColLabel')[0])->value);
39 }
40
41 public function testTokenDecodesAnIdentifierToken(): void
42 {
43 self::assertSame('foo', (new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172))->names->token(new Token(1, 'IDENT', 'FOO', 0))->value);
44 }
45
46 public function testTextReadsTheWordWithoutRecordingAnOperand(): void
47 {
48 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
49 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 AS Bar');
50 self::assertSame('bar', $lowering->names->text($tree->find('ColLabel')[0]));
51 self::assertSame([], $lowering->leaves->all());
52 }
53
54 public function testNamesLowersANameList(): void
55 {
56 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
57 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 FROM t AS x (a, "B", c)');
58 self::assertSame(['a', 'B', 'c'], array_map(static fn (Name $name): string => $name->value, $lowering->names->names($tree->find('name_list')[0])));
59 }
60
61 public function testNamesIsEmptyForAnAbsentColumnList(): void
62 {
63 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
64 $tree = (new PostgreSqlParser('pg-17.2'))->parse('COPY t FROM STDIN');
65 self::assertSame([], $lowering->names->names($tree->find('opt_column_list')[0]));
66 }
67
68 public function testNamesLowersAColumnList(): void
69 {
70 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
71 $tree = (new PostgreSqlParser('pg-17.2'))->parse('COPY t (a, b) FROM STDIN');
72 self::assertCount(2, $lowering->names->names($tree->find('opt_column_list')[0]));
73 }
74
75 public function testAttributesLowersTheNamesAfterTheDots(): void
76 {
77 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
78 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE a.b.select');
79 self::assertSame(['b', 'select'], array_map(static fn (Name $name): string => $name->value, $lowering->names->attributes($tree->find('attrs')[0])));
80 }
81
82 public function testFieldsLowersAnIndirectionOfFieldSelections(): void
83 {
84 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
85 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 FROM a.b.c');
86 self::assertSame(['b', 'c'], array_map(static fn (Name $name): string => $name->value, $lowering->names->fields($tree->find('indirection')[0])));
87 }
88
89 public function testDottedLowersAnyNameAndFuncName(): void
90 {
91 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
92 $tree = (new PostgreSqlParser('pg-17.2'))->parse("SELECT pg_catalog.int4 '1'");
93 self::assertSame(['pg_catalog', 'int4'], array_map(static fn (Name $name): string => $name->value, $lowering->names->dotted($tree->find('func_name')[0])->parts));
94 }
95
96 public function testDottedListLowersEveryName(): void
97 {
98 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
99 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE a, b.c');
100 $names = $lowering->names->dottedList($tree->find('any_name_list')[0]);
101 self::assertSame([1, 2], [count($names[0]->parts), count($names[1]->parts)]);
102 }
103
104 public function testQualifiedLowersCatalogSchemaAndName(): void
105 {
106 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
107 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 FROM c.s.t');
108 $name = $lowering->names->qualified($tree->find('qualified_name')[0]);
109 self::assertSame(['c', 's', 't'], [$name->catalog?->value, $name->schema?->value, $name->name->value]);
110 }
111
112 public function testQualifiedRejectsMoreThanThreeParts(): void
113 {
114 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
115 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 FROM a.b.c.d');
116 $this->expectException(AnalysisException::class);
117 $lowering->names->qualified($tree->find('qualified_name')[0]);
118 }
119
120 public function testQualifiedListLowersEveryName(): void
121 {
122 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
123 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE TABLE t () INHERITS (a, s.b)');
124 self::assertCount(2, $lowering->names->qualifiedList($tree->find('qualified_name_list')[0]));
125 }
126
127 public function testOptionalLowersANameOrNothing(): void
128 {
129 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
130 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE INDEX i ON t (a)');
131 self::assertSame('i', $lowering->names->optional($tree->find('opt_single_name')[0])?->value);
132 self::assertNull($lowering->names->optional((new PostgreSqlParser('pg-17.2'))->parse('CREATE INDEX ON t (a)')->find('opt_single_name')[0]));
133 }
134
135 public function testOptionalDottedLowersACollationOrNothing(): void
136 {
137 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
138 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE INDEX ON t (a COLLATE "C", b)');
139 self::assertSame('C', $lowering->names->optionalDotted($tree->find('opt_collate')[0])?->last()->value);
140 self::assertNull($lowering->names->optionalDotted($tree->find('opt_collate')[1]));
141 }
142}
143