8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

12.13 添加字符集12.13 Adding a Character Set

本节讨论了向 MySQL 添加字符集的程序。正确的程序取决于字符集是简单还是复杂:This section discusses the procedure for adding a character set to MySQL. The proper procedure depends on whether the character set is simple or complex:

  • 如果字符集不需要用于排序的特殊字符串整理例程,也不需要多字节字符支持,则它是简单的。If the character set does not need special string collating routines for sorting and does not need multibyte character support, it is simple.

  • 如果字符集需要上述任一功能,则它是复杂的。If the character set needs either of those features, it is complex.

例如,greek 和 swe7 是简单字符集,而 big5 和 czech 是复杂字符集。For example, greek and swe7 are simple character sets, whereas big5 and czech are complex character sets.

要使用以下说明,您必须拥有 MySQL 源代码分发版。在说明中,MYSET 代表您想要添加的字符集名称。To use the following instructions, you must have a MySQL source distribution. In the instructions, MYSET represents the name of the character set that you want to add.

  1. 在 sql/share/charsets/Index.xml 文件中为 MYSET 添加一个 <charset> 元素。使用文件中的现有内容作为添加新内容的指南。以下是 latin1 <charset> 元素的部分清单:Add a <charset> element for MYSET to the sql/share/charsets/Index.xml file. Use the existing contents in the file as a guide to adding new contents. A partial listing for the latin1 <charset> element follows:

    <charset name="latin1">
      <family>Western</family>
      <description>cp1252 West European</description>
      ...
      <collation name="latin1_swedish_ci" id="8" order="Finnish, Swedish">
        <flag>primary</flag>
        <flag>compiled</flag>
      </collation>
      <collation name="latin1_danish_ci" id="15" order="Danish"/>
      ...
      <collation name="latin1_bin" id="47" order="Binary">
        <flag>binary</flag>
        <flag>compiled</flag>
      </collation>
      ...
    </charset>
    

    <charset> 元素必须列出该字符集的所有排序规则。这些排序规则必须至少包括一个二进制排序规则和一个默认(主要)排序规则。默认排序规则通常使用后缀 general_ci(通用,区分大小写) 命名。二进制排序规则可以是默认排序规则,但通常它们是不同的。默认排序规则应该有一个 primary 标志。二进制排序规则应该有一个 binary 标志。The <charset> element must list all the collations for the character set. These must include at least a binary collation and a default (primary) collation. The default collation is often named using a suffix of general_ci (general, case-insensitive). It is possible for the binary collation to be the default collation, but usually they are different. The default collation should have a primary flag. The binary collation should have a binary flag.

    您必须为每个排序规则分配一个唯一的 ID 号。ID 范围从 1024 到 2047 保留给用户自定义的排序规则。要查找当前使用的排序规则 ID 的最大值,请使用此查询:You must assign a unique ID number to each collation. The range of IDs from 1024 to 2047 is reserved for user-defined collations. To find the maximum of the currently used collation IDs, use this query:

    SELECT MAX(ID) FROM INFORMATION_SCHEMA.COLLATIONS;
    
  2. 此步骤取决于您是添加简单字符集还是复杂字符集。简单字符集仅需要一个配置文件,而复杂字符集则需要定义排序函数、多字节函数或两者的 C 源文件。This step depends on whether you are adding a simple or complex character set. A simple character set requires only a configuration file, whereas a complex character set requires C source file that defines collation functions, multibyte functions, or both.

    对于一个简单的字符集,创建一个配置文件,MYSET.xml,该文件描述字符集属性。在 sql/share/charsets 目录中创建此文件。你可以使用 latin1.xml 的副本作为此文件的基础。该文件的语法非常简单:For a simple character set, create a configuration file, MYSET.xml, that describes the character set properties. Create this file in the sql/share/charsets directory. You can use a copy of latin1.xml as the basis for this file. The syntax for the file is very simple:

    • 注释用普通的 XML 注释书写(<!-- text -->)。Comments are written as ordinary XML comments (<!-- text -->).

    • <map> 数组元素中的单词由任意数量的空白字符分隔。Words within <map> array elements are separated by arbitrary amounts of whitespace.

    • <map> 数组元素中的每个单词都必须是十六进制格式的数字。Each word within <map> array elements must be a number in hexadecimal format.

    • <ctype> 元素的 <map> 数组元素有 257 个词。之后的其他 <map> 数组元素有 256 个词。参见 第 12.13.1 节,“字符定义数组”。The <map> array element for the <ctype> element has 257 words. The other <map> array elements after that have 256 words. See Section 12.13.1, “Character Definition Arrays”.

    • 对于 Index.xml 中字符集的 <charset> 元素列出的每个排序规则,MYSET.xml 必须包含一个定义字符顺序的 <collation> 元素。For each collation listed in the <charset> element for the character set in Index.xml, MYSET.xml must contain a <collation> element that defines the character ordering.

    对于复杂的字符集,创建一个 C 源文件,描述字符集属性并定义执行字符集操作所需的支持例程:For a complex character set, create a C source file that describes the character set properties and defines the support routines necessary to properly perform operations on the character set:

  3. 修改配置信息。使用现有的配置信息作为添加 MYSYS 信息的指南。这里的示例假设字符集有默认和二进制排序规则,但如果 MYSET 有额外的排序规则,则需要更多行。Modify the configuration information. Use the existing configuration information as a guide to adding information for MYSYS. The example here assumes that the character set has default and binary collations, but more lines are needed if MYSET has additional collations.

    1. 编辑 mysys/charset-def.c,并为新的字符集 “注册” 排序规则。Edit mysys/charset-def.c, and “register” the collations for the new character set.

      将这些行添加到 “声明” 部分:Add these lines to the “declaration” section:

      #ifdef HAVE_CHARSET_MYSET
      extern CHARSET_INFO my_charset_MYSET_general_ci;
      extern CHARSET_INFO my_charset_MYSET_bin;
      #endif
      

      将这些行添加到 “注册” 部分:Add these lines to the “registration” section:

      #ifdef HAVE_CHARSET_MYSET
        add_compiled_collation(&my_charset_MYSET_general_ci);
        add_compiled_collation(&my_charset_MYSET_bin);
      #endif
      
    2. 如果字符集使用 ctype-MYSET.c,编辑 strings/CMakeLists.txt 并将 ctype-MYSET.c 添加到 STRINGS_SOURCES 变量的定义中。If the character set uses ctype-MYSET.c, edit strings/CMakeLists.txt and add ctype-MYSET.c to the definition of the STRINGS_SOURCES variable.

    3. 编辑 cmake/character_sets.cmake:Edit cmake/character_sets.cmake:

      1. 将 MYSET 按字母顺序添加到 CHARSETS_AVAILABLE 的值中。Add MYSET to the value of with CHARSETS_AVAILABLE in alphabetic order.

      2. 将 MYSET 按字母顺序添加到 CHARSETS_COMPLEX 的值中。即使是简单字符集,也需要这样做,以便 CMake 能识别 -DDEFAULT_CHARSET=MYSET。Add MYSET to the value of CHARSETS_COMPLEX in alphabetic order. This is needed even for simple character sets, so that CMake can recognize -DDEFAULT_CHARSET=MYSET.

  4. 重新配置,重新编译,并进行测试。Reconfigure, recompile, and test.