packages/sql-semantics-mysql/tests/Unit/Statement/Relation/OdbcJoinTest.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\OdbcJoin;
17use SqlSemantics\Platform\MySql\Statement\Type\Integral;
18use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
19
20#[CoversClass(OdbcJoin::class)]
21#[Medium]
22final class OdbcJoinTest extends TestCase
23{
24 public function testDeriveRelationDerivesTheEscapedReference(): 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 c FROM { OJ t LEFT JOIN u ON t.a = u.a }', [$t, $u]);
36
37 self::assertSame(Nullability::Nullable, $operation->field('c')->nullability);
38 self::assertSame([], $operation->facts->diagnostics);
39 }
40
41 public function testRenderWritesTheEscape(): void
42 {
43 self::assertSame('SELECT 1 FROM { OJ t LEFT JOIN u ON t.a = u.a }', (new Semantics())->analyze('select 1 from {oj t left join u on t.a=u.a}')->toString());
44 }
45}
46