packages/sql-semantics-mysql/tests/Unit/Statement/Relation/NestedRelationTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation;
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\Relation\NestedRelation;
17use SqlSemantics\Platform\MySql\Statement\Type\Integral;
18use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
19
20#[CoversClass(NestedRelation::class)]
21#[Medium]
22final class NestedRelationTest extends TestCase
23{
24    public function testDeriveRelationKeepsTheInnerTablesVisible(): void
25    {
26        $semantics = new Semantics();
27        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
28            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
29            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
30        ]);
31        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
32            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
33            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
34        ]);
35        $operation = $semantics->analyze('SELECT t.a, c FROM (t, u)', [$t, $u]);
36
37        self::assertSame($t->columns[0], $operation->field(0)->column());
38        self::assertSame($u->columns[1], $operation->field(1)->column());
39        self::assertSame([], $operation->facts->diagnostics);
40    }
41
42    public function testRenderWritesTheParentheses(): void
43    {
44        self::assertSame('SELECT 1 FROM ((t)), ((t JOIN u)), ((t, u), v)', (new Semantics())->analyze('select 1 from ((t)), ((t join u)), ((t, u), v)')->toString());
45        self::assertSame('SELECT 1 FROM (t, u)', (new Semantics('mysql-5.7.44'))->analyze('select 1 from (t, u)')->toString());
46    }
47}
48