packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Text/ExtractTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Text;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
22use SqlSemantics\Platform\PostgreSql\Platform;
23use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Syntax\ValueFunction;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Syntax\ValueFunctionKind;
26use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Text\Extract;
27use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Text\ExtractUnit;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
30use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
32
33#[CoversClass(Extract::class)]
34#[Small]
35final class ExtractTest extends TestCase
36{
37 public function testFieldNameAnswersTheFieldPassed(): void
38 {
39 self::assertSame(['year', 'dow', 'x'], [(new Extract(ExtractUnit::Year, new NullLiteral()))->fieldName(), (new Extract(new Name('dow'), new NullLiteral()))->fieldName(), (new Extract(new StringConstant('x'), new NullLiteral()))->fieldName()]);
40 }
41
42 public function testOutputNameIsExtract(): void
43 {
44 self::assertSame('extract', (new Extract(ExtractUnit::Day, new NullLiteral()))->outputName()->value);
45 }
46
47 public function testDeriveScalarIsNumericForADate(): void
48 {
49 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
50 $fact = $derivation->scalar(new Extract(ExtractUnit::Year, new ValueFunction(ValueFunctionKind::CurrentDate)), $derivation->environment());
51 self::assertEquals(new ScalarFact(new Known(Builtin::Numeric), Nullability::NotNull), $fact);
52 }
53
54 public function testDeriveScalarDependsForAnAmbiguousSource(): void
55 {
56 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
57 $fact = $derivation->scalar(new Extract(ExtractUnit::Year, new Constant(new StringConstant('2020-01-01'))), $derivation->environment());
58 self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('extract'), new Name('pg_catalog')))]), $fact->type);
59 }
60
61 public function testRenderQuotesAnIdentifierSpelledLikeAKeyword(): void
62 {
63 $extract = new Extract(new Name('year'), new NullLiteral());
64 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
65 $extract->render($out);
66 self::assertSame('EXTRACT("year" FROM NULL)', (new Lexical())->join($out->pieces()));
67 }
68
69 public function testRenderWritesAPlainIdentifierAndAString(): void
70 {
71 $extract = new Extract(new Name('epoch'), new NullLiteral());
72 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
73 $extract->render($out);
74 (new Extract(new StringConstant('dow'), new NullLiteral()))->render($out);
75 self::assertSame('EXTRACT(epoch FROM NULL) EXTRACT(\'dow\' FROM NULL)', (new Lexical())->join($out->pieces()));
76 }
77}
78