packages/sql-semantics-mysql/tests/Unit/Lowering/Call/FunctionRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Call\FunctionRule;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Call\CharCall;
16use SqlSemantics\Platform\MySql\Statement\Call\FunctionCall;
17use SqlSemantics\Platform\MySql\Statement\Call\KeywordCall;
18use SqlSemantics\Platform\MySql\Statement\Call\KeywordFunction;
19use SqlSemantics\Platform\MySql\Statement\Call\Position;
20use SqlSemantics\Platform\MySql\Statement\Call\Trim;
21use SqlSemantics\Platform\MySql\Statement\Call\TrimSide;
22use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
23
24#[CoversClass(FunctionRule::class)]
25#[Small]
26final class FunctionRuleTest extends TestCase
27{
28 public function testClaimsTellsTheProductionsOfTheRule(): void
29 {
30 $platform = new Platform();
31 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
32 $rule = new FunctionRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
33
34 self::assertTrue($rule->claims('function_call_keyword: TRIM ( expr )'));
35 self::assertTrue($rule->claims('function_call_generic: IDENT_sys ( opt_udf_expr_list )'));
36 self::assertFalse($rule->claims('function_call_nonkeyword: now'));
37 }
38
39 public function testCallLowersKeywordAndGenericCalls(): void
40 {
41 $platform = new Platform();
42 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
43 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
44 $list = $platform->parser($profile)->parse("SELECT LEFT(a, 2), TRIM(BOTH FROM a), CHAR(65 USING ascii), POSITION('b' IN a), f(a AS x, 1), db.g()")->find('select_item_list')[0];
45 $rule = new FunctionRule($lowering);
46 $left = $rule->call($lowering->form($list->find('function_call_keyword')[0]));
47 $trim = $rule->call($lowering->form($list->find('function_call_keyword')[1]));
48 $char = $rule->call($lowering->form($list->find('function_call_keyword')[2]));
49 $position = $rule->call($lowering->form($list->find('function_call_nonkeyword')[0]));
50 $generic = $rule->call($lowering->form($list->find('function_call_generic')[0]));
51 $qualified = $rule->call($lowering->form($list->find('function_call_generic')[1]));
52
53 self::assertInstanceOf(KeywordCall::class, $left);
54 self::assertSame(KeywordFunction::Left, $left->function);
55 self::assertInstanceOf(Trim::class, $trim);
56 self::assertSame(TrimSide::Both, $trim->side);
57 self::assertInstanceOf(CharCall::class, $char);
58 self::assertSame('ascii', $char->charset?->name?->value);
59 self::assertInstanceOf(Position::class, $position);
60 self::assertInstanceOf(FunctionCall::class, $generic);
61 self::assertSame('x', $generic->arguments[0]->alias?->value);
62 self::assertInstanceOf(FunctionCall::class, $qualified);
63 self::assertSame('db', $qualified->schema?->value);
64 }
65
66 public function testCallReportsAProductionOutsideTheRule(): void
67 {
68 $platform = new Platform();
69 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
70 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
71 $node = $platform->parser($profile)->parse('SELECT NOW()')->find('function_call_nonkeyword')[0];
72 $this->expectExceptionMessage('No semantic rule is implemented for: ');
73
74 (new FunctionRule($lowering))->call($lowering->form($node));
75 }
76
77 public function testKeywordLowersTheArgumentsAtTheirPositions(): void
78 {
79 $platform = new Platform();
80 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
81 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
82 $node = $platform->parser($profile)->parse('SELECT INTERVAL(1, 2, 3, 4)')->find('function_call_keyword')[0];
83 $call = (new FunctionRule($lowering))->keyword($lowering->form($node), KeywordFunction::Interval, [2, 4], 6);
84
85 self::assertCount(4, $call->arguments);
86 }
87
88 public function testNiladicAcceptsOptionalParentheses(): void
89 {
90 $platform = new Platform();
91 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
92 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
93 $node = $platform->parser($profile)->parse('SELECT CURRENT_USER')->find('function_call_keyword')[0];
94 self::assertSame([], (new FunctionRule($lowering))->niladic($lowering->form($node), KeywordFunction::CurrentUser)->arguments);
95 }
96
97 public function testExpressionLowersTheExpressionAtAPosition(): void
98 {
99 $platform = new Platform();
100 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
101 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
102 $node = $platform->parser($profile)->parse('SELECT LEFT(1, 2)')->find('function_call_keyword')[0];
103 self::assertInstanceOf(NumberLiteral::class, (new FunctionRule($lowering))->expression($lowering->form($node), 2));
104 }
105
106 public function testArgumentsLowersAnEmptyList(): void
107 {
108 $platform = new Platform();
109 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
110 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
111 $node = $platform->parser($profile)->parse('SELECT f()')->find('opt_udf_expr_list')[0];
112 self::assertSame([], (new FunctionRule($lowering))->arguments($node));
113 }
114
115 public function testArgumentLowersTheAliasOfMySql57(): void
116 {
117 $platform = new Platform();
118 $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
119 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
120 $node = $platform->parser($profile)->parse('SELECT f(a b)')->find('udf_expr')[0];
121 self::assertSame('b', (new FunctionRule($lowering))->argument($node)->alias?->value);
122 }
123}
124