packages/sql-semantics-mysql/tests/Unit/Lowering/Query/QueryRulesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Lexer\Token;
11use SqlParser\Parser\Node;
12use SqlSemantics\Platform\MySql\Lowering\Lowering;
13use SqlSemantics\Platform\MySql\Lowering\Query\QueryRules;
14use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
15use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Query\ParenthesizedQuery;
20use SqlSemantics\Platform\MySql\Statement\Query\Select;
21use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
22use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
23use SqlSemantics\Platform\MySql\Statement\Query\Star;
24
25#[CoversClass(QueryRules::class)]
26#[Medium]
27final class QueryRulesTest extends TestCase
28{
29 public function testStatementLowersASelectOfEveryGrammarGeneration(): void
30 {
31 self::assertSame('SELECT a x FROM db.t u WHERE a = 1', (new Semantics('mysql-9.1.0'))->inspect('select a x from db.t u where a=1')->toString());
32 self::assertSame('SELECT a x FROM db.t u WHERE a = 1', (new Semantics('mysql-5.7.44'))->inspect('select a x from db.t u where a=1')->toString());
33 self::assertSame('SELECT a x FROM db.t u WHERE a = 1', (new Semantics('mysql-5.6.51'))->inspect('select a x from db.t u where a=1')->toString());
34 }
35
36 public function testQueryLowersTheQueriesOfOtherStatements(): void
37 {
38 self::assertSame('SELECT (SELECT 1) AS v, a IN (SELECT 2) AS w FROM (SELECT 3) d WHERE EXISTS (SELECT 4)', (new Semantics('mysql-8.4.7'))->inspect('select (select 1) as v, a in (select 2) as w from (select 3) d where exists (select 4)')->toString());
39 self::assertSame('SELECT (SELECT 1) AS v FROM t WHERE EXISTS (SELECT 2)', (new Semantics('mysql-5.6.51'))->inspect('select (select 1) as v from t where exists (select 2)')->toString());
40 }
41
42 public function testLegacyQueryLowersCreateSelectWithItsUnion(): void
43 {
44 $platform = new Platform();
45 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
46 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
47 $tree = $platform->parser($profile)->parse('CREATE TABLE t SELECT a FROM u UNION SELECT 2');
48 $verb = $tree->children[0];
49 self::assertInstanceOf(Node::class, $verb);
50 $statement = $verb->children[0];
51 self::assertInstanceOf(Node::class, $statement);
52 $create = $statement->children[0];
53 self::assertInstanceOf(Node::class, $create);
54 $second = $create->children[5];
55 self::assertInstanceOf(Node::class, $second);
56 $third = $second->children[2];
57 self::assertInstanceOf(Node::class, $third);
58 $select = $third->children[2];
59 $union = $third->children[3];
60 self::assertInstanceOf(Node::class, $select);
61 self::assertInstanceOf(Node::class, $union);
62
63 self::assertInstanceOf(SetOperation::class, (new QueryRules($lowering))->legacyQuery($select, $union));
64 self::assertInstanceOf(Select::class, (new QueryRules($lowering))->legacyQuery($select));
65 }
66
67 public function testLegacyParenthesizedQueryKeepsTheParenthesesAndTheUnion(): void
68 {
69 $platform = new Platform();
70 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
71 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
72 $tree = $platform->parser($profile)->parse('INSERT INTO t (SELECT a FROM u LIMIT 1) UNION SELECT 2');
73 $verb = $tree->children[0];
74 self::assertInstanceOf(Node::class, $verb);
75 $statement = $verb->children[0];
76 self::assertInstanceOf(Node::class, $statement);
77 $insert = $statement->children[0];
78 self::assertInstanceOf(Node::class, $insert);
79 $source = $insert->children[6];
80 self::assertInstanceOf(Node::class, $source);
81 $expression = $source->children[0];
82 self::assertInstanceOf(Node::class, $expression);
83 $select = $expression->children[1];
84 $union = $expression->children[3];
85 self::assertInstanceOf(Node::class, $select);
86 self::assertInstanceOf(Node::class, $union);
87 $query = (new QueryRules($lowering))->legacyParenthesizedQuery($select, $union);
88
89 self::assertInstanceOf(SetOperation::class, $query);
90 self::assertInstanceOf(ParenthesizedQuery::class, $query->left);
91 self::assertInstanceOf(Select::class, $query->left->query);
92 self::assertNotNull($query->left->query->limit);
93 }
94
95 public function testWhereAnswersNullForAnAbsentClause(): void
96 {
97 $platform = new Platform();
98 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
99 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
100 self::assertNull((new QueryRules($lowering))->where(new Node('opt_where_clause', 0, [])));
101 }
102
103 public function testOrderingAnswersNothingForAnAbsentClause(): void
104 {
105 $platform = new Platform();
106 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
107 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
108 self::assertSame([], (new QueryRules($lowering))->ordering(new Node('opt_order_clause', 0, [])));
109 }
110
111 public function testOrderItemLowersAnOrderingItemOfGroupConcat(): void
112 {
113 self::assertSame('SELECT GROUP_CONCAT(a ORDER BY a DESC) AS g FROM t', (new Semantics('mysql-5.6.51'))->inspect('select group_concat(a order by a desc) as g from t')->toString());
114 }
115
116 public function testDirectionAnswersNullForNoDirection(): void
117 {
118 $platform = new Platform();
119 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
120 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
121 self::assertNull((new QueryRules($lowering))->direction(new Node('opt_ordering_direction', 0, [])));
122 }
123
124 public function testLimitAnswersNullForAnAbsentClause(): void
125 {
126 $platform = new Platform();
127 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
128 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
129 self::assertNull((new QueryRules($lowering))->limit(new Node('opt_limit_clause', 0, [])));
130 }
131
132 public function testLimitValueLowersAnInteger(): void
133 {
134 $platform = new Platform();
135 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
136 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
137 $value = (new QueryRules($lowering))->limitValue(new Node('limit_option', 4, [new Token(0, 'NUM', '7', 0)]));
138
139 self::assertInstanceOf(NumberLiteral::class, $value);
140 self::assertSame('7', $value->text);
141 }
142
143 public function testTablesLowersTheReferencesOfAMultipleTableStatement(): void
144 {
145 $platform = new Platform();
146 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
147 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
148 $tree = $platform->parser($profile)->parse('SELECT 1 FROM t, u');
149 $verb = $tree->children[0];
150 self::assertInstanceOf(Node::class, $verb);
151 $statement = $verb->children[0];
152 self::assertInstanceOf(Node::class, $statement);
153 $select = $statement->children[0];
154 self::assertInstanceOf(Node::class, $select);
155 $init = $select->children[0];
156 self::assertInstanceOf(Node::class, $init);
157 $part = $init->children[1];
158 self::assertInstanceOf(Node::class, $part);
159 $from = $part->children[2];
160 self::assertInstanceOf(Node::class, $from);
161 $list = $from->children[1];
162 self::assertInstanceOf(Node::class, $list);
163
164 self::assertCount(2, (new QueryRules($lowering))->tables($list));
165 }
166
167 public function testPartitionsAnswersNothingForNoSelection(): void
168 {
169 $platform = new Platform();
170 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
171 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
172 self::assertSame([], (new QueryRules($lowering))->partitions(new Node('opt_use_partition', 0, [])));
173 }
174
175 public function testAliasLowersColumnAndTableAliases(): void
176 {
177 $operation = (new Semantics('mysql-5.7.44'))->inspect("SELECT a AS `x y`, b 'z' FROM t AS u");
178
179 self::assertInstanceOf(Select::class, $operation->statement);
180 self::assertInstanceOf(SelectExpression::class, $operation->statement->items[0]);
181 self::assertSame('x y', $operation->statement->items[0]->alias?->value);
182 self::assertSame('SELECT a AS `x y`, b z FROM t AS u', $operation->toString());
183 }
184
185 public function testColumnAliasesAnswersNothingForNoList(): void
186 {
187 $platform = new Platform();
188 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
189 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
190 self::assertSame([], (new QueryRules($lowering))->columnAliases(new Node('opt_derived_column_list', 0, [])));
191 }
192
193 public function testWithAnswersNullForNoClause(): void
194 {
195 $platform = new Platform();
196 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
197 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
198 self::assertNull((new QueryRules($lowering))->with(new Node('opt_with_clause', 0, [])));
199 }
200
201 public function testSelectItemsLowersTheStar(): void
202 {
203 $platform = new Platform();
204 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
205 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
206 $items = (new QueryRules($lowering))->selectItems(new Node('select_item_list', 2, [new Token(0, '*', '*', 0)]));
207
208 self::assertCount(1, $items);
209 self::assertInstanceOf(Star::class, $items[0]);
210 }
211
212 public function testIndexNamesAnswersNothingForNoList(): void
213 {
214 $platform = new Platform();
215 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
216 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
217 self::assertSame([], (new QueryRules($lowering))->indexNames(new Node('opt_key_usage_list', 0, [])));
218 }
219
220 public function testIndexKeysAnswersNothingForNoList(): void
221 {
222 $platform = new Platform();
223 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
224 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
225 self::assertSame([], (new QueryRules($lowering))->indexKeys(new Node('opt_key_usage_list', 0, [])));
226 }
227
228 public function testMarkAnswersWhatIsWrittenBeforeAColumnOrTableAlias(): void
229 {
230 self::assertSame('SELECT a x, b AS y FROM t u', (new Semantics())->inspect('select a x, b as y from t u')->toString());
231 self::assertSame('SELECT f(a b, c AS d) AS e FROM t', (new Semantics())->inspect('select f(a b, c as d) as e from t')->toString());
232 }
233}
234