From 35e0af42525301e81abcffa0e698d3b0cc5025cf Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Fri, 14 Apr 2017 12:54:14 -0600 Subject: [PATCH] Convert HAVING without GROUP BY to WHERE Fixes #9 Signed-off-by: Kevin Locke --- pg4wp/driver_pgsql.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index d4281b2..8807668 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -329,6 +329,18 @@ if( isset($wpdb) && false !== strpos( $sql, $wpdb->comments)) $sql = str_replace(' comment_id ', ' comment_ID ', $sql); + // MySQL treats a HAVING clause without GROUP BY like WHERE + if( false !== strpos($sql, 'HAVING') && false === strpos($sql, 'GROUP BY')) + { + if( false === strpos($sql, 'WHERE')) + $sql = str_replace('HAVING', 'WHERE', $sql); + else + { + $pattern = '/WHERE\s+(.*?)\s+HAVING\s+(.*?)(\s*(?:ORDER|LIMIT|PROCEDURE|INTO|FOR|LOCK|$))/'; + $sql = preg_replace( $pattern, 'WHERE ($1) AND ($2) $3', $sql); + } + } + // MySQL allows integers to be used as boolean expressions // where 0 is false and all other values are true. //