Skip to content

Commit 973810c

Browse files
authored
Add REGEXP_REPLACE(), REGEXP_SUBSTR(), and REGEXP_INSTR() (#625)
## Summary This PR adds MySQL's `REGEXP_REPLACE()`, `REGEXP_SUBSTR()`, and `REGEXP_INSTR()` functions, built on the REGEXP engine from #624. It also enables their test suites from #622. Before, all 16,649 cases of each suite failed, because the functions didn't exist. Now, these fail on PHP 8.5: - **`REGEXP_REPLACE()`:** 815 (MySQL cases: 276, ICU cases: 539). - **`REGEXP_SUBSTR()`:** 790 (MySQL cases: 264, ICU cases: 526). - **`REGEXP_INSTR()`:** 790 (MySQL cases: 264, ICU cases: 526). Almost all of them also fail for `REGEXP_LIKE()` (766), mostly because of ICU syntax that PCRE doesn't have. It's the second of the PRs that split #368 into smaller chunks. The main changes: - **`REGEXP_REPLACE()`** replaces every match, or only a chosen one, with MySQL's replacement syntax: `$1`, `${name}`, and backslash escapes. - **`REGEXP_SUBSTR()`** returns a match, and **`REGEXP_INSTR()`** returns where it starts or ends. - **Positions** count characters in text, and bytes in binary data that isn't valid UTF-8, like in MySQL. - **Empty matches** move the search forward by one character, like in MySQL. So `REGEXP_REPLACE('ab', '|a', '-')` returns `-a-b-`, where PHP's `preg_replace()` would give `---b-`. - **`\G` and `\K`** work like in MySQL: `\G` matches only where the previous match ended, and `\K` is a literal "K". In PHP, `\K` in a lookbehind could make a search loop forever. - **Arguments** work like in MySQL 8.4: `pos` one past the end, a negative `occurrence`, `NULL` arguments, and errors for invalid ones, checked in MySQL's order. ## Why Apps and plugins use these functions, and queries with them failed, because the driver didn't support them. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit - **New Features** - Added support for `REGEXP_REPLACE`, `REGEXP_SUBSTR`, and `REGEXP_INSTR` in MySQL-compatible SQLite queries, including occurrence selection, match positions, and replacement references. - Regex operations now support binary data and character- or byte-based positions, with improved handling of NULL values, match options, and empty matches. - **Tests** - Expanded coverage for regex functions and enabled their compatibility tests. Known differences are tracked across runtime versions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a7dc477 commit 973810c

6 files changed

Lines changed: 4195 additions & 49 deletions

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5396,8 +5396,12 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53965396
case 'VERSION':
53975397
return $this->quote_sqlite_value( $this->get_mysql_server_version_string() );
53985398
case 'REGEXP_LIKE':
5399-
// PDO narrows integer UDF arguments to 32 bits, so pass the subject and the pattern as text.
5400-
$text_args = min( count( $args ), 2 );
5399+
case 'REGEXP_SUBSTR':
5400+
case 'REGEXP_INSTR':
5401+
case 'REGEXP_REPLACE':
5402+
// PDO narrows integer UDF arguments to 32 bits, so pass the subject, the
5403+
// pattern, and REGEXP_REPLACE's replacement as text.
5404+
$text_args = min( count( $args ), 'REGEXP_REPLACE' === $name ? 3 : 2 );
54015405
for ( $i = 0; $i < $text_args; $i++ ) {
54025406
$args[ $i ] = sprintf( 'CAST(%s AS TEXT)', $args[ $i ] );
54035407
}

0 commit comments

Comments
 (0)