Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/mysql-parser-extension-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ jobs:
exit( 1 );
}
'
./vendor/bin/phpunit -c ./phpunit.xml.dist tests/mysql/WP_MySQL_Lexer_Tests.php tests/parser/WP_Parser_Node_Tests.php
working-directory: packages/mysql-on-sqlite

- name: Run PHPUnit tests with parser extension
run: php -d extension="$GITHUB_WORKSPACE/packages/php-ext-wp-mysql-parser/target/debug/libwp_mysql_parser.so" ./vendor/bin/phpunit -c ./phpunit.xml.dist
working-directory: packages/mysql-on-sqlite

sqlite-driver-extension-tests:
Expand Down Expand Up @@ -149,3 +152,7 @@ jobs:
exit( 1 );
}
'

- name: Run PHPUnit tests with SQLite driver using parser extension
run: php -d extension="$GITHUB_WORKSPACE/packages/php-ext-wp-mysql-parser/target/debug/libwp_mysql_parser.so" ./vendor/bin/phpunit -c ./phpunit.xml.dist
working-directory: packages/mysql-on-sqlite
4 changes: 2 additions & 2 deletions .github/workflows/wp-tests-phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
- name: Build and load parser extension in WordPress PHP containers
run: bash .github/workflows/wp-tests-phpunit-native-extension-setup.sh

- name: Verify WordPress uses parser extension
run: cd wordpress && node tools/local-env/scripts/docker.js run --rm php php /var/www/native-verify-extension.php
- name: Run WordPress PHPUnit tests with parser extension
run: node .github/workflows/wp-tests-phpunit-run.js

- name: Stop Docker containers
if: always()
Expand Down
11 changes: 11 additions & 0 deletions packages/mysql-on-sqlite/src/mysql/class-wp-mysql-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ class WP_MySQL_Parser extends WP_Parser {
*/
private $current_ast;

/**
* Reset this parser with a new token stream.
*
* @param array<WP_Parser_Token> $tokens The parser tokens.
*/
public function reset_tokens( array $tokens ): void {
$this->tokens = $tokens;
$this->position = 0;
$this->current_ast = null;
}

/**
* Parse the next query from the input SQL string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ class WP_PDO_MySQL_On_SQLite extends PDO {
*/
private static $mysql_grammar;

/**
* A reusable parser instance for MySQL queries.
*
* @var WP_MySQL_Parser|null
*/
private $mysql_parser = null;

/**
* The main database name.
*
Expand Down Expand Up @@ -1160,11 +1167,27 @@ public function create_parser( string $query ): WP_MySQL_Parser {
);
if ( $lexer instanceof WP_MySQL_Native_Lexer ) {
$tokens = $lexer->native_token_stream();
return new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
return $this->reset_or_create_parser( $tokens );
}

$tokens = $lexer->remaining_tokens();
return new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
return $this->reset_or_create_parser( $tokens );
}

/**
* Reset the reusable parser with new tokens or create it on first use.
*
* @param array<WP_Parser_Token>|object $tokens Parser tokens.
* @return WP_MySQL_Parser A parser initialized for the token stream.
*/
private function reset_or_create_parser( $tokens ): WP_MySQL_Parser {
if ( null === $this->mysql_parser || ! method_exists( $this->mysql_parser, 'reset_tokens' ) ) {
$this->mysql_parser = new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
} else {
$this->mysql_parser->reset_tokens( $tokens );
}

return $this->mysql_parser;
}

/**
Expand Down
Loading
Loading