From 31f8f61d0e59d6cd79489455218c44b8de170814 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Mon, 10 Mar 2025 21:01:21 +0100 Subject: [PATCH] MySQL, MariaDB: Fix default values with ' (fix #895) --- adminer/drivers/mysql.inc.php | 22 ++++++++++++++++------ changes.txt | 1 + 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 1ca3cde9..236602d4 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -587,15 +587,28 @@ if (!defined('Adminer\DRIVER')) { * @return array [$name => ["field" => , "full_type" => , "type" => , "length" => , "unsigned" => , "default" => , "null" => , "auto_increment" => , "on_update" => , "collation" => , "privileges" => , "comment" => , "primary" => , "generated" => ]] */ function fields($table) { + global $connection; + $maria = preg_match('~MariaDB~', $connection->server_info); $return = array(); foreach (get_rows("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . q($table) . " ORDER BY ORDINAL_POSITION") as $row) { $field = $row["COLUMN_NAME"]; - $default = $row["COLUMN_DEFAULT"]; $type = $row["COLUMN_TYPE"]; + $generation = $row["GENERATION_EXPRESSION"]; $extra = $row["EXTRA"]; // https://mariadb.com/kb/en/library/show-columns/, https://github.com/vrana/adminer/pull/359#pullrequestreview-276677186 preg_match('~^(VIRTUAL|PERSISTENT|STORED)~', $extra, $generated); preg_match('~^([^( ]+)(?:\((.+)\))?( unsigned)?( zerofill)?$~', $type, $match); + $default = $row["COLUMN_DEFAULT"]; + $is_text = preg_match('~text~', $match[1]); + if (!$maria && $is_text) { + // default value a'b of text column is stored as _utf8mb4\'a\\\'b\' in MySQL + $default = preg_replace("~^(_\w+)?('.*')$~", '\2', stripslashes($default)); + } + if ($maria || $is_text) { + $default = preg_replace_callback("~^'(.*)'$~", function ($match) { + return str_replace("''", "'", stripslashes($match[1])); + }, $default); + } $return[$field] = array( "field" => $field, "full_type" => $type, @@ -603,11 +616,8 @@ if (!defined('Adminer\DRIVER')) { "length" => $match[2], "unsigned" => ltrim($match[3] . $match[4]), "default" => ($generated - ? $row["GENERATION_EXPRESSION"] - : ($default != "" || preg_match("~char|set~", $match[1]) - ? (preg_match('~text~', $match[1]) ? stripslashes(preg_replace("~^'(.*)'\$~", '\1', $default)) : $default) - : null - ) + ? ($maria ? $generation : stripslashes($generation)) + : ($default != "" || preg_match("~char|set~", $match[1]) ? $default : null) ), "null" => ($row["IS_NULLABLE"] == "YES"), "auto_increment" => ($extra == "auto_increment"), diff --git a/changes.txt b/changes.txt index a77bce59..a8d65b43 100644 --- a/changes.txt +++ b/changes.txt @@ -2,6 +2,7 @@ Adminer dev: Fix gzip export (bug #896) Fix importing multiple SQL files not terminated by semicolon Use for altering collations +MySQL, MariaDB: Fix default values with ' (bug #895) MariaDB: Fix creating and altering generated columns (bug #897) Adminer 5.0.2 (released 2025-03-10):