[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 <kevin@kevinlocke.name>
This commit is contained in:
Kevin Locke
2015-06-06 20:27:32 -06:00
parent 55a671b9f7
commit cf3f571dbf

View File

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