packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/TextRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\SyntaxRule;
12use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\TextRule;
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\Lowering\Leaves;
17use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
18use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21
22#[CoversClass(TextRule::class)]
23#[Small]
24final class TextRuleTest extends TestCase
25{
26 public function testExpressionLowersTheKeywordCalls(): void
27 {
28 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
29 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT OVERLAY(1, 2), SUBSTRING(1)');
30 $node = array_map(static fn ($target) => (new SyntaxRule($lowering))->subexpression($target->find('func_expr_common_subexpr')[0]), $tree->find('target_list')[0]->find('target_el'));
31 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
32 $out->list($node);
33 self::assertSame('OVERLAY(1, 2), SUBSTRING(1)', (new Lexical())->join($out->pieces()));
34 }
35
36 public function testExtractLowersTheThreeFieldSpellings(): void
37 {
38 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
39 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT EXTRACT("YEAR" FROM 1), EXTRACT(month FROM 1), EXTRACT(\'dow\' FROM 1)');
40 $node = array_map(static fn ($list) => (new TextRule($lowering))->extract($list), $tree->find('target_list')[0]->find('extract_list'));
41 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
42 $out->list($node);
43 self::assertSame('EXTRACT("YEAR" FROM 1), EXTRACT(MONTH FROM 1), EXTRACT(\'dow\' FROM 1)', (new Lexical())->join($out->pieces()));
44 }
45
46 public function testOverlayLowersTheCount(): 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 OVERLAY(\'a\' PLACING \'b\' FROM 1 FOR 2)');
50 $node = (new TextRule($lowering))->overlay($tree->find('overlay_list')[0]);
51 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52 $out->node($node);
53 self::assertSame('OVERLAY(\'a\' PLACING \'b\' FROM 1 FOR 2)', (new Lexical())->join($out->pieces()));
54 }
55
56 public function testPositionLowersTheOperands(): void
57 {
58 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
59 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT POSITION(\'b\' IN \'abc\')');
60 $node = (new TextRule($lowering))->position($tree->find('position_list')[0]);
61 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
62 $out->node($node);
63 self::assertSame('POSITION(\'b\' IN \'abc\')', (new Lexical())->join($out->pieces()));
64 }
65
66 public function testSubstringLowersEveryForm(): void
67 {
68 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
69 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT SUBSTRING(\'a\' FROM 1 FOR 2), SUBSTRING(\'a\' FOR 2 FROM 1), SUBSTRING(\'a\' FROM 1), SUBSTRING(\'a\' FOR 2), SUBSTRING(\'a\' SIMILAR \'b\' ESCAPE \'c\')');
70 $node = array_map(static fn ($list) => (new TextRule($lowering))->substring($list), $tree->find('target_list')[0]->find('substr_list'));
71 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
72 $out->list($node);
73 self::assertSame('SUBSTRING(\'a\' FROM 1 FOR 2), SUBSTRING(\'a\' FOR 2 FROM 1), SUBSTRING(\'a\' FROM 1), SUBSTRING(\'a\' FOR 2), SUBSTRING(\'a\' SIMILAR \'b\' ESCAPE \'c\')', (new Lexical())->join($out->pieces()));
74 }
75
76 public function testTrimLowersTheListForms(): void
77 {
78 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
79 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT TRIM(BOTH \'x\' FROM \'a\'), TRIM(LEADING FROM \'a\'), TRIM(TRAILING \'a\', \'b\')');
80 $node = array_map(static fn ($target) => (new SyntaxRule($lowering))->subexpression($target->find('func_expr_common_subexpr')[0]), $tree->find('target_list')[0]->find('target_el'));
81 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
82 $out->list($node);
83 self::assertSame('TRIM(\'x\' FROM \'a\'), TRIM(LEADING \'a\'), TRIM(TRAILING \'a\', \'b\')', (new Lexical())->join($out->pieces()));
84 }
85}
86