|
4 | 4 |
|
5 | 5 | namespace PhpMyAdmin\SqlParser\Statements; |
6 | 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 | + |
7 | 16 | /** |
8 | 17 | * `EXPLAIN` statement. |
9 | 18 | */ |
10 | | -class ExplainStatement extends NotImplementedStatement |
| 19 | +class ExplainStatement extends Statement |
11 | 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 | DESCRIBE | DESC} |
| 47 | + * - {EXPLAIN | DESCRIBE | DESC} ANALYZE |
| 48 | + * - ANALYZE |
| 49 | + * |
| 50 | + * @var string |
| 51 | + */ |
| 52 | + public $statemenetAlias; |
| 53 | + |
| 54 | + /** |
| 55 | + * The connection identifier, if used. |
| 56 | + * |
| 57 | + * @var int|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 | + 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 | + $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 | DESCRIBE | DESC} |
| 93 | + * 2 -> {EXPLAIN | DESCRIBE | DESC} ANALYZE |
| 94 | + */ |
| 95 | + $miniState = 0; |
| 96 | + |
| 97 | + for (; $list->idx < $list->count; ++$list->idx) { |
| 98 | + /** |
| 99 | + * Token parsed at this moment. |
| 100 | + */ |
| 101 | + $token = $list->tokens[$list->idx]; |
| 102 | + |
| 103 | + // Skipping whitespaces and comments. |
| 104 | + if ($token->type === Token::TYPE_WHITESPACE || $token->type === Token::TYPE_COMMENT) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + if ($state === 0) { |
| 109 | + if ($token->keyword === 'ANALYZE' && $miniState === 0) { |
| 110 | + $state = 1; |
| 111 | + $this->statemenetAlias = 'ANALYZE'; |
| 112 | + } elseif ( |
| 113 | + $token->keyword === 'EXPLAIN' |
| 114 | + || $token->keyword === 'DESC' |
| 115 | + || $token->keyword === 'DESCRIBE' |
| 116 | + ) { |
| 117 | + $miniState = 1; |
| 118 | + $this->statemenetAlias = $token->keyword; |
| 119 | + |
| 120 | + $lastIdx = $list->idx; |
| 121 | + $nextKeyword = $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'ANALYZE'); |
| 122 | + if ($nextKeyword && $nextKeyword->keyword !== null) { |
| 123 | + $miniState = 2; |
| 124 | + $this->statemenetAlias .= ' ANALYZE'; |
| 125 | + } else { |
| 126 | + $list->idx = $lastIdx; |
| 127 | + } |
| 128 | + |
| 129 | + $state = 1; |
| 130 | + } |
| 131 | + } elseif ($state === 1) { |
| 132 | + // Parsing options. |
| 133 | + $this->options = OptionsArray::parse($parser, $list, static::$OPTIONS); |
| 134 | + $state = 2; |
| 135 | + } elseif ($state === 2) { |
| 136 | + $currIdx = $list->idx; |
| 137 | + $currToken = $list->getNext(); |
| 138 | + $nextToken = $list->getNext(); |
| 139 | + $list->idx = $currIdx; |
| 140 | + |
| 141 | + if ($token->keyword === 'FOR' && $nextToken->keyword === 'CONNECTION') { |
| 142 | + $forToken = $list->getNext(); // FOR |
| 143 | + $connectionToken = $list->getNext(); // CONNECTION |
| 144 | + $nextToken = $list->getNext(); // Identifier |
| 145 | + $this->connectionId = $nextToken->value; |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + // To support EXPLAIN tablename |
| 150 | + if ($token->type === Token::TYPE_NONE) { |
| 151 | + $this->explainedTable = $token->value; |
| 152 | + break; |
| 153 | + } |
| 154 | + |
| 155 | + if ( |
| 156 | + $token->keyword !== 'SELECT' |
| 157 | + && $token->keyword !== 'INSERT' |
| 158 | + && $token->keyword !== 'UPDATE' |
| 159 | + && $token->keyword !== 'DELETE' |
| 160 | + ) { |
| 161 | + $parser->error('Unexpected token.', $token); |
| 162 | + 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 | + $idxOfLastParsedToken = $list->count - 1; |
| 168 | + $subList = new TokensList(array_slice($list->tokens, $list->idx)); |
| 169 | + |
| 170 | + $this->bodyParser = new Parser($subList); |
| 171 | + if (count($this->bodyParser->errors)) { |
| 172 | + foreach ($this->bodyParser->errors as $error) { |
| 173 | + $parser->errors[] = $error; |
| 174 | + } |
| 175 | + |
| 176 | + break; |
| 177 | + } |
| 178 | + |
| 179 | + $list->idx = $idxOfLastParsedToken; |
| 180 | + break; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + public function build(): string |
| 186 | + { |
| 187 | + $str = $this->statemenetAlias; |
| 188 | + |
| 189 | + if (count($this->options->options)) { |
| 190 | + $str .= ' '; |
| 191 | + } |
| 192 | + |
| 193 | + $str .= OptionsArray::build($this->options) . ' '; |
| 194 | + |
| 195 | + if ($this->bodyParser) { |
| 196 | + foreach ($this->bodyParser->statements as $statement) { |
| 197 | + $str .= $statement->build(); |
| 198 | + } |
| 199 | + } elseif ($this->connectionId) { |
| 200 | + $str .= 'FOR CONNECTION ' . $this->connectionId; |
| 201 | + } elseif ($this->explainedTable) { |
| 202 | + $str .= $this->explainedTable; |
| 203 | + } |
| 204 | + |
| 205 | + return $str; |
| 206 | + } |
12 | 207 | } |
0 commit comments