GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sql-parser/src/Statement.php at master · phpmyadmin/sql-parser Expand file tree/ Copy pathStatement.phpMore file actions 497 lines (431 loc) · 16.6 KB / Copy pathStatement.phpFile metadata and controls497 lines (431 loc) · 16.6 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 <?php declare(strict_types=1); namespace PhpMyAdmin\SqlParser; use AllowDynamicProperties; use JsonSerializable; use PhpMyAdmin\SqlParser\Components\OptionsArray; use PhpMyAdmin\SqlParser\Exceptions\ParserException; use PhpMyAdmin\SqlParser\Parsers\OptionsArrays; use PhpMyAdmin\SqlParser\Statements\SelectStatement; use PhpMyAdmin\SqlParser\Statements\SetStatement; use PhpMyAdmin\SqlParser\Utils\Query; use Stringable; use function array_flip; use function array_key_exists; use function array_keys; use function array_merge; use function array_push; use function get_mangled_object_vars; use function is_array; use function is_string; use function str_contains; use function strtoupper; use function trim; /** * The result of the parser is an array of statements are extensions of the class defined here. * * A statement represents the result of parsing the lexemes. * * Abstract statement definition. */ #[AllowDynamicProperties] abstract class Statement implements Stringable, JsonSerializable { /** * Options for this statement. * * The option would be the key and the value can be an integer or an array. * * The integer represents only the index used. * * The array may have two keys: `0` is used to represent the index used and * `1` is the type of the option (which may be 'var' or 'var='). Both * options mean they expect a value after the option (e.g. `A = B` or `A B`, * in which case `A` is the key and `B` is the value). The only difference * is in the building process. `var` options are built as `A B` and `var=` * options are built as `A = B` * * Two options that can be used together must have different values for * indexes, else, when they will be used together, an error will occur. * * @var array<string, int|array<int, int|string>> * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})> */ public static array $statementOptions = []; protected const ADD_CLAUSE = 1; protected const ADD_KEYWORD = 2; /** * The clauses of this statement, in order. * * @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}> */ public static array $clauses = []; /** * The options of this query. * * @see Statement::$statementOptions */ public OptionsArray|null $options = null; /** * The index of the first token used in this statement. */ public int|null $first = null; /** * The index of the last token used in this statement. */ public int|null $last = null; /** * @param Parser|null $parser the instance that requests parsing * @param TokensList|null $list the list of tokens to be parsed * * @throws ParserException */ public function __construct(Parser|null $parser = null, TokensList|null $list = null) { if (($parser === null) || ($list === null)) { return; } $this->parse($parser, $list); } /** * Builds the string representation of this statement. */ public function build(): string { /** * Query to be returned. */ $query = ''; /** * Clauses which were built already. * * It is required to keep track of built clauses because some fields, * for example `join` is used by multiple clauses (`JOIN`, `LEFT JOIN`, * `LEFT OUTER JOIN`, etc.). The same happens for `VALUE` and `VALUES`. * * A clause is considered built just after fields' value * (`$this->field`) was used in building. */ $built = []; foreach ($this->getClauses() as [$name, $type]) { /** * The name of the field that is used as source for the builder. * Same field is used to store the result of parsing. */ $field = Parser::KEYWORD_PARSERS[$name]['field']; // The field is empty, there is nothing to be built. if (empty($this->$field)) { continue; } // Checking if this field was already built. if ($type & self::ADD_CLAUSE) { if (! empty($built[$field])) { continue; } $built[$field] = true; } // Checking if the name of the clause should be added. if ($type & self::ADD_KEYWORD) { $query = trim($query) . ' ' . $name; } // Checking if the result of the builder should be added. if (! ($type & self::ADD_CLAUSE)) { continue; } if (is_array($this->$field)) { $class = Parser::KEYWORD_PARSERS[$name]['class']; $query = trim($query) . ' ' . $class::buildAll($this->$field); } else { $query = trim($query) . ' ' . $this->$field->build(); } } return $query; } /** * Parses the statements defined by the tokens list. * * @param Parser $parser the instance that requests parsing * @param TokensList $list the list of tokens to be parsed * * @throws ParserException */ public function parse(Parser $parser, TokensList $list): void { /** * Array containing all list of clauses parsed. * This is used to check for duplicates. */ $parsedClauses = []; // This may be corrected by the parser. $this->first = $list->idx; /** * Whether options were parsed or not. * For statements that do not have any options this is set to `true` by * default. */ $parsedOptions = static::$statementOptions === []; 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) { break; } // Checking if this closing bracket is the pair for a bracket // outside the statement. if (($token->value === ')') && ($parser->brackets > 0)) { --$parser->brackets; continue; } // Only keywords are relevant here. Other parts of the query are // processed in the functions below. if ($token->type !== TokenType::Keyword) { if (($token->type !== TokenType::Comment) && ($token->type !== TokenType::Whitespace)) { $parser->error('Unexpected token.', $token); } continue; } // Unions are parsed by the parser because they represent more than // one statement. if ( ($token->keyword === 'UNION') || ($token->keyword === 'UNION ALL') || ($token->keyword === 'UNION DISTINCT') || ($token->keyword === 'EXCEPT') || ($token->keyword === 'INTERSECT') ) { break; } /** * The name of the class that is used for parsing. */ $class = null; /** * The name of the field where the result of the parsing is stored. */ $field = null; /** * Parser's options. */ $options = []; // Looking for duplicated clauses. if ( is_string($token->value) && ( isset(Parser::KEYWORD_PARSERS[$token->value]) || ( isset(Parser::STATEMENT_PARSERS[$token->value]) && Parser::STATEMENT_PARSERS[$token->value] !== '' ) ) ) { if (array_key_exists($token->value, $parsedClauses)) { $parser->error('This type of clause was previously parsed.', $token); break; } $parsedClauses[$token->value] = true; } // Checking if this is the beginning of a clause. // Fix Issue #221: As `truncate` is not a keyword, // but it might be the beginning of a statement of truncate, // so let the value use the keyword field for truncate type. $tokenValue = $token->keyword === 'TRUNCATE' ? $token->keyword : $token->value; if (is_string($tokenValue) && isset(Parser::KEYWORD_PARSERS[$tokenValue]) && $list->idx < $list->count) { $class = Parser::KEYWORD_PARSERS[$tokenValue]['class']; $field = Parser::KEYWORD_PARSERS[$tokenValue]['field']; if (isset(Parser::KEYWORD_PARSERS[$tokenValue]['options'])) { $options = Parser::KEYWORD_PARSERS[$tokenValue]['options']; } } // Checking if this is the beginning of the statement. if ( isset(Parser::STATEMENT_PARSERS[$token->keyword]) && Parser::STATEMENT_PARSERS[$token->keyword] !== '' ) { if (static::$clauses !== [] && is_string($token->value) && ! isset(static::$clauses[$token->value])) { // Some keywords (e.g. `SET`) may be the beginning of a // statement and a clause. // If such keyword was found, and it cannot be a clause of // this statement it means it is a new statement, but no // delimiter was found between them. $parser->error( 'A new statement was found, but no delimiter between it and the previous one.', $token, ); break; } if (! $parsedOptions) { if (! array_key_exists((string) $token->value, static::$statementOptions)) { // Skipping keyword because if it is not a option. ++$list->idx; } $this->options = OptionsArrays::parse($parser, $list, static::$statementOptions); $parsedOptions = true; } } elseif ($class === null) { if (! ($this instanceof SetStatement) || ($token->value !== 'COLLATE' && $token->value !== 'DEFAULT')) { // There is no parser for this keyword and isn't the beginning // of a statement (so no options) either. $parser->error('Unrecognized keyword.', $token); continue; } // Handle special end options in SET statement $this->endOptions = OptionsArrays::parse($parser, $list, SetStatement::STATEMENT_END_OPTIONS); } $this->before($parser, $list, $token); // Parsing this keyword. if ($class !== null) { // We can't parse keyword at the end of statement if ($list->idx >= $list->count) { $parser->error('Keyword at end of statement.', $token); continue; } ++$list->idx; // Skipping keyword or last option. $this->$field = $class::parse($parser, $list, $options); } $this->after($parser, $list, $token); } // This may be corrected by the parser. $this->last = --$list->idx; // Go back to last used token. } /** * Function called before the token is processed. * * @param Parser $parser the instance that requests parsing * @param TokensList $list the list of tokens to be parsed * @param Token $token the token that is being parsed */ public function before(Parser $parser, TokensList $list, Token $token): void { } /** * Function called after the token was processed. * * @param Parser $parser the instance that requests parsing * @param TokensList $list the list of tokens to be parsed * @param Token $token the token that is being parsed */ public function after(Parser $parser, TokensList $list, Token $token): void { } /** * Gets the clauses of this statement. * * @return array<string, array{non-empty-string, int-mask-of<Statement::ADD_*>}> */ public function getClauses(): array { return static::$clauses; } /** * Gets the clause order of this statement as an array * with clause as key and index as value. * * @return array<string, int> */ public function getClauseOrder(): array { $clauses = []; foreach (array_keys($this->getClauses()) as $key) { if ($key === '_END_OPTIONS') { // phpcs:ignore SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants if (static::STATEMENT_END_OPTIONS !== []) { // phpcs:ignore SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants array_push($clauses, ...array_keys(static::STATEMENT_END_OPTIONS)); } } else { $clauses[] = $key; } } return array_flip($clauses); } /** * Builds the string representation of this statement. * * @see static::build */ public function __toString(): string { return $this->build(); } /** * Validates the order of the clauses in parsed statement * Ideally this should be called after successfully * completing the parsing of each statement. * * @param Parser $parser the instance that requests parsing * @param TokensList $list the list of tokens to be parsed * * @throws ParserException */ public function validateClauseOrder(Parser $parser, TokensList $list): bool { $clauses = array_flip(array_keys($this->getClauses())); if ($clauses === []) { return true; } $minIdx = -1; /** * For tracking JOIN clauses in a query * = 0 - JOIN not found till now * > 0 - Index of first JOIN clause in the statement. */ $minJoin = 0; /** * For tracking JOIN clauses in a query * = 0 - JOIN not found till now * > 0 - Index of last JOIN clause * (which appears together with other JOINs) * in the statement. */ $maxJoin = 0; $error = 0; $lastIdx = 0; foreach (array_keys($clauses) as $clauseType) { $clauseStartIdx = Query::getClauseStartOffset($this, $list, $clauseType); if ( $clauseStartIdx !== -1 && $this instanceof SelectStatement && ($clauseType === 'FORCE' || $clauseType === 'IGNORE' || $clauseType === 'USE') ) { // TODO: ordering of clauses in a SELECT statement with // Index hints is not supported return true; } // Handle ordering of Multiple Joins in a query if ($clauseStartIdx !== -1) { $containsJoinClause = str_contains(strtoupper($clauseType), 'JOIN'); if ($minJoin === 0 && $containsJoinClause) { // First JOIN clause is detected $minJoin = $maxJoin = $clauseStartIdx; } elseif ($minJoin !== 0 && ! $containsJoinClause) { // After a previous JOIN clause, a non-JOIN clause has been detected $maxJoin = $lastIdx; } elseif ($maxJoin < $clauseStartIdx && $containsJoinClause) { $error = 1; } } if ($clauseStartIdx !== -1 && $clauseStartIdx < $minIdx) { if ($minJoin === 0 || $error === 1) { $token = $list->tokens[$clauseStartIdx]; $parser->error('Unexpected ordering of clauses.', $token); return false; } $minIdx = $clauseStartIdx; } elseif ($clauseStartIdx !== -1) { $minIdx = $clauseStartIdx; } $lastIdx = $clauseStartIdx !== -1 ? $clauseStartIdx : $lastIdx; } return true; } /** @return array<mixed> */ public function jsonSerialize(): array { return array_merge(['@type' => static::class], get_mangled_object_vars($this)); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |