Skip to content

Commit 121372a

Browse files
committed
Fix: Handle RAND(seed) determinism and refine FOR UPDATE regex for trailing comments
1 parent 87935d3 commit 121372a

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

integrations/plugin-compatibility/boot.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ function wp_sqlite_integration_plugin_compat( $query ) {
3030

3131
// 2. Global fallback for FOR UPDATE / SKIP LOCKED.
3232
// This ensures the legacy driver natively ignores these locking constraints where SQLite faults.
33-
// We use a strict end-of-query match to avoid destroying strings inside WHERE clauses.
33+
// We use a non-destructive lookahead for whitespace, semicolon, or comments to avoid destroying strings.
3434
if ( stripos( $query, 'FOR UPDATE' ) !== false ) {
35-
$query = preg_replace( '/\s+FOR\s+UPDATE(?:\s+(?:SKIP\s+LOCKED|NOWAIT))?\s*(?:;+)?\s*$/i', '', $query );
35+
$query = preg_replace( '/\s+FOR\s+UPDATE(?:\s+(?:SKIP\s+LOCKED|NOWAIT))?(?=\s*(?:;|\/\*|$))/i', '', $query );
3636
}
3737

3838
// 3. Action Scheduler specific compatibility fixes.

tests/WP_SQLite_Driver_Tests.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9068,24 +9068,23 @@ public function testColumnInfoForExpressions(): void {
90689068
'mysqli:type' => 8,
90699069
),
90709070
array(
9071-
// TODO: Fix custom "RAND()" function to behave like in MySQL.
9072-
'native_type' => 'LONGLONG', // DOUBLE in MySQL.
9073-
'pdo_type' => PDO::PARAM_INT, // PARAM_STR in MySQL.
9071+
'native_type' => 'DOUBLE', // DOUBLE in MySQL.
9072+
'pdo_type' => PDO::PARAM_STR, // PARAM_STR in MySQL.
90749073
'flags' => array( 'not_null' ),
90759074
'table' => '',
90769075
'name' => 'col_expr_19',
9077-
'len' => 21, // 23 in MySQL.
9078-
'precision' => 0, // 31 in MySQL.
9076+
'len' => 23, // 23 in MySQL.
9077+
'precision' => 31, // 31 in MySQL.
90799078
'sqlite:decl_type' => '',
9080-
9081-
// Additional MySQLi metadata.
9082-
'mysqli:orgname' => 'col_expr_19',
9083-
'mysqli:orgtable' => '',
9084-
'mysqli:db' => 'wp',
9085-
'mysqli:charsetnr' => 63,
9086-
'mysqli:flags' => 0, // 32769 in MySQL.
9087-
'mysqli:type' => 8, // 5 in MySQL.
9088-
),
9079+
9080+
// Additional MySQLi metadata.
9081+
'mysqli:orgname' => 'col_expr_19',
9082+
'mysqli:orgtable' => '',
9083+
'mysqli:db' => 'wp',
9084+
'mysqli:charsetnr' => 63,
9085+
'mysqli:flags' => 0, // 32769 in MySQL.
9086+
'mysqli:type' => 5, // 5 in MySQL.
9087+
),
90899088
array(
90909089
'native_type' => 'LONGLONG',
90919090
'pdo_type' => PDO::PARAM_INT,

wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4371,7 +4371,10 @@ private function translate_function_call( WP_Parser_Node $node ): string {
43714371

43724372
switch ( $name ) {
43734373
case 'RAND':
4374-
return '(ABS(RANDOM()) / 9223372036854775808.0)';
4374+
if ( empty( $args ) ) {
4375+
return '(ABS(RANDOM()) / 9223372036854775808.0)';
4376+
}
4377+
break;
43754378
case 'DATE_FORMAT':
43764379
list ( $date, $mysql_format ) = $args;
43774380

0 commit comments

Comments
 (0)