packages/sql-semantics-mysql/tests/Unit/Statement/Literal/TemporalLiteralTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
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\Type\Known;
12use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\MySql\Semantics;
14use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
15use SqlSemantics\Platform\MySql\Statement\Literal\EscapeRule;
16use SqlSemantics\Platform\MySql\Statement\Literal\TemporalForm;
17use SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral;
18use SqlSemantics\Platform\MySql\Statement\Query\Select;
19use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\TemporalKind;
21use SqlSemantics\Platform\MySql\Statement\Type\Temporal;
22
23#[CoversClass(TemporalLiteral::class)]
24#[Medium]
25final class TemporalLiteralTest extends TestCase
26{
27    public function testTypeAnswersDateWithoutAPrecision(): void
28    {
29        $type = (new TemporalLiteral(TemporalForm::Date, '2024-01-02 03:04:05.123'))->type();
30
31        self::assertSame(TemporalKind::Date, $type->kind);
32        self::assertNull($type->precision);
33    }
34
35    public function testTypeCountsTheFractionalDigitsUpToSix(): void
36    {
37        self::assertNull((new TemporalLiteral(TemporalForm::Time, '10:11:12'))->type()->precision);
38        self::assertNull((new TemporalLiteral(TemporalForm::Time, '10:11:12.'))->type()->precision);
39        self::assertSame('1', (new TemporalLiteral(TemporalForm::Time, '10:11:12.5'))->type()->precision);
40        self::assertSame('6', (new TemporalLiteral(TemporalForm::Timestamp, '2024-01-02 03:04:05.123456789'))->type()->precision);
41    }
42
43    public function testTypeAnswersDateTimeForTimestampAndIgnoresATimeZoneOffset(): void
44    {
45        $type = (new TemporalLiteral(TemporalForm::Timestamp, '2024-01-02 03:04:05.12+05:30'))->type();
46
47        self::assertSame(TemporalKind::DateTime, $type->kind);
48        self::assertSame('2', $type->precision);
49        self::assertSame(TemporalKind::Time, (new TemporalLiteral(TemporalForm::Time, '10:11:12.123-01:00'))->type()->kind);
50        self::assertSame('3', (new TemporalLiteral(TemporalForm::Time, '10:11:12.123-01:00'))->type()->precision);
51    }
52
53    public function testDeriveScalarAnswersTheTemporalTypeThatIsNeverNull(): void
54    {
55        $operation = (new Semantics())->analyze("SELECT a FROM t WHERE a = TIMESTAMP '2024-01-02 03:04:05.25'");
56        $select = $operation->statement;
57        self::assertInstanceOf(Select::class, $select);
58        self::assertInstanceOf(Comparison::class, $select->where);
59        $literal = $select->where->right;
60        self::assertInstanceOf(TemporalLiteral::class, $literal);
61        $fact = $operation->facts->scalar($literal);
62
63        self::assertSame(TemporalForm::Timestamp, $literal->form);
64        self::assertSame('2024-01-02 03:04:05.25', $literal->text);
65        self::assertSame(EscapeRule::Backslash, $literal->escapes);
66        self::assertInstanceOf(Known::class, $fact->type);
67        self::assertInstanceOf(Temporal::class, $fact->type->descriptor);
68        self::assertSame(TemporalKind::DateTime, $fact->type->descriptor->kind);
69        self::assertSame('2', $fact->type->descriptor->precision);
70        self::assertSame(Nullability::NotNull, $fact->nullability);
71    }
72
73    public function testDeriveScalarAnswersDateForADateLiteral(): void
74    {
75        $operation = (new Semantics())->analyze("SELECT DATE '2024-01-02'");
76        $select = $operation->statement;
77        self::assertInstanceOf(Select::class, $select);
78        $item = $select->items[0];
79        self::assertInstanceOf(SelectExpression::class, $item);
80        $fact = $operation->facts->scalar($item->expression);
81
82        self::assertInstanceOf(Known::class, $fact->type);
83        self::assertInstanceOf(Temporal::class, $fact->type->descriptor);
84        self::assertSame(TemporalKind::Date, $fact->type->descriptor->kind);
85        self::assertSame(Nullability::NotNull, $operation->field(0)->nullability);
86    }
87
88    public function testDeriveScalarRejectsALiteralSpelledUnderTheOtherEscapeRule(): void
89    {
90        $context = (new Semantics())->context();
91        $statement = new Select([], [new SelectExpression(new TemporalLiteral(TemporalForm::Date, '2024-01-02', EscapeRule::Verbatim))]);
92
93        $this->expectExceptionMessage('A temporal literal must be spelled under the escape rule of the language profile.');
94
95        new Operation($context, $statement);
96    }
97
98    public function testRenderWritesTheKeywordAndTheQuotedText(): void
99    {
100        $semantics = new Semantics();
101
102        self::assertSame("SELECT DATE '2024-01-02' AS v", $semantics->analyze("select date '2024-01-02' as v")->toString());
103        self::assertSame("SELECT TIME 'a''b' AS v", $semantics->analyze("SELECT TIME 'a\\'b' AS v")->toString());
104        self::assertSame("SELECT TIMESTAMP '2024-01-02 03:04:05' AS v", $semantics->analyze('SELECT TIMESTAMP "2024-01-02 03:04:05" AS v')->toString());
105        self::assertSame("SELECT date '2024-01-02'", $semantics->analyze("SELECT date '2024-01-02'")->toString());
106    }
107}
108