Support enum and double columns in MysqlDatabaseEditor::getColumns()
authorMatthias Schmidt <gravatronics@live.com>
Sun, 11 Aug 2019 10:19:21 +0000 (12:19 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 11 Aug 2019 10:19:21 +0000 (12:19 +0200)
Close #3011

wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php

index 80b2cf98bcb1bab969a83d52400508f41dbdec21..5779a23b5bfa57c76815bb09fc76a936d681cba0 100644 (file)
@@ -30,7 +30,7 @@ class MySQLDatabaseEditor extends DatabaseEditor {
         */
        public function getColumns($tableName) {
                $columns = [];
-               $regex = new Regex('([a-z]+)\(([0-9]+)\)', Regex::CASE_INSENSITIVE);
+               $regex = new Regex('([a-z]+)\((.+)\)', Regex::CASE_INSENSITIVE);
                
                $sql = "SHOW COLUMNS FROM `".$tableName."`";
                $statement = $this->dbObj->prepareStatement($sql);
@@ -39,13 +39,52 @@ class MySQLDatabaseEditor extends DatabaseEditor {
                        $regex->match($row['Type']);
                        $typeMatches = $regex->getMatches();
                        
+                       $type = $row['Type'];
+                       $length = '';
+                       $decimals = '';
+                       $enumValues = '';
+                       if (!empty($typeMatches)) {
+                               $type = $typeMatches[1];
+                               
+                               switch ($type) {
+                                       case 'enum':
+                                       case 'set':
+                                               $enumValues = $typeMatches[2];
+                                               break;
+                                               
+                                       case 'decimal':
+                                       case 'double':
+                                       case 'float':
+                                               $pieces = explode(',', $typeMatches[2]);
+                                               switch (count($pieces)) {
+                                                       case 1:
+                                                               $length = $pieces[0];
+                                                               break;
+                                                               
+                                                       case 2:
+                                                               list($length, $decimals) = $pieces;
+                                                               break;
+                                               }
+                                               
+                                               break;
+                                               
+                                       default:
+                                               if ($typeMatches[2] == (int)$typeMatches[2]) {
+                                                       $length = $typeMatches[2];
+                                               }
+                                               break;
+                               }
+                       }
+                       
                        $columns[] = ['name' => $row['Field'], 'data' => [
-                               'type' => empty($typeMatches) ? $row['Type'] : $typeMatches[1],
-                               'length' => empty($typeMatches) ? '' : $typeMatches[2],
-                               'notNull' => ($row['Null'] == 'YES') ? false : true,
+                               'type' => $type,
+                               'length' => $length,
+                               'notNull' => $row['Null'] == 'YES' ? false : true,
                                'key' => ($row['Key'] == 'PRI') ? 'PRIMARY' : (($row['Key'] == 'UNI') ? 'UNIQUE' : ''),
                                'default' => $row['Default'],
-                               'autoIncrement' => $row['Extra'] == 'auto_increment' ? true : false
+                               'autoIncrement' => $row['Extra'] == 'auto_increment' ? true : false,
+                               'enumValues' => $enumValues,
+                               'decimals' => $decimals
                        ]];
                }