-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathExplainStatement.php
More file actions
268 lines (226 loc) · 8.78 KB
/
ExplainStatement.php
File metadata and controls
268 lines (226 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Statements;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Parsers\OptionsArrays;
use PhpMyAdmin\SqlParser\Statement;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
use function array_slice;
/**
* `EXPLAIN` statement.
*/
class ExplainStatement extends Statement
{
/**
* Options for `EXPLAIN` statements.
*/
private const OPTIONS = [
'EXTENDED' => 1,
'PARTITIONS' => 1,
'FORMAT' => [
1,
'var',
],
];
/**
* The parser of the statement to be explained
*/
public Parser|null $bodyParser = null;
/**
* The statement alias, could be any of the following:
* - {EXPLAIN | DESCRIBE | DESC}
* - {EXPLAIN | DESCRIBE | DESC} ANALYZE
* - ANALYZE
*/
public string $statementAlias;
/**
* The connection identifier, if used.
*/
public int|null $connectionId = null;
/**
* The explained database for the table's name, if used.
*/
public string|null $explainedDatabase = null;
/**
* The explained table's name, if used.
*/
public string|null $explainedTable = null;
/**
* The explained column's name, if used.
*/
public string|null $explainedColumn = null;
/**
* @param Parser $parser the instance that requests parsing
* @param TokensList $list the list of tokens to be parsed
*/
public function parse(Parser $parser, TokensList $list): void
{
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 -------------------[ EXPLAIN/EXPLAIN ANALYZE/ANALYZE ]-----------------------> 1
*
* 0 ------------------------[ EXPLAIN/DESC/DESCRIBE ]----------------------------> 3
*
* 1 ------------------------------[ OPTIONS ]------------------------------------> 2
*
* 2 --------------[ tablename / STATEMENT / FOR CONNECTION ]---------------------> 2
*
* 3 -----------------------------[ tablename ]-----------------------------------> 3
*/
$state = 0;
/**
* To Differentiate between ANALYZE / EXPLAIN / EXPLAIN ANALYZE
* 0 -> ANALYZE ( used by mariaDB https://mariadb.com/kb/en/analyze-statement)
* 1 -> {EXPLAIN | DESCRIBE | DESC}
* 2 -> {EXPLAIN | DESCRIBE | DESC} ANALYZE
*/
$miniState = 0;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === TokenType::Delimiter) {
--$list->idx; // Back up one token, no real reasons to document
break;
}
// Skipping whitespaces and comments.
if ($token->type === TokenType::Whitespace || $token->type === TokenType::Comment) {
continue;
}
if ($state === 0) {
if ($token->keyword === 'ANALYZE' && $miniState === 0) {
$state = 1;
$this->statementAlias = 'ANALYZE';
} elseif (
$token->keyword === 'EXPLAIN'
|| $token->keyword === 'DESC'
|| $token->keyword === 'DESCRIBE'
) {
$this->statementAlias = $token->keyword;
$lastIdx = $list->idx;
$list->idx++; // Ignore the current token
$nextKeyword = $list->getNextOfType(TokenType::Keyword);
$list->idx = $lastIdx;
// There is no other keyword, we must be describing a table
if ($nextKeyword === null) {
$state = 3;
continue;
}
$miniState = 1;
$lastIdx = $list->idx;
$nextKeyword = $list->getNextOfTypeAndValue(TokenType::Keyword, 'ANALYZE');
if ($nextKeyword && $nextKeyword->keyword !== null) {
$miniState = 2;
$this->statementAlias .= ' ANALYZE';
} else {
$list->idx = $lastIdx;
}
$state = 1;
}
} elseif ($state === 1) {
// Parsing options.
$this->options = OptionsArrays::parse($parser, $list, self::OPTIONS);
$state = 2;
} elseif ($state === 2) {
$currIdx = $list->idx;
$list->idx++; // Ignore the current token
$nextToken = $list->getNext();
$list->idx = $currIdx;
if ($token->keyword === 'FOR' && $nextToken->keyword === 'CONNECTION') {
$list->idx++; // Ignore the current token
$list->getNext(); // CONNECTION
$nextToken = $list->getNext(); // Identifier
$this->connectionId = $nextToken->value;
break;
}
if (
$token->keyword !== 'SELECT'
&& $token->keyword !== 'TABLE'
&& $token->keyword !== 'INSERT'
&& $token->keyword !== 'REPLACE'
&& $token->keyword !== 'UPDATE'
&& $token->keyword !== 'DELETE'
) {
$parser->error('Unexpected token.', $token);
break;
}
// Index of the last parsed token by default would be the last token in the $list, because we're
// assuming that all remaining tokens at state 2, are related to the to-be-explained statement.
$idxOfLastParsedToken = $list->count - 1;
$subList = new TokensList(array_slice($list->tokens, $list->idx));
$this->bodyParser = new Parser($subList);
if ($this->bodyParser->errors !== []) {
foreach ($this->bodyParser->errors as $error) {
$parser->errors[] = $error;
}
break;
}
$list->idx = $idxOfLastParsedToken;
break;
} elseif ($state === 3) {
if (($token->type === TokenType::Operator) && ($token->value === '.')) {
continue;
}
if ($this->explainedDatabase === null) {
$lastIdx = $list->idx;
$nextDot = $list->getNextOfTypeAndValue(TokenType::Operator, '.');
$list->idx = $lastIdx;
if ($nextDot !== null) {// We found a dot, so it must be a db.table name format
$this->explainedDatabase = $token->value;
continue;
}
}
if ($this->explainedTable === null) {
$this->explainedTable = $token->value;
continue;
}
if ($this->explainedColumn === null && $token->value !== null) {
$this->explainedColumn = (string) $token->value;
}
}
}
if ($state !== 3 || $this->explainedTable !== null) {
return;
}
// We reached end of the state 3 and no table name was found
/** Token parsed at this moment. */
$token = $list->tokens[$list->idx];
$parser->error('Expected a table name.', $token);
}
public function build(): string
{
$str = $this->statementAlias;
if ($this->options !== null) {
if ($this->options->options !== []) {
$str .= ' ';
}
$str .= $this->options->build() . ' ';
}
if ($this->options === null) {
$str .= ' ';
}
if ($this->bodyParser) {
foreach ($this->bodyParser->statements as $statement) {
$str .= $statement->build();
}
} elseif ($this->connectionId) {
$str .= 'FOR CONNECTION ' . $this->connectionId;
}
if ($this->explainedDatabase !== null && $this->explainedTable !== null) {
$str .= Context::escape($this->explainedDatabase) . '.' . Context::escape($this->explainedTable);
} elseif ($this->explainedTable !== null) {
$str .= Context::escape($this->explainedTable);
}
if ($this->explainedColumn !== null) {
$str .= ' ' . Context::escape($this->explainedColumn);
}
return $str;
}
}