Skip to content

Commit

Permalink
Merge pull request #134 from PostgreSQL-For-Wordpress/hotfix/handle-r…
Browse files Browse the repository at this point in the history
…egexp

replace regexp with its postgres version
  • Loading branch information
mattbucci authored Oct 17, 2024
2 parents f03cc84 + d0eab62 commit 9f33ebc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pg4wp/rewriters/SelectSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public function rewrite(): string
// Handle COUNT(*)...ORDER BY...
$sql = preg_replace('/COUNT(.+)ORDER BY.+/s', 'COUNT$1', $sql);

// HANDLE REGEXP
$sql = preg_replace('/REGEXP/', '~', $sql);

// In order for users counting to work...
$matches = array();
if(preg_match_all('/COUNT[^C]+\),/', $sql, $matches)) {
Expand Down
21 changes: 16 additions & 5 deletions tests/rewriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,9 @@ public function test_it_rewrites_0CASTS()
)
GROUP BY wp_posts.ID
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 )
WHERE 1=1 AND (
wp_posts.post_date > '2024-07-17 23:59:59'
Expand All @@ -737,14 +737,25 @@ public function test_it_rewrites_0CASTS()
)
ORDER BY CAST(wp_postmeta.meta_value AS INTEGER) DESC, wp_posts.post_date DESC
SQL;
SQL;

$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
$postgresql = pg4wp_rewrite($sql);
$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));
}



Expand Down

0 comments on commit 9f33ebc

Please sign in to comment.