1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Tests\Parser; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\WithKeyword; |
8
|
|
|
use PhpMyAdmin\SqlParser\Lexer; |
9
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
10
|
|
|
use PhpMyAdmin\SqlParser\Tests\TestCase; |
11
|
|
|
use stdClass; |
12
|
|
|
|
13
|
|
|
class WithStatementTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param mixed $test |
17
|
|
|
* |
18
|
|
|
* @dataProvider parseWith |
19
|
|
|
*/ |
20
|
|
|
public function testParse($test): void |
21
|
|
|
{ |
22
|
|
|
$this->runParserTest($test); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function parseWith(): array |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
['parser/parseWithStatement'], |
29
|
|
|
['parser/parseWithStatement1'], |
30
|
|
|
['parser/parseWithStatement2'], |
31
|
|
|
['parser/parseWithStatement3'], |
32
|
|
|
['parser/parseWithStatementErr'], |
33
|
|
|
['parser/parseWithStatementErr1'], |
34
|
|
|
['parser/parseWithStatementErr2'], |
35
|
|
|
['parser/parseWithStatementErr3'], |
36
|
|
|
['parser/parseWithStatementErr4'], |
37
|
|
|
['parser/parseWithStatementErr5'], |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testWith(): void |
42
|
|
|
{ |
43
|
|
|
$sql = <<<SQL |
44
|
|
|
WITH categories(identifier, name, parent_id) AS ( |
45
|
|
|
SELECT c.identifier, c.name, c.parent_id FROM category c WHERE c.identifier = 'a' |
46
|
|
|
UNION ALL |
47
|
|
|
SELECT c.identifier, c.name, c.parent_id FROM categories, category c WHERE c.identifier = categories.parent_id |
48
|
|
|
), foo AS ( SELECT * FROM test ) |
49
|
|
|
SELECT * FROM categories |
50
|
|
|
SQL; |
51
|
|
|
|
52
|
|
|
$lexer = new Lexer($sql); |
53
|
|
|
|
54
|
|
|
$lexerErrors = $this->getErrorsAsArray($lexer); |
55
|
|
|
$this->assertCount(0, $lexerErrors); |
56
|
|
|
$parser = new Parser($lexer->list); |
57
|
|
|
$parserErrors = $this->getErrorsAsArray($parser); |
58
|
|
|
$this->assertCount(0, $parserErrors); |
59
|
|
|
$this->assertCount(1, $parser->statements); |
60
|
|
|
|
61
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
62
|
|
|
$expected = <<<SQL |
63
|
|
|
WITH categories(identifier, name, parent_id) AS (SELECT c.identifier, c.name, c.parent_id FROM category AS `c` WHERE c.identifier = 'a' UNION ALL SELECT c.identifier, c.name, c.parent_id FROM categories, category AS `c` WHERE c.identifier = categories.parent_id), foo AS (SELECT * FROM test) |
64
|
|
|
SQL; |
65
|
|
|
// phpcs:enable |
66
|
|
|
$this->assertEquals($expected, $parser->statements[0]->build()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testWithHasErrors(): void |
70
|
|
|
{ |
71
|
|
|
$sql = <<<SQL |
72
|
|
|
WITH categories(identifier, name, parent_id) AS ( |
73
|
|
|
SOMETHING * FROM foo |
74
|
|
|
) |
75
|
|
|
SELECT * FROM categories |
76
|
|
|
SQL; |
77
|
|
|
|
78
|
|
|
$lexer = new Lexer($sql); |
79
|
|
|
|
80
|
|
|
$lexerErrors = $this->getErrorsAsArray($lexer); |
81
|
|
|
$this->assertCount(0, $lexerErrors); |
82
|
|
|
$parser = new Parser($lexer->list); |
83
|
|
|
$parserErrors = $this->getErrorsAsArray($parser); |
84
|
|
|
$this->assertCount(2, $parserErrors); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testWithEmbedParenthesis(): void |
88
|
|
|
{ |
89
|
|
|
$sql = <<<SQL |
90
|
|
|
WITH categories AS ( |
91
|
|
|
SELECT * FROM (SELECT * FROM foo) |
92
|
|
|
) |
93
|
|
|
SELECT * FROM categories |
94
|
|
|
SQL; |
95
|
|
|
|
96
|
|
|
$lexer = new Lexer($sql); |
97
|
|
|
$lexerErrors = $this->getErrorsAsArray($lexer); |
98
|
|
|
$this->assertCount(0, $lexerErrors); |
99
|
|
|
$parser = new Parser($lexer->list); |
100
|
|
|
$parserErrors = $this->getErrorsAsArray($parser); |
101
|
|
|
$this->assertCount(0, $parserErrors); |
102
|
|
|
|
103
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
104
|
|
|
$expected = <<<SQL |
105
|
|
|
WITH categories AS (SELECT * FROM (SELECT * FROM foo)) |
106
|
|
|
SQL; |
107
|
|
|
// phpcs:enable |
108
|
|
|
$this->assertEquals($expected, $parser->statements[0]->build()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testWithHasUnclosedParenthesis(): void |
112
|
|
|
{ |
113
|
|
|
$sql = <<<SQL |
114
|
|
|
WITH categories(identifier, name, parent_id) AS ( |
115
|
|
|
SELECT * FROM (SELECT * FROM foo |
116
|
|
|
) |
117
|
|
|
SELECT * FROM categories |
118
|
|
|
SQL; |
119
|
|
|
|
120
|
|
|
$lexer = new Lexer($sql); |
121
|
|
|
|
122
|
|
|
$lexerErrors = $this->getErrorsAsArray($lexer); |
123
|
|
|
$this->assertCount(0, $lexerErrors); |
124
|
|
|
$parser = new Parser($lexer->list); |
125
|
|
|
$parserErrors = $this->getErrorsAsArray($parser); |
126
|
|
|
$this->assertEquals($parserErrors[0][0], 'A closing bracket was expected.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testBuildWrongWithKeyword(): void |
130
|
|
|
{ |
131
|
|
|
$this->expectExceptionMessage('Can not build a component that is not a WithKeyword'); |
132
|
|
|
WithKeyword::build(new stdClass()); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testBuildBadWithKeyword(): void |
136
|
|
|
{ |
137
|
|
|
$this->expectExceptionMessage('No statement inside WITH'); |
138
|
|
|
WithKeyword::build(new WithKeyword('test')); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|