From 08e48c86419fd31b8bef28e576d0a49f783ffd05 Mon Sep 17 00:00:00 2001 From: SeaEagle Date: Mon, 29 Jul 2024 19:08:50 +0000 Subject: [PATCH] MySQL: Fix where clause for JSON column Issue: https://github.com/adminerevo/adminerevo/issues/175 --- adminer/include/functions.inc.php | 34 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index b55f4b98..c4ae22d4 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -477,24 +477,36 @@ function escape_key($key) { */ function where($where, $fields = array()) { global $connection, $jush; - $return = array(); + + $conditions = []; + foreach ((array) $where["where"] as $key => $val) { $key = bracket_escape($key, 1); // 1 - back $column = escape_key($key); - $return[] = $column - . ($jush == "sql" && is_numeric($val) && preg_match('~\.~', $val) ? " LIKE " . q($val) // LIKE because of floats but slow with ints - : ($jush == "mssql" ? " LIKE " . q(preg_replace('~[_%[]~', '[\0]', $val)) // LIKE because of text - : " = " . unconvert_field($fields[$key], q($val)) - )) - ; //! enum and set - if ($jush == "sql" && preg_match('~char|text~', $fields[$key]["type"]) && preg_match("~[^ -@]~", $val)) { // not just [a-z] to catch non-ASCII characters - $return[] = "$column = " . q($val) . " COLLATE " . charset($connection) . "_bin"; + + if ($jush == "sql" && $fields[$key]["type"] == "json") { + $conditions[] = "$column = CAST(" . q($val) . " AS JSON)"; + } elseif ($jush == "sql" && is_numeric($val) && strpos($val, ".") !== false) { + // LIKE because of floats but slow with ints. + $conditions[] = "$column LIKE " . q($val); + } elseif ($jush == "mssql") { + // LIKE because of text. + $conditions[] = "$column LIKE " . q(preg_replace('~[_%[]~', '[\0]', $val)); + } else { + $conditions[] = "$column = " . unconvert_field($fields[$key], q($val)); + } + + // Not just [a-z] to catch non-ASCII characters. + if ($jush == "sql" && preg_match('~char|text~', $fields[$key]["type"]) && preg_match("~[^ -@]~", $val)) { + $conditions[] = "$column = " . q($val) . " COLLATE " . charset($connection) . "_bin"; } } + foreach ((array) $where["null"] as $key) { - $return[] = escape_key($key) . " IS NULL"; + $conditions[] = escape_key($key) . " IS NULL"; } - return implode(" AND ", $return); + + return implode(" AND ", $conditions); } /** Create SQL condition from query string