From 785307ee9576f522f49f5837fbb4e6070da5dc7a Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Sat, 6 Jun 2015 20:44:12 -0600 Subject: [PATCH] [nextgen-gallery] Fix identifiers quoted as strings MySQL allows quoting identifiers, such as column names, as strings using single-quotes in addition to quoting as identifiers using grave accents. PostgreSQL does not, resulting in errors such as: Error running : SELECT DISTINCT wp_ngg_pictures.* , GROUP_CONCAT(CONCAT_WS('@@', meta_key, meta_value)) AS 'extras' FROM `wp_ngg_pictures` LEFT OUTER JOIN `wp_postmeta` ON `wp_postmeta`.`post_id` = `extras_post_id` GROUP BY wp_ngg_pictures.pid LIMIT 1 ---- converted to ---- SELECT DISTINCT wp_ngg_pictures.* , GROUP_CONCAT(CONCAT_WS('@@', meta_key, meta_value)) AS 'extras' FROM wp_ngg_pictures LEFT OUTER JOIN wp_postmeta ON wp_postmeta.post_id = extras_post_id GROUP BY wp_ngg_pictures.pid LIMIT 1 ----> ERROR: syntax error at or near "'extras'" LINE 1: ..._CONCAT(CONCAT_WS('@@', meta_key, meta_value)) AS 'extras' F... ^ Fix this by replacing single quotes with grave accents when they occur after ") AS ". This strategy obviously has both false-positive and false-negative issues, but suits the current needs and should be relatively safe from false-positives. Proper replacement would require parsing the SQL. Signed-off-by: Kevin Locke --- pg4wp/driver_pgsql.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index 73edae0..2d7a522 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -292,6 +292,11 @@ if( false !== strpos( $sql, $wpdb->comments)) $sql = str_replace(' comment_id ', ' comment_ID ', $sql); + // MySQL supports strings as names, PostgreSQL needs identifiers. + // Limit to after closing parenthesis to reduce false-positives + // Currently only an issue for nextgen-gallery plugin + $pattern = '/\) AS \'([^\'])\'/'; + $sql = preg_replace( $pattern, ') AS "$1"', $sql); } // SELECT elseif( 0 === strpos($sql, 'UPDATE')) {