packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/RoutinesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
12use SqlSemantics\Platform\PostgreSql\Lowering\Routine\Routines;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
18use SqlSemantics\Platform\PostgreSql\Statement\Object\ChangeOwner;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Comment;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\CopyCollation;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Drop;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\ExtensionDependency;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Rename;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\SecurityLabel;
25use SqlSemantics\Platform\PostgreSql\Statement\Object\SetSchema;
26use SqlSemantics\Platform\PostgreSql\Statement\Operator\AlterOperator;
27use SqlSemantics\Platform\PostgreSql\Statement\Routine\AlterRoutine;
28use SqlSemantics\Platform\PostgreSql\Statement\Routine\CreateFunction;
29
30#[CoversClass(Routines::class)]
31#[Small]
32final class RoutinesTest extends TestCase
33{
34 public function testStatementDispatchesEveryStatementOfTheFamily(): 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('COMMENT ON TABLE t IS NULL; ALTER FUNCTION f() STABLE; SECURITY LABEL ON SCHEMA s IS NULL; ALTER SCHEMA s OWNER TO r; ALTER TYPE t SET SCHEMA s; ALTER INDEX i DEPENDS ON EXTENSION e; CREATE FUNCTION f() RETURNS int4 RETURN 1; DROP AGGREGATE a(*); ALTER SCHEMA s RENAME TO t; CREATE COLLATION c FROM d; ALTER OPERATOR = (int4, int4) SET (hashes)');
38 $routines = $lowering->routines;
39 self::assertSame(
40 [Comment::class, AlterRoutine::class, SecurityLabel::class, ChangeOwner::class, SetSchema::class, ExtensionDependency::class, CreateFunction::class, Drop::class, Rename::class, CopyCollation::class, AlterOperator::class],
41 [
42 $routines->statement($tree->find('CommentStmt')[0])::class,
43 $routines->statement($tree->find('AlterFunctionStmt')[0])::class,
44 $routines->statement($tree->find('SecLabelStmt')[0])::class,
45 $routines->statement($tree->find('AlterOwnerStmt')[0])::class,
46 $routines->statement($tree->find('AlterObjectSchemaStmt')[0])::class,
47 $routines->statement($tree->find('AlterObjectDependsStmt')[0])::class,
48 $routines->statement($tree->find('CreateFunctionStmt')[0])::class,
49 $routines->statement($tree->find('RemoveAggrStmt')[0])::class,
50 $routines->statement($tree->find('RenameStmt')[0])::class,
51 $routines->statement($tree->find('DefineStmt')[0])::class,
52 $routines->statement($tree->find('AlterOperatorStmt')[0])::class,
53 ],
54 );
55 }
56
57 public function testStatementRejectsAStatementOfAnotherFamily(): void
58 {
59 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
60 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CHECKPOINT');
61 $this->expectExceptionMessage('No semantic rule is implemented for: CheckPointStmt: CHECKPOINT');
62 $lowering->routines->statement($tree->find('CheckPointStmt')[0]);
63 }
64
65 public function testFunctionSignatureLowersAName(): void
66 {
67 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
68 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FUNCTION f(int), g');
69 self::assertNull($lowering->routines->functionSignature($tree->find('function_with_argtypes')[1])->arguments);
70 }
71
72 public function testFunctionSignaturesLowersTheList(): void
73 {
74 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
75 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FUNCTION f(int), g');
76 self::assertCount(2, $lowering->routines->functionSignatures($tree->find('function_with_argtypes_list')[0]));
77 }
78
79 public function testAggregateSignatureLowersTheArguments(): void
80 {
81 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
82 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP AGGREGATE a(int), b(*)');
83 self::assertCount(1, $lowering->routines->aggregateSignature($tree->find('aggregate_with_argtypes')[0])->arguments->direct);
84 }
85
86 public function testAggregateSignaturesLowersTheList(): void
87 {
88 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
89 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP AGGREGATE a(int), b(*)');
90 self::assertCount(2, $lowering->routines->aggregateSignatures($tree->find('aggregate_with_argtypes_list')[0]));
91 }
92
93 public function testOperatorSignatureLowersTheOperator(): void
94 {
95 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
96 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP OPERATOR s.+ (int, int)');
97 self::assertSame(['s', '+'], [$lowering->routines->operatorSignature($tree->find('operator_with_argtypes')[0])->operator->qualifiers[0]->value, $lowering->routines->operatorSignature($tree->find('operator_with_argtypes')[0])->operator->name->value]);
98 }
99
100 public function testOperatorSignaturesLowersTheList(): void
101 {
102 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
103 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP OPERATOR + (int, int), - (NONE, int)');
104 self::assertCount(2, $lowering->routines->operatorSignatures($tree->find('operator_with_argtypes_list')[0]));
105 }
106
107 public function testObjectKindLowersTheKind(): void
108 {
109 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
110 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FOREIGN DATA WRAPPER w');
111 self::assertSame(ObjectKind::ForeignDataWrapper, $lowering->routines->objectKind($tree->find('drop_type_name')[0]));
112 }
113
114 public function testAttributeChangesReadsTheAttributesOfTheCommand(): void
115 {
116 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
117 $tree = (new PostgreSqlParser('pg-17.2'))->parse('ALTER TYPE t SET (storage = plain, analyse = f)');
118 $changes = $lowering->routines->attributeChanges($tree->find('operator_def_list')[0], \SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\TypeChangeAttribute::class);
119 self::assertSame([\SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\TypeChangeAttribute::Storage, null], [$changes[0]->attribute->known, $changes[1]->attribute->known]);
120 }
121}
122