From 37e63dd82f9f3982e92107eeed2bbb30f41c24a8 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Mon, 10 Mar 2025 23:32:28 +0100 Subject: [PATCH] MySQL: Stop treating enum as set as numbers (bug #475) --- adminer/call.inc.php | 5 +---- adminer/edit.inc.php | 8 +------- adminer/include/functions.inc.php | 23 ++++++++++------------- changes.txt | 1 + plugins/enum-option.php | 12 ++++-------- 5 files changed, 17 insertions(+), 32 deletions(-) diff --git a/adminer/call.inc.php b/adminer/call.inc.php index 1800ee16..b46eccd8 100644 --- a/adminer/call.inc.php +++ b/adminer/call.inc.php @@ -73,11 +73,8 @@ if ($in) { echo "" . $adminer->fieldName($field); $value = $_POST["fields"][$name]; if ($value != "") { - if ($field["type"] == "enum") { - $value = +$value; - } if ($field["type"] == "set") { - $value = array_sum($value); + $value = implode(",", $value); } } input($field, $value, (string) $_POST["function"][$name]); // param name can be empty diff --git a/adminer/edit.inc.php b/adminer/edit.inc.php index 70148535..07381151 100644 --- a/adminer/edit.inc.php +++ b/adminer/edit.inc.php @@ -68,13 +68,7 @@ if ($_POST["save"]) { $select = array(); foreach ($fields as $name => $field) { if (isset($field["privileges"]["select"])) { - $as = convert_field($field); - if ($_POST["clone"] && $field["auto_increment"]) { - $as = "''"; - } - if (JUSH == "sql" && preg_match("~enum|set~", $field["type"])) { - $as = "1*" . idf_escape($name); - } + $as = ($_POST["clone"] && $field["auto_increment"] ? "''" : convert_field($field)); $select[] = ($as ? "$as AS " : "") . idf_escape($name); } } diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 0dcb1bd1..fbc086de 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -891,18 +891,18 @@ function column_foreign_keys($table) { * @param string "radio"|"checkbox" * @param string * @param array -* @param mixed int|string|array +* @param mixed string|array * @param string * @return null */ function enum_input($type, $attrs, $field, $value, $empty = null) { global $adminer; preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches); - $return = ($empty !== null ? "" : ""); + $return = ($empty !== null ? "" : ""); foreach ($matches[1] as $i => $val) { $val = stripcslashes(str_replace("''", "'", $val)); - $checked = (is_int($value) ? $value == $i+1 : (is_array($value) ? in_array($i+1, $value) : $value === $val)); - $return .= " '; + $checked = (is_array($value) ? in_array($val, $value) : $value === $val); + $return .= " '; } return $return; } @@ -949,12 +949,12 @@ function input($field, $value, $function) { } elseif (preg_match('~bool~', $field["type"])) { echo "" . ""; - } elseif ($field["type"] == "set") { //! 64 bits + } elseif ($field["type"] == "set") { preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches); foreach ($matches[1] as $i => $val) { $val = stripcslashes(str_replace("''", "'", $val)); - $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true)); - echo " '; + $checked = in_array($val, explode(",", $value), true); + echo " '; } } elseif (preg_match('~blob|bytea|raw|file~', $field["type"]) && ini_bool("file_uploads")) { echo ""; @@ -1023,9 +1023,6 @@ function process_input($field) { return "NULL"; } } - if ($field["type"] == "enum") { - return +$value; - } if ($field["auto_increment"] && $value == "") { return null; } @@ -1036,7 +1033,7 @@ function process_input($field) { return "NULL"; } if ($field["type"] == "set") { - return array_sum((array) $value); + $value = implode(",", (array) $value); } if ($function == "json") { $function = ""; @@ -1440,8 +1437,8 @@ function edit_form($table, $fields, $row, $update) { } } $value = ($row !== null - ? ($row[$name] != "" && JUSH == "sql" && preg_match("~enum|set~", $field["type"]) - ? (is_array($row[$name]) ? array_sum($row[$name]) : +$row[$name]) + ? ($row[$name] != "" && JUSH == "sql" && preg_match("~enum|set~", $field["type"]) && is_array($row[$name]) + ? implode(",", $row[$name]) : (is_bool($row[$name]) ? +$row[$name] : $row[$name]) ) : (!$update && $field["auto_increment"] diff --git a/changes.txt b/changes.txt index a6092a48..410fa9ea 100644 --- a/changes.txt +++ b/changes.txt @@ -3,6 +3,7 @@ Fix gzip export (bug #896) Fix importing multiple SQL files not terminated by semicolon Use for altering collations MySQL: Allow setting default values of text column +MySQL: Stop treating enum as set as numbers (bug #475) MySQL, MariaDB: Fix default values with ' (bug #895) MariaDB: Fix creating and altering generated columns (bug #897) diff --git a/plugins/enum-option.php b/plugins/enum-option.php index 7ad0d213..418561b1 100644 --- a/plugins/enum-option.php +++ b/plugins/enum-option.php @@ -20,20 +20,16 @@ class AdminerEnumOption { } if ($field["null"]) { $options[""] = "NULL"; - if ($value === null && !isset($_GET["select"])) { + if ($selected === null) { $selected = ""; } } - $options[0] = Adminer\lang('empty'); preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches); - foreach ($matches[1] as $i => $val) { + foreach ($matches[1] as $val) { $val = stripcslashes(str_replace("''", "'", $val)); - $options[$i + 1] = $val; - if ($value === $val) { - $selected = $i + 1; - } + $options[$val] = $val; } - return "" . Adminer\optionlist($options, (string) $selected, 1) . ""; // 1 - use keys + return "" . Adminer\optionlist($options, $selected, 1) . ""; // 1 - use keys } } }