From 42c63a916f4b1bca70e2b4f8fb3e80ef6d4e1924 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Sat, 6 Jun 2015 21:03:09 -0600 Subject: [PATCH] [nextgen-gallery] Remove ORDER BY from DELETE where possible MySQL supports the ORDER BY clause on DELETE statements for use with the LIMIT clause to choose which rows are deleted. PostgreSQL does not support this clause, which leads to errors such as: Error running : DELETE FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'display_type' AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')) ORDER BY wp_posts.post_date DESC ---- converted to ---- DELETE FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'display_type' AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')) ORDER BY wp_posts.post_date DESC ----> ERROR: syntax error at or near "ORDER" LINE 1: ...rash' AND wp_posts.post_status <> 'auto-draft')) ORDER BY w... When the ORDER BY clause is specified without a LIMIT clause, as it is in the above error, it does not have any effect that I am aware of (due to how transactions are handled). In these cases, we remove the ORDER BY clause to avoid the error. Signed-off-by: Kevin Locke --- pg4wp/driver_pgsql.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index 2a232bb..ca966ea 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -412,6 +412,15 @@ elseif( 0 === strpos( $sql, 'DELETE' )) { $logto = 'DELETE'; + + // ORDER BY is not supported in DELETE queries, and not required + // when LIMIT is not present + if( false !== strpos( $sql, 'ORDER BY') && false === strpos( $sql, 'LIMIT')) + { + $pattern = '/ORDER BY \S+ (ASC|DESC)?/'; + $sql = preg_replace( $pattern, '', $sql); + } + // LIMIT is not allowed in DELETE queries $sql = str_replace( 'LIMIT 1', '', $sql); $sql = str_replace( ' REGEXP ', ' ~ ', $sql);