14.2. Alternative Character Sets
A character set defines the basic alphabet supported by a database. An encoding then maps the character set to the 1s and 0s understood by a computer. MySQL comes with support for a variety of character sets and encodings. Unfortunately, this support is very peculiar. MySQL ties the concepts of character set, encoding, and sorting rules together so that these three normally distinct concepts cannot be separated. While this scheme works well if you use one of the supported character sets with the built-in sorting rules, you may need to go to some extra lengths if the character sets that come with MySQL do not meet your needs.
First, you need to determine whether the character set needs special string collating routines for sorting and whether it needs multi-byte character support. If it needs either of these, it will need to be compiled in. Otherwise, support for the character set can be added by simply modifying the configuration.
14.2.1. Simple Character Sets
Additional character sets may be configured into MySQL if they don't require multi-byte character support or string collating routines. Adding a character set through configuration requires the following steps:
$ cat sql/share/charsets/Index # sql/share/charsets/Index # # This file lists all of the available character sets. Please keep this # file sorted by character set number. big5 1 czech 2 dec8 3 . . . latin5 30 latin1_de 31
To add a new character set, simply add the character set to the end of the file with a unique index:
latin5 30 latin1_de 31 elvish 32
The next step is to create a configuration file in sql/share/charsets for your character set. You can base it on sql/share/charsets/latin1.conf.
$ cd sql/share/charsets $ cp latin1.conf elvish.conf $ vi elvish.conf
Once you have configured your character set, you are ready to compile MySQL to include it. Before recompiling MySQL, you need to edit configure.in and add your new character set to CHARSETS_AVAILABLE:
CHARSETS_AVAILABLE="big5 cp1251 cp1257 croat czech danish dec8 dos estonia euc_kr gb2312 gbk german1 greek hebrew hp8 hungarian koi8_ru koi8_ukr latin1 latin1_de latin2 latin5 sjis swe7 tis620 ujis usa7 win1250 win1251ukr elvish"
$ make $ make install
14.2.2. Complex Character Sets
Character sets requiring special string collating routines for sorting or multi-byte character support will need custom work a configuration file simply cannot handle. To accomplish this task, you will need to be familiar with C programming, as you will need to create several C source files. If you plan to attempt this, it is highly recommended that you study the existing ctype-*.c files and base yours on these.
As with simple character sets, you need to edit the sql/share/charsets/Index and configure.in files. Instead of a configuration file, however, you need to create a C source file called ctype-charset.c. In our case, this file will be ctype-elvish.c. You can get a head start with your character set file by copying the source file for a similar character set.
The first task in this source file is to define ctype, to_lower, to_upper, and sort_order. These are the same arrays you configured in the previous section, except they are defined as C arrays. You'll need to create:
For our example, we need to create ctype_elvish, to_upper_elvish, to_lower_elvish, and sort_order_elvish.
If you need string collating functions, you must write your own custom string collating functions. These functions should be named:
We therefore need to create my_strnncoll_elvish, my_strnxfrm_elvish, my_strcoll_elvish, my_strxfrm_elvish, and my_like_range_elvish.
Multi-byte support requires the coding of special multi-byte functions:
We need to create ismbchar_elvish, ismbhead_elvish, and mbcharlen_elvish.
With your coding complete, you need to create a comment header for your ctype file. This header should look like this:
/* * This comment is parsed by configure to create ctype.c, * so don't change it unless you know what you are doing. * * .configure. number_charset=[character set index number from Index file] * .configure. strxfrm_multiply_charset=[max ratio that strings may grow * during my_strxfrm_charset - a * positive integer] * .configure. mbmaxlen_charset=[size in bytes of largest char in set] */
For our example, this might look like:
* .configure. number_elvish=32 * .configure. strxfrm_multiply_elvish=1 * .configure. mbmaxlen_elvish=2
Copyright © 2003 O'Reilly & Associates. All rights reserved.