packages/sql-semantics-sqlite/tests/Unit/Lowering/Definition/KeyTermRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Lowering\Definition\KeyTermRule;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\Collate;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\ColumnUse;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Grouped;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortDirection;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortTerm;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateIndex;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
21use SqlSemantics\Platform\Sqlite\Statement\Schema\LiteralColumn;
22use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TablePrimaryKey;
23use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TableUnique;
24
25#[CoversClass(KeyTermRule::class)]
26#[Medium]
27final class KeyTermRuleTest extends TestCase
28{
29    public function testKeyTermsWrapAStringUnderAnyParenthesesAndCollations(): void
30    {
31        $statement = (new Semantics())->analyze("CREATE TABLE t (a, b, PRIMARY KEY ((('a') COLLATE nocase) COLLATE binary), UNIQUE (b, 'b' DESC))")->statement;
32
33        self::assertInstanceOf(CreateTable::class, $statement);
34        $key = $statement->constraints[0]->items[0];
35        $unique = $statement->constraints[1]->items[0];
36        self::assertInstanceOf(TablePrimaryKey::class, $key);
37        self::assertInstanceOf(TableUnique::class, $unique);
38        $outer = $key->terms[0]->expression;
39        self::assertInstanceOf(Collate::class, $outer);
40        self::assertInstanceOf(Grouped::class, $outer->operand);
41        self::assertInstanceOf(Collate::class, $outer->operand->operand);
42        self::assertInstanceOf(Grouped::class, $outer->operand->operand->operand);
43        self::assertInstanceOf(LiteralColumn::class, $outer->operand->operand->operand->operand);
44        self::assertInstanceOf(ColumnUse::class, $unique->terms[0]->expression);
45        self::assertInstanceOf(LiteralColumn::class, $unique->terms[1]->expression);
46        self::assertSame(SortDirection::Descending, $unique->terms[1]->direction);
47    }
48
49    public function testIndexTermsWrapAStringUnderAtMostOneCollation(): void
50    {
51        $statement = (new Semantics())->analyze("CREATE INDEX i ON t ('a', 'a' COLLATE nocase, 'a' COLLATE nocase COLLATE binary, ('a'))")->statement;
52
53        self::assertInstanceOf(CreateIndex::class, $statement);
54        self::assertInstanceOf(LiteralColumn::class, $statement->terms[0]->expression);
55        self::assertInstanceOf(Collate::class, $statement->terms[1]->expression);
56        self::assertInstanceOf(LiteralColumn::class, $statement->terms[1]->expression->operand);
57        self::assertInstanceOf(Collate::class, $statement->terms[2]->expression);
58        self::assertInstanceOf(Collate::class, $statement->terms[2]->expression->operand);
59        self::assertInstanceOf(TextLiteral::class, $statement->terms[2]->expression->operand->operand);
60        self::assertInstanceOf(Grouped::class, $statement->terms[3]->expression);
61        self::assertInstanceOf(LiteralColumn::class, $statement->terms[3]->expression->operand);
62    }
63
64    public function testTermLeavesEveryOtherTermAsItIs(): void
65    {
66        $rule = new KeyTermRule();
67        $column = new SortTerm(new ColumnUse(new Name('a')), SortDirection::Descending);
68        $string = new SortTerm(new Grouped(new TextLiteral('a')));
69
70        self::assertSame($column, $rule->term($column, null));
71        self::assertNotSame($string, $rule->term($string, null));
72        self::assertSame(SortDirection::Descending, $rule->term($column, 1)->direction);
73    }
74
75    public function testExpressionRebuildsTheWrappersInOrderAroundTheLiteral(): void
76    {
77        $literal = new TextLiteral('a');
78        $rebuilt = (new KeyTermRule())->expression(new Grouped(new Collate($literal, new Name('nocase'))));
79
80        self::assertInstanceOf(Grouped::class, $rebuilt);
81        self::assertInstanceOf(Collate::class, $rebuilt->operand);
82        self::assertSame('nocase', $rebuilt->operand->collation->value);
83        self::assertInstanceOf(LiteralColumn::class, $rebuilt->operand->operand);
84        self::assertSame($literal, $rebuilt->operand->operand->literal);
85    }
86}
87