|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Statements; |
|
6
|
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\OptionsArray; |
|
8
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
|
9
|
|
|
use PhpMyAdmin\SqlParser\Statement; |
|
10
|
|
|
use PhpMyAdmin\SqlParser\Token; |
|
11
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
|
12
|
|
|
|
|
13
|
|
|
use function array_slice; |
|
14
|
|
|
use function count; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* `EXPLAIN` statement. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ExplainStatement extends Statement |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Options for `EXPLAIN` statements. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array<string, int|array<int, int|string>> |
|
25
|
|
|
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})> |
|
26
|
|
|
*/ |
|
27
|
|
|
public static $OPTIONS = [ |
|
28
|
|
|
|
|
29
|
|
|
'EXTENDED' => 1, |
|
30
|
|
|
'PARTITIONS' => 1, |
|
31
|
|
|
'FORMAT' => [ |
|
32
|
|
|
1, |
|
33
|
|
|
'var', |
|
34
|
|
|
], |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The parser of the statement to be explained |
|
39
|
|
|
* |
|
40
|
|
|
* @var Parser|null |
|
41
|
|
|
*/ |
|
42
|
|
|
public $bodyParser = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The statement alias, could be any of the following: |
|
46
|
|
|
* - EXPLAIN/DESC/DESCRIBE |
|
47
|
|
|
* - EXPLAIN/DESC/DESCRIBE ANALYZE |
|
48
|
|
|
* - ANALYZE |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
public $statemenetAlias; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The connection identifier, if used. |
|
56
|
|
|
* |
|
57
|
|
|
* @var number|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public $connectionId = null; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The explained table's name, if used. |
|
63
|
|
|
* |
|
64
|
|
|
* @var string|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public $explainedTable = null; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Parser $parser the instance that requests parsing |
|
70
|
|
|
* @param TokensList $list the list of tokens to be parsed |
|
71
|
|
|
*/ |
|
72
|
44 |
|
public function parse(Parser $parser, TokensList $list) |
|
73
|
|
|
{ |
|
74
|
|
|
/** |
|
75
|
|
|
* The state of the parser. |
|
76
|
|
|
* |
|
77
|
|
|
* Below are the states of the parser. |
|
78
|
|
|
* |
|
79
|
|
|
* 0 -------------------[ EXPLAIN/EXPLAIN ANALYZE/ANALYZE ]-----------------------> 1 |
|
80
|
|
|
* |
|
81
|
|
|
* 1 ------------------------------[ OPTIONS ]------------------------------------> 2 |
|
82
|
|
|
* |
|
83
|
|
|
* 2 --------------[ tablename / STATEMENT / FOR CONNECTION ]---------------------> 2 |
|
84
|
|
|
* |
|
85
|
|
|
* @var int |
|
86
|
|
|
*/ |
|
87
|
44 |
|
$state = 0; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* To Differentiate between ANALYZE / EXPLAIN / EXPLAIN ANALYZE |
|
91
|
|
|
* 0 -> ANALYZE ( used by mariaDB https://mariadb.com/kb/en/analyze-statement) |
|
92
|
|
|
* 1 -> EXPLAIN / DESC / DESCRIBE |
|
93
|
|
|
* 2 -> EXPLAIN / DESC / DESCRIBE [ANALYZE] |
|
94
|
|
|
*/ |
|
95
|
44 |
|
$miniState = 0; |
|
96
|
|
|
|
|
97
|
44 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
|
98
|
|
|
/** |
|
99
|
|
|
* Token parsed at this moment. |
|
100
|
|
|
*/ |
|
101
|
44 |
|
$token = $list->tokens[$list->idx]; |
|
102
|
|
|
|
|
103
|
|
|
// Skipping whitespaces and comments. |
|
104
|
44 |
|
if ($token->type === Token::TYPE_WHITESPACE || $token->type === Token::TYPE_COMMENT) { |
|
105
|
36 |
|
continue; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
44 |
|
if ($state === 0) { |
|
109
|
44 |
|
if ($token->keyword === 'ANALYZE' && $miniState === 0) { |
|
110
|
12 |
|
$state = 1; |
|
111
|
12 |
|
$this->statemenetAlias = 'ANALYZE'; |
|
112
|
|
|
} elseif ( |
|
113
|
36 |
|
$token->keyword === 'EXPLAIN' |
|
114
|
12 |
|
|| $token->keyword === 'DESC' |
|
115
|
36 |
|
|| $token->keyword === 'DESCRIBE' |
|
116
|
|
|
) { |
|
117
|
36 |
|
$miniState === 1; |
|
118
|
36 |
|
$this->statemenetAlias = $token->keyword; |
|
119
|
|
|
|
|
120
|
36 |
|
$lastIdx = $list->idx; |
|
121
|
36 |
|
$nextKeyword = $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'ANALYZE'); |
|
122
|
36 |
|
if ($nextKeyword && $nextKeyword->keyword !== null) { |
|
123
|
12 |
|
$miniState = 2; |
|
124
|
12 |
|
$this->statemenetAlias .= ' ANALYZE'; |
|
125
|
|
|
} else { |
|
126
|
28 |
|
$list->idx = $lastIdx; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
44 |
|
$state = 1; |
|
130
|
|
|
} |
|
131
|
44 |
|
} elseif ($state === 1) { |
|
132
|
|
|
// Parsing options. |
|
133
|
44 |
|
$this->options = OptionsArray::parse($parser, $list, static::$OPTIONS); |
|
134
|
44 |
|
$state = 2; |
|
135
|
44 |
|
} elseif ($state === 2) { |
|
136
|
44 |
|
$currIdx = $list->idx; |
|
137
|
44 |
|
$currToken = $list->getNext(); |
|
|
|
|
|
|
138
|
44 |
|
$nextToken = $list->getNext(); |
|
139
|
44 |
|
$list->idx = $currIdx; |
|
140
|
|
|
|
|
141
|
44 |
|
if ($token->keyword === 'FOR' && $nextToken->keyword === 'CONNECTION') { |
|
142
|
4 |
|
$forToken = $list->getNext(); // FOR |
|
|
|
|
|
|
143
|
4 |
|
$connectionToken = $list->getNext(); // CONNECTION |
|
|
|
|
|
|
144
|
4 |
|
$nextToken = $list->getNext(); // Identifier |
|
145
|
4 |
|
$this->connectionId = $nextToken->value; |
|
146
|
4 |
|
break; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
// To support EXPLAIN tablename |
|
150
|
44 |
|
if ($token->type === Token::TYPE_NONE) { |
|
151
|
16 |
|
$this->explainedTable = $token->value; |
|
152
|
16 |
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ( |
|
156
|
32 |
|
$token->keyword !== 'SELECT' |
|
157
|
32 |
|
&& $token->keyword !== 'INSERT' |
|
158
|
32 |
|
&& $token->keyword !== 'UPDATE' |
|
159
|
32 |
|
&& $token->keyword !== 'DELETE' |
|
160
|
|
|
) { |
|
161
|
12 |
|
$parser->error('Unexpected token.', $token); |
|
162
|
12 |
|
break; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// Index of the last parsed token by default would be the last token in the $list, because we're |
|
166
|
|
|
// assuming that all remaining tokens at state 2, are related to the to-be-explained statement. |
|
167
|
20 |
|
$idxOfLastParsedToken = $list->count - 1; |
|
168
|
20 |
|
$subList = new TokensList(array_slice($list->tokens, $list->idx)); |
|
169
|
|
|
|
|
170
|
20 |
|
$this->bodyParser = new Parser($subList); |
|
171
|
20 |
|
if (count($this->bodyParser->errors)) { |
|
172
|
4 |
|
foreach ($this->bodyParser->errors as $error) { |
|
173
|
4 |
|
$parser->errors[] = $error; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
4 |
|
break; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
16 |
|
$list->idx = $idxOfLastParsedToken; |
|
180
|
16 |
|
break; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
22 |
|
} |
|
184
|
|
|
|
|
185
|
4 |
|
public function build(): string |
|
186
|
|
|
{ |
|
187
|
4 |
|
$str = $this->statemenetAlias . ' '; |
|
188
|
|
|
|
|
189
|
4 |
|
$str .= OptionsArray::build($this->options); |
|
190
|
|
|
|
|
191
|
4 |
|
if ($this->bodyParser) { |
|
192
|
4 |
|
foreach ($this->bodyParser->statements as $statement) { |
|
193
|
4 |
|
$str .= $statement->build(); |
|
194
|
|
|
} |
|
195
|
4 |
|
} elseif ($this->connectionId) { |
|
196
|
4 |
|
$str .= 'FOR CONNECTION '; |
|
197
|
4 |
|
$str .= $this->connectionId; |
|
198
|
4 |
|
} elseif ($this->explainedTable) { |
|
199
|
4 |
|
$str .= $this->explainedTable; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
4 |
|
return $str; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|