packages/sql-semantics-mysql/tests/Unit/Lowering/Expression/ConstructRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Expression\ConstructRule;
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\Expression\Access\FullTextMode;
16use SqlSemantics\Platform\MySql\Statement\Expression\Access\FullTextSearch;
17use SqlSemantics\Platform\MySql\Statement\Expression\Branching\CaseExpression;
18use SqlSemantics\Platform\MySql\Statement\Expression\Conversion\Cast;
19use SqlSemantics\Platform\MySql\Statement\Expression\Conversion\CastAtLocal;
20use SqlSemantics\Platform\MySql\Statement\Expression\Conversion\CharsetConversion;
21use SqlSemantics\Platform\MySql\Statement\Expression\Operator\BinaryCast;
22use SqlSemantics\Platform\MySql\Statement\Expression\OptionalWords;
23use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
24
25#[CoversClass(ConstructRule::class)]
26#[Medium]
27final class ConstructRuleTest extends TestCase
28{
29 public function testLowerLowersTheKeywordConstructs(): void
30 {
31 $platform = new Platform();
32 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
33 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
34 $rule = new ConstructRule($lowering);
35 $productions = $platform->productions($profile);
36 $parser = $platform->parser($profile);
37 $convert = $rule->lower($productions->form($parser->parse('SELECT CONVERT(1, SIGNED)')->find('simple_expr')[0]));
38
39 self::assertInstanceOf(Cast::class, $convert);
40 self::assertFalse($convert->array);
41 self::assertInstanceOf(CastAtLocal::class, $rule->lower($productions->form($parser->parse('SELECT CAST(1 AT LOCAL AS DATE)')->find('simple_expr')[0])));
42 self::assertInstanceOf(CharsetConversion::class, $rule->lower($productions->form($parser->parse('SELECT CONVERT(1 USING latin1)')->find('simple_expr')[0])));
43 self::assertInstanceOf(BinaryCast::class, $rule->lower($productions->form($parser->parse('SELECT BINARY 1')->find('simple_expr')[0])));
44 self::assertInstanceOf(FullTextSearch::class, $rule->lower($productions->form($parser->parse("SELECT MATCH (a) AGAINST ('x')")->find('simple_expr')[0])));
45 self::assertInstanceOf(CaseExpression::class, $rule->lower($productions->form($parser->parse('SELECT CASE WHEN 1 THEN 2 END')->find('simple_expr')[0])));
46 }
47
48 public function testZoneLowersTheTimeZoneAndThePrecision(): void
49 {
50 $platform = new Platform();
51 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
52 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
53 $zone = (new ConstructRule($lowering))->zone($platform->productions($profile)->form($platform->parser($profile)->parse("SELECT CAST(1 AT TIME ZONE INTERVAL '+00:00' AS DATETIME(2))")->find('simple_expr')[0]));
54
55 self::assertSame(['+00:00', true, '2'], [$zone->zone->value, $zone->interval, $zone->precision]);
56 }
57
58 public function testOptionalLowersTheOperandAndTheElseOfCase(): void
59 {
60 $platform = new Platform();
61 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
62 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
63 $tree = $platform->parser($profile)->parse('SELECT CASE 1 WHEN 2 THEN 3 ELSE 4 END');
64 $rule = new ConstructRule($lowering);
65
66 self::assertInstanceOf(NumberLiteral::class, $rule->optional($tree->find('opt_expr')[0]));
67 self::assertInstanceOf(NumberLiteral::class, $rule->optional($tree->find('opt_else')[0]));
68 self::assertNull($rule->optional($platform->parser($profile)->parse('SELECT CASE WHEN 2 THEN 3 END')->find('opt_else')[0]));
69 }
70
71 public function testBranchesKeepsTheOrderOfTheWhenBranches(): void
72 {
73 $platform = new Platform();
74 $profile = $platform->profile('mysql-5.6.51', null, ParameterStyle::Native);
75 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
76 $branches = (new ConstructRule($lowering))->branches($platform->parser($profile)->parse('SELECT CASE WHEN 1 THEN 2 WHEN 3 THEN 4 WHEN 5 THEN 6 END')->find('when_list')[0]);
77
78 self::assertCount(3, $branches);
79 self::assertInstanceOf(NumberLiteral::class, $branches[2]->result);
80 self::assertSame('6', $branches[2]->result->text);
81 }
82
83 public function testFlagTellsWhetherArrayIsWritten(): void
84 {
85 $platform = new Platform();
86 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
87 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
88 $rule = new ConstructRule($lowering);
89
90 self::assertSame([true, false], [$rule->flag($platform->parser($profile)->parse('SELECT CAST(1 AS SIGNED ARRAY)')->find('opt_array_cast')[0]), $rule->flag($platform->parser($profile)->parse('SELECT CAST(1 AS SIGNED)')->find('opt_array_cast')[0])]);
91 }
92
93 public function testColumnsLowersTheColumnsWithAndWithoutParentheses(): void
94 {
95 $platform = new Platform();
96 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
97 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
98 $rule = new ConstructRule($lowering);
99
100 self::assertCount(2, $rule->columns($platform->parser($profile)->parse("SELECT MATCH a, b AGAINST ('x')")->find('ident_list_arg')[0]));
101 self::assertCount(1, $rule->columns($platform->parser($profile)->parse("SELECT MATCH (a) AGAINST ('x')")->find('ident_list_arg')[0]));
102 }
103
104 public function testModeLowersTheSearchModes(): void
105 {
106 $platform = new Platform();
107 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
108 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
109 $rule = new ConstructRule($lowering);
110 $parser = $platform->parser($profile);
111
112 self::assertSame(
113 [FullTextMode::NaturalLanguage, FullTextMode::QueryExpansion, FullTextMode::Boolean],
114 [
115 $rule->mode($parser->parse("SELECT MATCH (a) AGAINST ('x' IN NATURAL LANGUAGE MODE)")->find('fulltext_options')[0]),
116 $rule->mode($parser->parse("SELECT MATCH (a) AGAINST ('x' WITH QUERY EXPANSION)")->find('fulltext_options')[0]),
117 $rule->mode($parser->parse("SELECT MATCH (a) AGAINST ('x' IN BOOLEAN MODE)")->find('fulltext_options')[0]),
118 ],
119 );
120 }
121
122 public function testSearchKeepsTheParenthesesAndTheStatedMode(): void
123 {
124 $platform = new Platform();
125 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
126 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
127 $node = $platform->parser($profile)->parse("SELECT MATCH a, b AGAINST ('x' IN NATURAL LANGUAGE MODE)")->find('simple_expr')[0];
128 $search = (new ConstructRule($lowering))->search($lowering->form($node));
129
130 self::assertCount(2, $search->columns);
131 self::assertSame(OptionalWords::Omitted, $search->parentheses);
132 self::assertSame(OptionalWords::Written, $search->stated);
133 self::assertSame(FullTextMode::NaturalLanguage, $search->mode);
134 }
135}
136