Skip to content

Commit 86a9113

Browse files
authored
Add REVERSE() to user defined functions (#434)
I ran into an issue where a plugin is using the REVERSE() function to add the reverse of a string into the database. Since this isn't supported by SQLite it breaks the plugin. This fixes it for me.
1 parent 49d783b commit 86a9113

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static function register_for( $pdo ): self {
9090
'utc_time' => 'utc_time',
9191
'utc_timestamp' => 'utc_timestamp',
9292
'version' => 'version',
93+
'reverse' => 'reverse',
9394

9495
// Internal helper functions.
9596
'_helper_like_to_glob_pattern' => '_helper_like_to_glob_pattern',
@@ -929,6 +930,22 @@ public function version() {
929930
return '5.5';
930931
}
931932

933+
/**
934+
* Method to emulate MySQL REVERSE() function.
935+
*
936+
* Takes a string and returns the reverse of it.
937+
*
938+
* @param string|null $str The string to reverse.
939+
*
940+
* @return string|null reversed string, or NULL.
941+
*/
942+
public function reverse( $str ) {
943+
if ( null === $str ) {
944+
return null;
945+
}
946+
return strrev( $str );
947+
}
948+
932949
/**
933950
* A helper to covert LIKE pattern to a GLOB pattern for "LIKE BINARY" support.
934951

‎packages/mysql-on-sqlite/tests/WP_SQLite_Driver_Tests.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12546,4 +12546,18 @@ public function testCreateTableWithDefaultExpressions(): void {
1254612546
$this->assertSame( "DATETIME(CURRENT_TIMESTAMP, '+' || 1 || ' YEAR')", $result[2]['dflt_value'] );
1254712547
$this->assertSame( "('a' || 'b')", $result[3]['dflt_value'] );
1254812548
}
12549+
12550+
public function testReverseFunction(): void {
12551+
// Normal reverse.
12552+
$result = $this->assertQuery( "SELECT REVERSE('aoeuidhtns') AS reversed" );
12553+
$this->assertSame( 'snthdiueoa', $result[0]->reversed );
12554+
12555+
// Empty string.
12556+
$result = $this->assertQuery( "SELECT REVERSE('') AS reversed" );
12557+
$this->assertSame( '', $result[0]->reversed );
12558+
12559+
// NULL input returns NULL.
12560+
$result = $this->assertQuery( 'SELECT REVERSE(NULL) AS reversed' );
12561+
$this->assertNull( $result[0]->reversed );
12562+
}
1254912563
}

0 commit comments

Comments
 (0)