From cf3f571dbf180b30a18c8302e657d1b69555f11f Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Sat, 6 Jun 2015 20:27:32 -0600 Subject: [PATCH] [nextgen-gallery] Support for CONVERT() in place of CAST() MySQL provides a CONVERT function for doing data type conversion. This currently results in errors such as: Error running : SELECT image_slug, SUBSTR(image_slug, 10) AS 'i' FROM wp_ngg_pictures WHERE (image_slug LIKE 'img_0601-%' AND CONVERT(SUBSTR(image_slug, 10), SIGNED) BETWEEN 1 AND 2147483647) OR image_slug = 'img_0601' ORDER BY i DESC LIMIT 1 ---- converted to ---- SELECT image_slug, SUBSTR(image_slug, 10) AS "i" FROM wp_ngg_pictures WHERE (image_slug ILIKE 'img_0601-%' AND CONVERT(SUBSTR(image_slug, 10), SIGNED) BETWEEN 1 AND 2147483647) OR image_slug = 'img_0601' ORDER BY i DESC LIMIT 1 ----> ERROR: column "signed" does not exist LINE 1: ... 'img_0601-%' AND CONVERT(SUBSTR(image_slug, 10), SIGNED) BE... Recognize this function and replace it with CAST(). Signed-off-by: Kevin Locke --- pg4wp/driver_pgsql.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pg4wp/driver_pgsql.php b/pg4wp/driver_pgsql.php index 16c84f9..4a5702d 100644 --- a/pg4wp/driver_pgsql.php +++ b/pg4wp/driver_pgsql.php @@ -226,6 +226,10 @@ $pattern = '/SELECT\s+([^\s]+)\s+(FROM.+)/'; $sql = preg_replace( $pattern, 'SELECT COUNT($1) $2', $sql); } + + // Convert CONVERT to CAST + $pattern = '/CONVERT\(([^()]*(\(((?>[^()]+)|(?-2))*\))?[^()]*),\s*([^\s]+)\)/x'; + $sql = preg_replace( $pattern, 'CAST($1 AS $4)', $sql); // Handle CAST( ... AS CHAR) $sql = preg_replace( '/CAST\((.+) AS CHAR\)/', 'CAST($1 AS TEXT)', $sql);