packages/sql-semantics-mysql/tests/Unit/Statement/Query/Set/LeadingUnionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\Set;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Operation;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
13use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
14use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockingClause;
15use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockStrength;
16use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
17use SqlSemantics\Platform\MySql\Statement\Query\Select;
18use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
19use SqlSemantics\Platform\MySql\Statement\Query\Set\LeadingUnion;
20use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
21use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator;
22use SqlSemantics\Platform\MySql\Statement\Query\Set\SetQuantifier;
23use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
24
25#[CoversClass(LeadingUnion::class)]
26#[Medium]
27final class LeadingUnionTest extends TestCase
28{
29 public function testRenderKeepsTheOwnClausesOfAnEarlierSelect(): void
30 {
31 $operation = (new Semantics('mysql-5.6.51'))->analyze('select 1 union all select a from t for update union select 3 limit 1 union select 4');
32 $union = $operation->statement;
33 self::assertInstanceOf(SetOperation::class, $union);
34 $leading = $union->left;
35 self::assertInstanceOf(LeadingUnion::class, $leading);
36 $first = $leading->left;
37 self::assertInstanceOf(LeadingUnion::class, $first);
38
39 self::assertNotNull($leading->right->limit);
40 self::assertCount(1, $first->right->locking);
41 self::assertSame(SetQuantifier::All, $first->quantifier);
42 self::assertInstanceOf(Select::class, $first->left);
43 self::assertCount(1, $operation->facts->query($first->left)->projection);
44 self::assertSame([], $operation->facts->diagnostics);
45 }
46
47 public function testRenderNeedsALastOperandWithOnlyItsOwnLimitOrLocking(): void
48 {
49 $one = new Select([], [new SelectExpression(new NumberLiteral('1'))]);
50 $ordered = new Select([], [new SelectExpression(new NumberLiteral('2'))], new Dual(), null, null, null, [], null, [new OrderItem(new NumberLiteral('1.5'))]);
51
52 $this->expectExceptionMessage('The last operand of a leading union keeps its own LIMIT or locking clauses and nothing else.');
53
54 new LeadingUnion($one, null, $ordered);
55 }
56
57 public function testRenderNeedsUnionAfterIt(): void
58 {
59 $limited = new Select([], [new SelectExpression(new NumberLiteral('2'))], null, null, null, null, [], null, [], new RowLimit(new NumberLiteral('1')));
60 $leading = new LeadingUnion(new Select([], [new SelectExpression(new NumberLiteral('1'))]), null, $limited);
61
62 $this->expectExceptionMessage('A leading union continues with UNION.');
63
64 new SetOperation($leading, SetOperator::Intersect, null, new Select([], [new SelectExpression(new NumberLiteral('3'))]));
65 }
66
67 public function testRenderWritesTheOperandsAroundUnion(): void
68 {
69 $semantics = new Semantics('mysql-5.7.44');
70
71 self::assertSame('SELECT 1 FROM t FOR UPDATE UNION DISTINCT SELECT 2 FROM t LOCK IN SHARE MODE UNION SELECT 3', $semantics->analyze('SELECT 1 FROM t FOR UPDATE UNION DISTINCT SELECT 2 FROM t LOCK IN SHARE MODE UNION SELECT 3')->toString());
72 }
73
74 public function testRenderNeedsMySql5ForTheFormAndMySql56ForAnOwnLimit(): void
75 {
76 $locked = new Select([], [new SelectExpression(new NumberLiteral('2'))], new Dual(), null, null, null, [], null, [], null, null, [new LockingClause(LockStrength::Update)]);
77 $union = new SetOperation(new LeadingUnion(new Select([], [new SelectExpression(new NumberLiteral('1'))]), null, $locked), SetOperator::Union, null, new Select([], [new SelectExpression(new NumberLiteral('3'))]));
78 $accepted = new Operation((new Semantics('mysql-5.7.44'))->context([]), $union);
79 $limited = new Select([], [new SelectExpression(new NumberLiteral('2'))], null, null, null, null, [], null, [], new RowLimit(new NumberLiteral('1')));
80 $limitedUnion = new SetOperation(new LeadingUnion(new Select([], [new SelectExpression(new NumberLiteral('1'))]), null, $limited), SetOperator::Union, null, new Select([], [new SelectExpression(new NumberLiteral('3'))]));
81
82 self::assertSame('SELECT 1 UNION SELECT 2 FROM DUAL FOR UPDATE UNION SELECT 3', $accepted->toString());
83 $this->expectExceptionMessage('A SELECT that keeps its own LIMIT before a later UNION needs MySQL 5.6.');
84
85 new Operation((new Semantics('mysql-5.7.44'))->context([]), $limitedUnion);
86 }
87}
88