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