packages/sql-semantics-mysql/tests/Unit/Lowering/Call/TemporalRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Parser\Node;
11use SqlSemantics\Platform\MySql\Lowering\Call\TemporalRule;
12use SqlSemantics\Platform\MySql\Lowering\Lowering;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Statement\Call\Clock;
17use SqlSemantics\Platform\MySql\Statement\Call\ClockCall;
18use SqlSemantics\Platform\MySql\Statement\Call\Extract;
19use SqlSemantics\Platform\MySql\Statement\Call\Temporal\DateArithmetic;
20use SqlSemantics\Platform\MySql\Statement\Call\Temporal\GetFormat;
21use SqlSemantics\Platform\MySql\Statement\Call\Temporal\TemporalFormat;
22use SqlSemantics\Platform\MySql\Statement\Call\Temporal\TimestampCall;
23use SqlSemantics\Platform\MySql\Statement\Call\Temporal\TimestampOperation;
24use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
25use SqlSemantics\Platform\MySql\Statement\Expression\OptionalWords;
26use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
27
28#[CoversClass(TemporalRule::class)]
29#[Medium]
30final class TemporalRuleTest extends TestCase
31{
32    public function testClaimsTellsTheProductionsOfTheRule(): void
33    {
34        $platform = new Platform();
35        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
36        $rule = new TemporalRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
37
38        self::assertTrue($rule->claims('function_call_nonkeyword: now'));
39        self::assertFalse($rule->claims('function_call_keyword: TRIM ( expr )'));
40    }
41
42    public function testCallLowersTheTemporalSyntaxes(): void
43    {
44        $platform = new Platform();
45        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
46        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
47        $list = $platform->parser($profile)->parse('SELECT UTC_TIME(2), CURRENT_DATE, SUBDATE(a, INTERVAL 1 DAY), EXTRACT(HOUR FROM a), GET_FORMAT(DATETIME, a), TIMESTAMPADD(SQL_TSI_DAY, 1, a), NOW()')->find('select_item_list')[0];
48        $rule = new TemporalRule($lowering);
49        $calls = array_map(static fn (Node $node): \SqlSemantics\Platform\MySql\Model\Statement\Scalar => $rule->call($lowering->form($node)), $list->find('function_call_nonkeyword'));
50
51        self::assertEquals(new ClockCall(Clock::UtcTime, new Numeral('2')), $calls[0]);
52        self::assertEquals(new ClockCall(Clock::CurrentDate, null, OptionalWords::Omitted), $calls[1]);
53        self::assertInstanceOf(DateArithmetic::class, $calls[2]);
54        self::assertTrue($calls[2]->subtract);
55        self::assertInstanceOf(Extract::class, $calls[3]);
56        self::assertInstanceOf(GetFormat::class, $calls[4]);
57        self::assertSame(TemporalFormat::DateTime, $calls[4]->format);
58        self::assertInstanceOf(TimestampCall::class, $calls[5]);
59        self::assertSame(IntervalUnit::Day, $calls[5]->unit);
60        self::assertEquals(new ClockCall(Clock::Now), $calls[6]);
61    }
62
63    public function testCallReportsAProductionOutsideTheRule(): void
64    {
65        $platform = new Platform();
66        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
67        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
68        $node = $platform->parser($profile)->parse('SELECT LEFT(1, 2)')->find('function_call_keyword')[0];
69        $this->expectExceptionMessage('No semantic rule is implemented for: ');
70
71        (new TemporalRule($lowering))->call($lowering->form($node));
72    }
73
74    public function testTimestampLowersTheUnitAndTheOperands(): void
75    {
76        $platform = new Platform();
77        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
78        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
79        $node = $platform->parser($profile)->parse('SELECT TIMESTAMPDIFF(MONTH, a, b)')->find('function_call_nonkeyword')[0];
80        self::assertSame(IntervalUnit::Month, (new TemporalRule($lowering))->timestamp($lowering->form($node), TimestampOperation::Difference)->unit);
81    }
82
83    public function testCurrentTimestampLowersNow(): void
84    {
85        $platform = new Platform();
86        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
87        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
88        $node = $platform->parser($profile)->parse('CREATE TABLE t (c TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6))')->find('now')[0];
89        self::assertEquals(new ClockCall(Clock::Now, new Numeral('6')), (new TemporalRule($lowering))->currentTimestamp($node));
90    }
91
92    public function testCurrentTimestampReportsAnotherNode(): void
93    {
94        $platform = new Platform();
95        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
96        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
97        $node = $platform->parser($profile)->parse('SELECT 1')->find('select_item_list')[0];
98        $this->expectExceptionMessage('No semantic rule is implemented for: ');
99
100        (new TemporalRule($lowering))->currentTimestamp($node);
101    }
102
103    public function testPrecisionTreatsEmptyParenthesesAsNoPrecision(): void
104    {
105        $platform = new Platform();
106        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
107        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
108        $node = $platform->parser($profile)->parse('SELECT SYSDATE()')->find('func_datetime_precision')[0];
109        self::assertNull((new TemporalRule($lowering))->precision($node));
110    }
111
112    public function testParenthesesTellsWhetherTheParenthesesAreWritten(): void
113    {
114        $platform = new Platform();
115        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
116        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
117        $tree = $platform->parser($profile)->parse('SELECT CURRENT_TIMESTAMP, NOW(), NOW(3)');
118        $precisions = $tree->find('func_datetime_precision');
119
120        self::assertSame([OptionalWords::Omitted, OptionalWords::Written, OptionalWords::Written], array_map(static fn (Node $node): OptionalWords => (new TemporalRule($lowering))->parentheses($node), $precisions));
121    }
122
123    public function testFormatLowersTheKind(): void
124    {
125        $platform = new Platform();
126        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
127        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
128        $node = $platform->parser($profile)->parse('SELECT GET_FORMAT(TIME, a)')->find('date_time_type')[0];
129        self::assertSame(TemporalFormat::Time, (new TemporalRule($lowering))->format($node));
130    }
131}
132