Skip to content

Commit 0283884

Browse files
committed
Parse queries from "wp_get_db_schema()"
1 parent cde7da8 commit 0283884

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ 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 );
658+
$ast = $this->parse_query( $query )->current();
659659

660660
// Handle transaction commands.
661661

@@ -728,23 +728,24 @@ public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mo
728728
}
729729

730730
/**
731-
* Parse a MySQL query into an AST.
731+
* Parse a MySQL query into an array of ASTs.
732732
*
733-
* @param string $query The MySQL query to parse.
734-
* @return WP_Parser_Node The AST representing the parsed query.
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.
735735
* @throws WP_SQLite_Driver_Exception When the query parsing fails.
736736
*/
737-
public function parse_query( string $query ): WP_Parser_Node {
737+
public function parse_query( string $query ): Generator {
738738
$lexer = new WP_MySQL_Lexer( $query );
739739
$tokens = $lexer->remaining_tokens();
740740

741741
$parser = new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
742-
$ast = $parser->parse();
743-
744-
if ( null === $ast ) {
745-
throw $this->new_driver_exception( 'Failed to parse the MySQL query.' );
742+
while ( $parser->next_ast() ) {
743+
$ast = $parser->get_ast();
744+
if ( null === $ast ) {
745+
throw $this->new_driver_exception( 'Failed to parse the MySQL query.' );
746+
}
747+
yield $ast;
746748
}
747-
return $ast;
748749
}
749750

750751
/**

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ 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-
$parts = preg_split( '/(CREATE\s+TABLE)/', $schema, -1, PREG_SPLIT_NO_EMPTY );
68-
foreach ( $parts as $part ) {
69-
$name = $this->unquote_mysql_identifier(
70-
preg_split( '/\s+/', $part, 2, PREG_SPLIT_NO_EMPTY )[0]
71-
);
72-
$wp_tables[ $name ] = 'CREATE TABLE' . $part;
67+
foreach ( $this->driver->parse_query( $schema ) as $query ) {
68+
$create_node = $query->get_first_descendant_node( 'createStatement' );
69+
if ( $create_node && $create_node->has_child_node( 'createTable' ) ) {
70+
$name_node = $create_node->get_first_descendant_node( 'tableName' );
71+
$name = $this->unquote_mysql_identifier(
72+
substr( $schema, $name_node->get_start(), $name_node->get_length() )
73+
);
74+
75+
$wp_tables[ $name ] = $create_node;
76+
}
7377
}
7478
}
7579

@@ -78,12 +82,12 @@ public function ensure_correct_information_schema(): void {
7882
if ( ! in_array( $table, $information_schema_tables, true ) ) {
7983
if ( isset( $wp_tables[ $table ] ) ) {
8084
// WordPress core table (as returned by "wp_get_db_schema()").
81-
$sql = $wp_tables[ $table ];
85+
$ast = $wp_tables[ $table ];
8286
} else {
8387
// Other table (a WordPress plugin or unrelated to WordPress).
8488
$sql = $this->generate_create_table_statement( $table );
89+
$ast = $this->driver->parse_query( $sql )->current();
8590
}
86-
$ast = $this->driver->parse_query( $sql );
8791
$this->information_schema_builder->record_create_table( $ast );
8892
}
8993
}
@@ -92,7 +96,7 @@ public function ensure_correct_information_schema(): void {
9296
foreach ( $information_schema_tables as $table ) {
9397
if ( ! in_array( $table, $tables, true ) ) {
9498
$sql = sprintf( 'DROP %s', $this->quote_sqlite_identifier( $table ) );
95-
$ast = $this->driver->parse_query( $sql );
99+
$ast = $this->driver->parse_query( $sql )->current();
96100
$this->information_schema_builder->record_drop_table( $ast );
97101
}
98102
}

0 commit comments

Comments
 (0)