packages/sql-semantics-mysql/tests/Unit/Statement/Query/Set/OrderedSetOperationTest.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\Model\Statement\Reference\Column\AliasTarget;
12use SqlSemantics\Platform\MySql\Semantics;
13use SqlSemantics\Platform\MySql\Statement\Expression\Subquery\ScalarSubquery;
14use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
15use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
16use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockingClause;
17use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockStrength;
18use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
19use SqlSemantics\Platform\MySql\Statement\Query\Select;
20use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
21use SqlSemantics\Platform\MySql\Statement\Query\Set\OrderedSetOperation;
22use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
23use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator;
24use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
25
26#[CoversClass(OrderedSetOperation::class)]
27#[Medium]
28final class OrderedSetOperationTest extends TestCase
29{
30 public function testDeriveQueryOrdersTheResultAndKeepsTheClausesOfTheLastSelect(): void
31 {
32 $operation = (new Semantics('mysql-5.6.51'))->analyze('SELECT (SELECT 1 AS x UNION SELECT 2 FROM DUAL LIMIT 1 ORDER BY x LIMIT 3)', []);
33 $select = $operation->statement;
34 self::assertInstanceOf(Select::class, $select);
35 $item = $select->items[0];
36 self::assertInstanceOf(SelectExpression::class, $item);
37 $subquery = $item->expression;
38 self::assertInstanceOf(ScalarSubquery::class, $subquery);
39 $union = $subquery->query;
40 self::assertInstanceOf(OrderedSetOperation::class, $union);
41
42 self::assertNotNull($union->right->limit);
43 self::assertNotNull($union->limit);
44 self::assertInstanceOf(AliasTarget::class, $operation->facts->scalar($union->orderBy[0]->expression)->resolution);
45 self::assertSame('x', $operation->facts->query($union)->projection[0]->name?->value);
46 }
47
48 public function testDeriveQueryNeedsMySql56(): void
49 {
50 $one = new Select([], [new SelectExpression(new NumberLiteral('1'))]);
51 $two = new Select([], [new SelectExpression(new NumberLiteral('2'))], new Dual(), null, null, null, [], null, [], null, null, [new LockingClause(LockStrength::Update)]);
52 $union = new OrderedSetOperation($one, SetOperator::Union, null, $two, [], new RowLimit(new NumberLiteral('1')));
53
54 $this->expectExceptionMessage('A set operation ordered after the clauses of its last SELECT needs MySQL 5.6.');
55
56 new Operation((new Semantics('mysql-5.7.44'))->context([]), new Select([], [new SelectExpression(new ScalarSubquery($union))]));
57 }
58
59 public function testRenderWritesTheOrderingAfterTheLockingClauseOfTheLastSelect(): void
60 {
61 $semantics = new Semantics('mysql-5.6.51');
62
63 self::assertSame('SELECT (SELECT 1 UNION SELECT a FROM t FOR UPDATE ORDER BY 1) AS v', $semantics->analyze('select (select 1 union select a from t for update order by 1) as v')->toString());
64 self::assertInstanceOf(QueryExpression::class, $semantics->analyze('SELECT 1 UNION SELECT a FROM t ORDER BY 1')->statement);
65 }
66
67 public function testRejectsALastSelectWithoutClausesOfItsOwn(): void
68 {
69 $this->expectExceptionMessage('The last SELECT of an ordered set operation writes an ORDER BY, LIMIT or locking clause of its own and nothing after them.');
70
71 new OrderedSetOperation(new Select([], [new SelectExpression(new NumberLiteral('1'))]), SetOperator::Union, null, new Select([], [new SelectExpression(new NumberLiteral('2'))]), [], new RowLimit(new NumberLiteral('1')));
72 }
73
74 public function testRejectsAsAnOperandOfAnotherSetOperation(): void
75 {
76 $two = new Select([], [new SelectExpression(new NumberLiteral('2'))], new Dual(), null, null, null, [], null, [], null, null, [new LockingClause(LockStrength::Update)]);
77 $union = new OrderedSetOperation(new Select([], [new SelectExpression(new NumberLiteral('1'))]), SetOperator::Union, null, $two, [], new RowLimit(new NumberLiteral('1')));
78
79 $this->expectExceptionMessage('A set operation ordered after its last SELECT ends its subquery.');
80
81 new SetOperation($union, SetOperator::Union, null, new Select([], [new SelectExpression(new NumberLiteral('3'))]));
82 }
83}
84