From 9120c5fec25a77215dcf9901ec6292e3328d4fcd Mon Sep 17 00:00:00 2001 From: hawk__ Date: Sun, 20 May 2012 19:24:06 +0000 Subject: [PATCH] Protect against a possible collision when handling 'ON DUPLICATE KEY' git-svn-id: https://plugins.svn.wordpress.org/postgresql-for-wordpress/trunk@546689 b8457f37-d9ea-0310-8a92-e5e31aec5664 --- pg4wp/driver_pgsql.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index 5497711..2c0a390 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -313,12 +313,17 @@ // consisting in deleting the row before inserting it if( false !== $pos = strpos( $sql, 'ON DUPLICATE KEY')) { - // Remove 'ON DUPLICATE KEY UPDATE...' and following - $sql = substr( $sql, 0, $pos); // Get the elements we need (table name, first field, corresponding value) $pattern = '/INSERT INTO\s+([^\(]+)\(([^,]+)[^\(]+VALUES\s*\(([^,]+)/'; preg_match($pattern, $sql, $matches); - $sql = 'DELETE FROM '.$matches[1].' WHERE '.$matches[2].' = '.$matches[3].';'.$sql; + $table = trim( $matches[1], ' `'); + if( !in_array(trim($matches[1],'` '), array($wpdb->posts,$wpdb->comments))) + { + // Remove 'ON DUPLICATE KEY UPDATE...' and following + $sql = substr( $sql, 0, $pos); + // Add a delete query to handle the maybe existing data + $sql = 'DELETE FROM '.$table.' WHERE '.$matches[2].' = '.$matches[3].';'.$sql; + } } // To avoid Encoding errors when inserting data coming from outside