Skip to content

Commit 94daf93

Browse files
committed
Reuse SQLite driver MySQL parser
1 parent b61b4d2 commit 94daf93

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

packages/mysql-on-sqlite/ext/wp-mysql-parser/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,18 @@ impl WpMySqlNativeParser {
15871587
})
15881588
}
15891589

1590+
pub fn reset_tokens(&mut self, tokens: &mut Zval) -> PhpResult<()> {
1591+
let (token_source, token_ids) = export_tokens(tokens)?;
1592+
1593+
self.token_source = Arc::new(token_source);
1594+
self.token_ids = token_ids;
1595+
self.position = 0;
1596+
self.current_ast = None;
1597+
self.current_php_ast = None;
1598+
1599+
Ok(())
1600+
}
1601+
15901602
pub fn parse(&mut self) -> PhpResult<Zval> {
15911603
stacker::maybe_grow(STACK_RED_ZONE, STACK_GROW_SIZE, || {
15921604
let ast = self.parse_native_ast()?;

packages/mysql-on-sqlite/src/mysql/class-wp-mysql-parser.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ class WP_MySQL_Parser extends WP_Parser {
88
*/
99
private $current_ast;
1010

11+
/**
12+
* Reset this parser with a new token stream.
13+
*
14+
* @param array<WP_Parser_Token> $tokens The parser tokens.
15+
*/
16+
public function reset_tokens( array $tokens ): void {
17+
$this->tokens = $tokens;
18+
$this->position = 0;
19+
$this->current_ast = null;
20+
}
21+
1122
/**
1223
* Parse the next query from the input SQL string.
1324
*

packages/mysql-on-sqlite/src/sqlite/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ class WP_PDO_MySQL_On_SQLite extends PDO {
410410
*/
411411
private static $mysql_grammar;
412412

413+
/**
414+
* A reusable parser instance for MySQL queries.
415+
*
416+
* @var WP_MySQL_Parser|null
417+
*/
418+
private $mysql_parser = null;
419+
413420
/**
414421
* The main database name.
415422
*
@@ -1160,11 +1167,27 @@ public function create_parser( string $query ): WP_MySQL_Parser {
11601167
);
11611168
if ( method_exists( $lexer, 'native_token_stream' ) ) {
11621169
$tokens = $lexer->native_token_stream();
1163-
return new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
1170+
return $this->reset_or_create_parser( $tokens );
11641171
}
11651172

11661173
$tokens = $lexer->remaining_tokens();
1167-
return new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
1174+
return $this->reset_or_create_parser( $tokens );
1175+
}
1176+
1177+
/**
1178+
* Reset the reusable parser with new tokens or create it on first use.
1179+
*
1180+
* @param array<WP_Parser_Token>|object $tokens Parser tokens.
1181+
* @return WP_MySQL_Parser A parser initialized for the token stream.
1182+
*/
1183+
private function reset_or_create_parser( $tokens ): WP_MySQL_Parser {
1184+
if ( null === $this->mysql_parser || ! method_exists( $this->mysql_parser, 'reset_tokens' ) ) {
1185+
$this->mysql_parser = new WP_MySQL_Parser( self::$mysql_grammar, $tokens );
1186+
} else {
1187+
$this->mysql_parser->reset_tokens( $tokens );
1188+
}
1189+
1190+
return $this->mysql_parser;
11681191
}
11691192

11701193
/**

tmp-test-native/build-extension.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ fi
4343
PHP_INCLUDES="$("$PHP_CONFIG" --includes)"
4444
CLANG_RESOURCE="$(clang -print-resource-dir)"
4545

46-
export BINDGEN_EXTRA_CLANG_ARGS="$PHP_INCLUDES -isystem $CLANG_RESOURCE/include ${BINDGEN_EXTRA_CLANG_ARGS:-}"
46+
# PHP's Zend headers include SSE intrinsics when __SSE2__ is defined. In some
47+
# Nix clang/libclang combinations bindgen fails while parsing those intrinsics,
48+
# even though the generated extension itself does not need them.
49+
export BINDGEN_EXTRA_CLANG_ARGS="$PHP_INCLUDES -isystem $CLANG_RESOURCE/include -U__SSE2__ ${BINDGEN_EXTRA_CLANG_ARGS:-}"
4750
export CFLAGS="$PHP_INCLUDES -isystem $CLANG_RESOURCE/include ${CFLAGS:-}"
4851

4952
cargo build --release

0 commit comments

Comments
 (0)