| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* MySQL parser used by the SQLite driver. |
| 5 |
* |
| 6 |
* @access private |
| 7 |
*/ |
| 8 |
class WP_MySQL_Parser extends WP_Parser { |
| 9 |
/** |
| 10 |
* The current query AST. |
| 11 |
* |
| 12 |
* @var WP_Parser_Node|null |
| 13 |
*/ |
| 14 |
private $current_ast; |
| 15 |
|
| 16 |
/** |
| 17 |
* Reset this parser with a new token stream. |
| 18 |
* |
| 19 |
* @param array<WP_Parser_Token> $tokens The parser tokens. |
| 20 |
*/ |
| 21 |
public function reset_tokens( array $tokens ): void { |
| 22 |
$this->tokens = $tokens; |
| 23 |
$this->position = 0; |
| 24 |
$this->current_ast = null; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Parse the next query from the input SQL string. |
| 29 |
* |
| 30 |
* This method reads tokens until a query is parsed, or the parsing fails. |
| 31 |
* It returns a boolean indicating whether a query was successfully parsed. |
| 32 |
* |
| 33 |
* Example: |
| 34 |
* |
| 35 |
* // Parse all queries in the input SQL string. |
| 36 |
* $parser = new WP_MySQL_Parser( $sql ); |
| 37 |
* while ( $parser->next_query() ) { |
| 38 |
* $ast = $parser->get_query_ast(); |
| 39 |
* if ( ! $ast ) { |
| 40 |
* // The parsing failed. |
| 41 |
* } |
| 42 |
* // The query was successfully parsed. |
| 43 |
* } |
| 44 |
* |
| 45 |
* @return bool Whether a query was successfully parsed. |
| 46 |
*/ |
| 47 |
public function next_query(): bool { |
| 48 |
if ( $this->position >= count( $this->tokens ) ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
$this->current_ast = $this->parse(); |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the current query AST. |
| 57 |
* |
| 58 |
* When no query has been parsed yet, the parsing failed, or the end of the |
| 59 |
* input was reached, this method returns null. |
| 60 |
* |
| 61 |
* @see WP_MySQL_Parser::next_query() for usage example. |
| 62 |
* |
| 63 |
* @return WP_Parser_Node|null The current query AST, or null if no query was parsed. |
| 64 |
*/ |
| 65 |
public function get_query_ast(): ?WP_Parser_Node { |
| 66 |
return $this->current_ast; |
| 67 |
} |
| 68 |
} |
| 69 |
|