[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 <kevin@kevinlocke.name>
This commit is contained in:
Kevin Locke
2015-06-06 21:03:09 -06:00
parent a9edb3a572
commit 42c63a916f

View File

@ -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);