本节讨论了向 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.
在
sql/share/charsets/Index.xml文件中为MYSET添加一个<charset>元素。使用文件中的现有内容作为添加新内容的指南。以下是latin1<charset>元素的部分清单:Add a<charset>element forMYSETto thesql/share/charsets/Index.xmlfile. Use the existing contents in the file as a guide to adding new contents. A partial listing for thelatin1<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 ofgeneral_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 aprimaryflag. The binary collation should have abinaryflag.您必须为每个排序规则分配一个唯一的 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;
此步骤取决于您是添加简单字符集还是复杂字符集。简单字符集仅需要一个配置文件,而复杂字符集则需要定义排序函数、多字节函数或两者的 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.xmlsql/share/charsets目录中创建此文件。你可以使用latin1.xml的副本作为此文件的基础。该文件的语法非常简单:For a simple character set, create a configuration file,, that describes the character set properties. Create this file in theMYSET.xmlsql/share/charsetsdirectory. You can use a copy oflatin1.xmlas 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 inIndex.xml,must contain aMYSET.xml<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:
在
strings目录中创建文件ctype-。查看现有的MYSET.cctype-*.c文件(例如ctype-big5.c)以了解需要定义的内容。你文件中的数组必须使用类似ctype_、MYSETto_lower_等名称。这些对应于简单字符集的数组。参考 第 12.13.1 节,“字符定义数组”。Create the fileMYSETctype-in theMYSET.cstringsdirectory. Look at one of the existingctype-*.cfiles (such asctype-big5.c) to see what needs to be defined. The arrays in your file must have names likectype_,MYSETto_lower_, and so on. These correspond to the arrays for a simple character set. See Section 12.13.1, “Character Definition Arrays”.MYSET对于
<collation>元素中列出的每一个字符集<charset>元素,在Index.xml中,ctype-文件必须提供该排序的实现。For eachMYSET.c<collation>element listed in the<charset>element for the character set inIndex.xml, thectype-file must provide an implementation of the collation.MYSET.c如果字符集需要字符串排序函数,请参见 第 12.13.2 节,“复杂字符集的字符串排序支持”。If the character set requires string collating functions, see Section 12.13.2, “String Collating Support for Complex Character Sets”.
如果字符集需要多字节字符支持,请参见 第 12.13.3 节,“复杂字符集的多字节字符支持”。If the character set requires multibyte character support, see Section 12.13.3, “Multi-Byte Character Support for Complex Character Sets”.
修改配置信息。使用现有的配置信息作为添加
MYSYS信息的指南。这里的示例假设字符集有默认和二进制排序规则,但如果MYSET有额外的排序规则,则需要更多行。Modify the configuration information. Use the existing configuration information as a guide to adding information forMYSYS. The example here assumes that the character set has default and binary collations, but more lines are needed ifMYSEThas additional collations.编辑
mysys/charset-def.c,并为新的字符集 “注册” 排序规则。Editmysys/charset-def.c, and “register” the collations for the new character set.将这些行添加到 “声明” 部分:Add these lines to the “declaration” section:
#ifdef HAVE_CHARSET_
MYSETextern 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_
MYSETadd_compiled_collation(&my_charset_MYSET_general_ci); add_compiled_collation(&my_charset_MYSET_bin); #endif如果字符集使用
ctype-,编辑MYSET.cstrings/CMakeLists.txt并将ctype-添加到MYSET.cSTRINGS_SOURCES变量的定义中。If the character set usesctype-, editMYSET.cstrings/CMakeLists.txtand addctype-to the definition of theMYSET.cSTRINGS_SOURCESvariable.编辑
cmake/character_sets.cmake:Editcmake/character_sets.cmake:将
MYSET按字母顺序添加到CHARSETS_AVAILABLE的值中。AddMYSETto the value of withCHARSETS_AVAILABLEin alphabetic order.将
MYSET按字母顺序添加到CHARSETS_COMPLEX的值中。即使是简单字符集,也需要这样做,以便 CMake 能识别-DDEFAULT_CHARSET=。AddMYSETMYSETto the value ofCHARSETS_COMPLEXin alphabetic order. This is needed even for simple character sets, so that CMake can recognize-DDEFAULT_CHARSET=.MYSET
重新配置,重新编译,并进行测试。Reconfigure, recompile, and test.