From f751d597088246d5a92572dca997f897be9be7d7 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Fri, 22 Jan 2021 00:51:47 +0900 Subject: [PATCH] Use latin1 charset for ASCII-only columns that need to be short for indexing --- common/framework/parsers/dbtable/column.php | 2 +- common/framework/parsers/dbtable/table.php | 15 +++++++++++---- common/framework/parsers/dbtableparser.php | 10 +++++++--- modules/module/schemas/module_trigger.xml | 10 +++++----- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/common/framework/parsers/dbtable/column.php b/common/framework/parsers/dbtable/column.php index 51df96f69..294685156 100644 --- a/common/framework/parsers/dbtable/column.php +++ b/common/framework/parsers/dbtable/column.php @@ -11,7 +11,7 @@ class Column public $type; public $xetype; public $size; - public $utf8mb4 = true; + public $charset = 'utf8mb4'; public $default_value; public $not_null = false; public $is_indexed = false; diff --git a/common/framework/parsers/dbtable/table.php b/common/framework/parsers/dbtable/table.php index d82f376c6..58a8d9b27 100644 --- a/common/framework/parsers/dbtable/table.php +++ b/common/framework/parsers/dbtable/table.php @@ -33,7 +33,7 @@ class Table foreach ($this->columns as $column) { $columndef = ' `' . $column->name . '`' . ' ' . strtoupper($column->type); - $max_size = $column->utf8mb4 ? 191 : 255; + $max_size = ($column->charset === 'utf8mb4' && $charset === 'utf8mb4') ? 191 : 255; if (preg_match('/char/i', $column->type) && $column->size > $max_size && ($column->is_unique || $column->is_primary_key)) { $adjusted_sizes[$column->name] = $max_size; @@ -42,9 +42,16 @@ class Table { $columndef .= '(' . (isset($adjusted_sizes[$column->name]) ? $adjusted_sizes[$column->name] : $column->size) . ')'; } - if ($column->utf8mb4 === false && $charset === 'utf8mb4') + if ($column->charset !== 'utf8mb4' && $column->charset !== $charset) { - $columndef .= ' CHARACTER SET utf8 COLLATE utf8_unicode_ci'; + if ($column->charset === 'utf8') + { + $columndef .= ' CHARACTER SET ' . $column->charset . ' COLLATE ' . $column->charset . '_unicode_ci'; + } + else + { + $columndef .= ' CHARACTER SET ' . $column->charset . ' COLLATE ' . $column->charset . '_general_ci'; + } } if ($column->not_null) { @@ -84,7 +91,7 @@ class Table { $column_info = $this->columns[$column_name]; $current_size = isset($adjusted_sizes[$column_name]) ? $adjusted_sizes[$column_name] : $column_info->size; - $max_size = $column_info->utf8mb4 ? 191 : 255; + $max_size = ($column_info->charset === 'utf8mb4' && $charset === 'utf8mb4') ? 191 : 255; if (preg_match('/char/i', $column_info->type) && $current_size > $max_size) { $prefix_size = $max_size; diff --git a/common/framework/parsers/dbtableparser.php b/common/framework/parsers/dbtableparser.php index e49f0a97a..aa91b9c49 100644 --- a/common/framework/parsers/dbtableparser.php +++ b/common/framework/parsers/dbtableparser.php @@ -78,10 +78,14 @@ class DBTableParser extends BaseParser // Get all attributes. $attribs = self::_getAttributes($column_info); - // Get the utf8mb4 attribute. - if (isset($attribs['utf8mb4'])) + // Get the charset/utf8mb4 attribute. + if (isset($attribs['charset'])) { - $column->utf8mb4 = toBool($attribs['utf8mb4']); + $column->charset = $attribs['charset']; + } + elseif (isset($attribs['utf8mb4'])) + { + $column->charset = toBool($attribs['utf8mb4']) ? 'utf8mb4' : 'utf8'; } // Get the default value. diff --git a/modules/module/schemas/module_trigger.xml b/modules/module/schemas/module_trigger.xml index b00d4790b..5e0252d32 100644 --- a/modules/module/schemas/module_trigger.xml +++ b/modules/module/schemas/module_trigger.xml @@ -1,7 +1,7 @@ - - - - - + + + + +