]> git.joonet.de Git - adminer.git/commitdiff
MySQL, MariaDB: Fix default values with ' (fix #895)
authorJakub Vrana <jakub@vrana.cz>
Mon, 10 Mar 2025 20:01:21 +0000 (21:01 +0100)
committerJakub Vrana <jakub@vrana.cz>
Mon, 10 Mar 2025 20:01:21 +0000 (21:01 +0100)
adminer/drivers/mysql.inc.php
changes.txt

index 1ca3cde94aadb698243f851b70c73283f011144e..236602d4dac38e1c2c5b5ff81d416274923852c7 100644 (file)
@@ -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"),
index a77bce596ae4510e091037d9ea71bfa848917770..a8d65b435ef462266d4de6b4b7430ab2f3fc4008 100644 (file)
@@ -2,6 +2,7 @@ Adminer dev:
 Fix gzip export (bug #896)
 Fix importing multiple SQL files not terminated by semicolon
 Use <datalist> 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):