Skip to content

Commit f03cc84

Browse files
authored
Merge pull request #133 from PostgreSQL-For-Wordpress/hotfix/handle-zero-int-addition-casting
cast numbers explictly as ints rather than adding 0 to them
2 parents 3de1eaa + 90d1c47 commit f03cc84

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pg4wp/rewriters/SelectSQLRewriter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function rewrite(): string
4646

4747
$sql = $this->ensureOrderByInSelect($sql);
4848

49+
// Handle +0 casting in order by
50+
// Regular expression to match the "ORDER BY" pattern
51+
$pattern = '/ORDER BY\s+([a-zA-Z0-9_]+)\.meta_value\s*\+\s*0/i';
52+
$replacement = 'ORDER BY CAST($1.meta_value AS SIGNED)';
53+
$sql = preg_replace($pattern, $replacement, $sql);
54+
4955
// Convert CONVERT to CAST
5056
$pattern = '/CONVERT\(([^()]*(\(((?>[^()]+)|(?-2))*\))?[^()]*),\s*([^\s]+)\)/x';
5157
$sql = preg_replace($pattern, 'CAST($1 AS $4)', $sql);
@@ -120,6 +126,8 @@ public function rewrite(): string
120126
$pattern = '/@@SESSION.sql_mode/';
121127
$sql = preg_replace($pattern, "''", $sql);
122128

129+
130+
// TODO: this seems wrong but if we remove it we get failures with XYZ is not part of the group By
123131
if(isset($wpdb)) {
124132
$sql = str_replace('GROUP BY ' . $wpdb->prefix . 'posts.ID', '', $sql);
125133
}

tests/rewriteTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,40 @@ public function test_it_rewrites_mediumints()
710710
$this->assertSame(trim($expected), trim($postgresql));
711711
}
712712

713+
public function test_it_rewrites_0CASTS()
714+
{
715+
716+
$sql = <<<SQL
717+
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
718+
WHERE 1=1 AND (
719+
wp_posts.post_date > '2024-07-17 23:59:59'
720+
) AND (
721+
wp_postmeta.meta_key = 'make_feature_post'
722+
) AND (
723+
(wp_posts.post_type = 'announcement' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private'))
724+
)
725+
GROUP BY wp_posts.ID
726+
ORDER BY wp_postmeta.meta_value+0 DESC, wp_posts.post_date DESC
727+
SQL;
728+
729+
$expected = <<<SQL
730+
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts."ID" = wp_postmeta.post_id )
731+
WHERE 1=1 AND (
732+
wp_posts.post_date > '2024-07-17 23:59:59'
733+
) AND (
734+
wp_postmeta.meta_key = 'make_feature_post'
735+
) AND (
736+
(wp_posts.post_type = 'announcement' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private'))
737+
)
738+
739+
ORDER BY CAST(wp_postmeta.meta_value AS INTEGER) DESC, wp_posts.post_date DESC
740+
SQL;
741+
742+
$postgresql = pg4wp_rewrite($sql);
743+
$this->assertSame(trim($expected), trim($postgresql));
744+
}
745+
746+
713747

714748

715749

0 commit comments

Comments
 (0)