Skip to content

Commit 6a3b370

Browse files
committed
Return parser instance rather than a generator
1 parent 30c8212 commit 6a3b370

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

+12-15
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,13 @@ public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mo
655655

656656
try {
657657
// Parse the MySQL query.
658-
$ast = $this->parse_query( $query )->current();
658+
// TODO: Translate and execute all queries in the SQL input string.
659+
$parser = $this->create_parser( $query );
660+
$parser->next_query();
661+
$ast = $parser->get_query_ast();
662+
if ( null === $ast ) {
663+
throw $this->new_driver_exception( 'Failed to parse the MySQL query.' );
664+
}
659665

660666
// Handle transaction commands.
661667

@@ -728,24 +734,15 @@ public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mo
728734
}
729735

730736
/**
731-
* Parse a MySQL query into an array of ASTs.
737+
* Tokenize a MySQL query and initialize a parser.
732738
*
733-
* @param string $query The MySQL query to parse.
734-
* @return Generator<WP_Parser_Node> A generator of ASTs representing the queries parsed from the input.
735-
* @throws WP_SQLite_Driver_Exception When the query parsing fails.
739+
* @param string $query The MySQL query to parse.
740+
* @return WP_MySQL_Parser A parser initialized for the MySQL query.
736741
*/
737-
public function parse_query( string $query ): Generator {
742+
public function create_parser( string $query ): WP_MySQL_Parser {
738743
$lexer = new WP_MySQL_Lexer( $query );
739744
$tokens = $lexer->remaining_tokens();
740-
741-
$parser = new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
742-
while ( $parser->next_query() ) {
743-
$ast = $parser->get_query_ast();
744-
if ( null === $ast ) {
745-
throw $this->new_driver_exception( 'Failed to parse the MySQL query.' );
746-
}
747-
yield $ast;
748-
}
745+
return new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
749746
}
750747

751748
/**

wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ public function ensure_correct_information_schema(): void {
6464
throw new Exception( 'The "wp_get_db_schema()" function was not defined.' );
6565
}
6666
$schema = wp_get_db_schema();
67-
foreach ( $this->driver->parse_query( $schema ) as $query ) {
68-
$create_node = $query->get_first_descendant_node( 'createStatement' );
67+
$parser = $this->driver->create_parser( $schema );
68+
while ( $parser->next_query() ) {
69+
$ast = $parser->get_query_ast();
70+
if ( null === $ast ) {
71+
throw new WP_SQLite_Driver_Exception( $this->driver, 'Failed to parse the MySQL query.' );
72+
}
73+
74+
$create_node = $ast->get_first_descendant_node( 'createStatement' );
6975
if ( $create_node && $create_node->has_child_node( 'createTable' ) ) {
7076
$name_node = $create_node->get_first_descendant_node( 'tableName' );
7177
$name = $this->unquote_mysql_identifier(
@@ -86,7 +92,10 @@ public function ensure_correct_information_schema(): void {
8692
} else {
8793
// Other table (a WordPress plugin or unrelated to WordPress).
8894
$sql = $this->generate_create_table_statement( $table );
89-
$ast = $this->driver->parse_query( $sql )->current();
95+
$ast = $this->driver->create_parser( $sql )->parse();
96+
if ( null === $ast ) {
97+
throw new WP_SQLite_Driver_Exception( $this->driver, 'Failed to parse the MySQL query.' );
98+
}
9099
}
91100
$this->information_schema_builder->record_create_table( $ast );
92101
}
@@ -96,7 +105,10 @@ public function ensure_correct_information_schema(): void {
96105
foreach ( $information_schema_tables as $table ) {
97106
if ( ! in_array( $table, $tables, true ) ) {
98107
$sql = sprintf( 'DROP %s', $this->quote_sqlite_identifier( $table ) );
99-
$ast = $this->driver->parse_query( $sql )->current();
108+
$ast = $this->driver->create_parser( $sql )->parse();
109+
if ( null === $ast ) {
110+
throw new WP_SQLite_Driver_Exception( $this->driver, 'Failed to parse the MySQL query.' );
111+
}
100112
$this->information_schema_builder->record_drop_table( $ast );
101113
}
102114
}

0 commit comments

Comments
 (0)