replace regexp with its postgres version

This commit is contained in:
Matthew Bucci
2024-10-17 02:45:32 -07:00
parent 90d1c47695
commit d0eab624eb
2 changed files with 19 additions and 5 deletions

View File

@ -65,6 +65,9 @@ class SelectSQLRewriter extends AbstractSQLRewriter
// Handle COUNT(*)...ORDER BY... // Handle COUNT(*)...ORDER BY...
$sql = preg_replace('/COUNT(.+)ORDER BY.+/s', 'COUNT$1', $sql); $sql = preg_replace('/COUNT(.+)ORDER BY.+/s', 'COUNT$1', $sql);
// HANDLE REGEXP
$sql = preg_replace('/REGEXP/', '~', $sql);
// In order for users counting to work... // In order for users counting to work...
$matches = array(); $matches = array();
if(preg_match_all('/COUNT[^C]+\),/', $sql, $matches)) { if(preg_match_all('/COUNT[^C]+\),/', $sql, $matches)) {

View File

@ -724,9 +724,9 @@ final class rewriteTest extends TestCase
) )
GROUP BY wp_posts.ID GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value+0 DESC, wp_posts.post_date DESC ORDER BY wp_postmeta.meta_value+0 DESC, wp_posts.post_date DESC
SQL; SQL;
$expected = <<<SQL $expected = <<<SQL
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts."ID" = wp_postmeta.post_id ) SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts."ID" = wp_postmeta.post_id )
WHERE 1=1 AND ( WHERE 1=1 AND (
wp_posts.post_date > '2024-07-17 23:59:59' wp_posts.post_date > '2024-07-17 23:59:59'
@ -737,14 +737,25 @@ final class rewriteTest extends TestCase
) )
ORDER BY CAST(wp_postmeta.meta_value AS INTEGER) DESC, wp_posts.post_date DESC ORDER BY CAST(wp_postmeta.meta_value AS INTEGER) DESC, wp_posts.post_date DESC
SQL; SQL;
$postgresql = pg4wp_rewrite($sql); $postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql)); $this->assertSame(trim($expected), trim($postgresql));
} }
public function test_it_rewrites_regexp()
{
$sql = <<<SQL
SELECT DISTINCT meta_key FROM wp_gf_entry_meta WHERE form_id=2 AND meta_key REGEXP '^[0-9]+(\.[0-9]+)?$'
SQL;
$expected = <<<SQL
SELECT DISTINCT meta_key FROM wp_gf_entry_meta WHERE form_id=2 AND meta_key ~ '^[0-9]+(\.[0-9]+)?$'
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}