packages/sql-parser/tests/Unit/PostgreSql/PostgreSqlParserTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\PostgreSql;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use RuntimeException;
12use SqlParser\Lexer\LexicalException;
13use SqlParser\Lexer\ParameterSyntax;
14use SqlParser\Lexer\Token;
15use SqlParser\Parser\SyntaxException;
16use SqlParser\PostgreSql\PostgreSqlParser;
17use SqlParser\PostgreSql\PostgreSqlVersion;
18
19#[CoversClass(PostgreSqlParser::class)]
20#[UsesClass(PostgreSqlVersion::class)]
21#[UsesClass(ParameterSyntax::class)]
22#[UsesClass(SyntaxException::class)]
23#[UsesClass(LexicalException::class)]
24#[UsesClass(Token::class)]
25#[UsesClass(\SqlParser\Grammar\SymbolTable::class)]
26#[UsesClass(\SqlParser\Lexer\Cursor::class)]
27#[UsesClass(\SqlParser\Lexer\Lexeme::class)]
28#[UsesClass(\SqlParser\Lexer\SourcePosition::class)]
29#[UsesClass(\SqlParser\Lexer\TerminalIndex::class)]
30#[UsesClass(\SqlParser\Parser\LrParser::class)]
31#[UsesClass(\SqlParser\Parser\Node::class)]
32#[UsesClass(\SqlParser\PostgreSql\Lexer\KeywordTable::class)]
33#[UsesClass(\SqlParser\PostgreSql\Lexer\LookaheadFilter::class)]
34#[UsesClass(\SqlParser\PostgreSql\Lexer\NumberScanner::class)]
35#[UsesClass(\SqlParser\PostgreSql\Lexer\OperatorScanner::class)]
36#[UsesClass(\SqlParser\PostgreSql\Lexer\PostgreSqlLexer::class)]
37#[UsesClass(\SqlParser\PostgreSql\Lexer\QuotedScanner::class)]
38#[UsesClass(\SqlParser\PostgreSql\Lexer\Scan::class)]
39#[UsesClass(\SqlParser\PostgreSql\Lexer\TriviaScanner::class)]
40#[UsesClass(\SqlParser\PostgreSql\Lexer\WordScanner::class)]
41#[UsesClass(\SqlParser\Resource\SqlVersion::class)]
42#[UsesClass(\SqlParser\Resource\VersionRegistry::class)]
43#[UsesClass(\SqlParser\Table\ActionCode::class)]
44#[UsesClass(\SqlParser\Table\PackedRows::class)]
45#[UsesClass(\SqlParser\Table\ParseTable::class)]
46#[UsesClass(\SqlParser\Table\TableCodec::class)]
47#[UsesClass(\SqlParser\Table\TableFile::class)]
48#[UsesClass(\SqlParser\Table\TableRule::class)]
49#[UsesClass(\SqlParser\Lexer\SourceException::class)]
50#[Small]
51final class PostgreSqlParserTest extends TestCase
52{
53 public function testVersion(): void
54 {
55 self::assertSame('pg-17.2', (new PostgreSqlParser())->version());
56 }
57
58 public function testVersionRejectsAnUnknownRelease(): void
59 {
60 $this->expectException(RuntimeException::class);
61
62 new PostgreSqlParser('pg-9.6');
63 }
64
65 public function testTokenize(): void
66 {
67 $tokens = (new PostgreSqlParser())->tokenize('SELECT "a" FROM t WHERE b IS NOT NULL AND c NOT LIKE $1');
68
69 self::assertSame(['SELECT', 'IDENT', 'FROM', 'IDENT', 'WHERE', 'IDENT', 'IS', 'NOT', 'NULL_P', 'AND', 'IDENT', 'NOT_LA', 'LIKE', 'PARAM', '$end'], array_map(static fn (Token $token): string => $token->name, $tokens));
70 }
71
72 public function testTokenizeRejectsAnUnterminatedString(): void
73 {
74 $this->expectException(LexicalException::class);
75
76 (new PostgreSqlParser())->tokenize("SELECT 'abc");
77 }
78
79 public function testParseReadsNamedParametersUnderTheNamedSyntax(): void
80 {
81 $sql = "UPDATE users SET name = :name, tags = tags ? 'x' WHERE id = :id AND created < \$1::date";
82
83 self::assertSame($sql, (new PostgreSqlParser(parameters: ParameterSyntax::Named))->parse($sql)->toString());
84 self::assertSame(3, count(array_filter((new PostgreSqlParser(parameters: ParameterSyntax::Named))->tokenize($sql), static fn (Token $token): bool => $token->name === 'PARAM')));
85
86 $this->expectException(SyntaxException::class);
87
88 (new PostgreSqlParser())->parse($sql);
89 }
90
91 public function testParse(): void
92 {
93 $sql = 'SELECT id FROM users WHERE id = $1; INSERT INTO t (a) VALUES (1) RETURNING a';
94 $tree = (new PostgreSqlParser())->parse($sql);
95
96 self::assertSame('parse_toplevel', $tree->name);
97 self::assertCount(2, array_filter($tree->find('stmt'), static fn ($node): bool => !$node->isEmpty()));
98 self::assertSame('SELECT id FROM users WHERE id = $1', $tree->find('SelectStmt')[0]->text($sql));
99 }
100
101 public function testParseWritesTheTextBackWithItsCommentsAndWhitespace(): void
102 {
103 $sql = " SELECT /* one */ a NOT IN (1,2)\nFROM t ; -- done\n";
104 $unicode = "SELECT U&'d\\0061t' UESCAPE '!' FROM t";
105
106 self::assertSame($sql, (new PostgreSqlParser())->parse($sql)->toString());
107 self::assertSame($unicode, (new PostgreSqlParser())->parse($unicode)->toString());
108 self::assertSame("-- nothing at all\n/* either */", (new PostgreSqlParser())->parse("-- nothing at all\n/* either */")->toString());
109 }
110
111 public function testParseAcceptsAnEmptyInput(): void
112 {
113 self::assertSame('parse_toplevel', (new PostgreSqlParser())->parse('')->name);
114 }
115
116 public function testParseRejectsAChainedComparison(): void
117 {
118 $this->expectException(SyntaxException::class);
119
120 (new PostgreSqlParser())->parse('SELECT 1 = 2 = 3');
121 }
122
123 public function testVersions(): void
124 {
125 self::assertSame(['pg-16.6', 'pg-17.2'], PostgreSqlParser::versions());
126 }
127}
128