packages/sql-semantics-mysql/tests/Unit/Statement/Query/ParenthesizedQueryTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Query\ParenthesizedQuery;
17use SqlSemantics\Platform\MySql\Statement\Query\Select;
18use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
19use SqlSemantics\Platform\MySql\Statement\Type\Integral;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
21
22#[CoversClass(ParenthesizedQuery::class)]
23#[Medium]
24final class ParenthesizedQueryTest extends TestCase
25{
26 public function testDeriveStatementRecordsTheRowsOfTheInnerQuery(): void
27 {
28 $semantics = new Semantics();
29 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
30 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
31 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
32 ]);
33 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
34 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
35 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
36 ]);
37 $operation = $semantics->analyze('(SELECT a FROM t)', [$t]);
38
39 self::assertInstanceOf(ParenthesizedQuery::class, $operation->statement);
40 self::assertInstanceOf(Select::class, $operation->statement->query);
41 self::assertSame($operation->facts->query($operation->statement->query), $operation->facts->output);
42 }
43
44 public function testDeriveQueryAnswersTheFactsOfTheInnerQuery(): void
45 {
46 $semantics = new Semantics();
47 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
48 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
49 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
50 ]);
51 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
52 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
53 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
54 ]);
55 $operation = $semantics->analyze('((SELECT a FROM t)) UNION SELECT c FROM u', [$t, $u]);
56
57 self::assertInstanceOf(SetOperation::class, $operation->statement);
58 self::assertInstanceOf(ParenthesizedQuery::class, $operation->statement->left);
59 self::assertInstanceOf(ParenthesizedQuery::class, $operation->statement->left->query);
60 self::assertSame($t->columns[0], $operation->facts->query($operation->statement->left)->fields()?->at(0)->column());
61 }
62
63 public function testRenderWritesTheParentheses(): void
64 {
65 self::assertSame('((SELECT 1)) UNION SELECT 2', (new Semantics())->analyze('((select 1)) union select 2')->toString());
66 }
67}
68