packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Access/OdbcEscapeTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Access;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Access\OdbcEscape;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\TemporalKind;
21use SqlSemantics\Platform\MySql\Statement\Type\Temporal;
22
23#[CoversClass(OdbcEscape::class)]
24#[Medium]
25final class OdbcEscapeTest extends TestCase
26{
27    public function testDeriveScalarReadsATemporalLiteralForTheTemporalKinds(): void
28    {
29        $platform = new Platform();
30        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
31        $timestamp = $derivation->scalar(new OdbcEscape(new Name('TS'), new StringLiteral(['2024-01-31 10:00:00'])), $derivation->environment());
32        $other = $derivation->scalar(new OdbcEscape(new Name('fn'), new NumberLiteral('1')), $derivation->environment());
33
34        self::assertEquals(new Known(new Temporal(TemporalKind::DateTime)), $timestamp->type);
35        self::assertInstanceOf(Known::class, $other->type);
36        self::assertSame('BIGINT', $other->type->descriptor->name());
37    }
38
39    public function testRenderWritesTheBraces(): void
40    {
41        $platform = new Platform();
42        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
43        (new OdbcEscape(new Name('t'), new StringLiteral(['10:00'])))->render($out);
44
45        self::assertSame("{ t '10:00' }", (new Lexical())->join($out->pieces()));
46    }
47}
48