Bug #7891 mysql_character_set_name() returns collation
Submitted: 14 Jan 2005 2:04 Modified: 17 Jan 2005 5:36
Reporter: Andy Dustman Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.1.8 OS:Any (any)
Assigned to: Alexander Barkov CPU Architecture:Any

[14 Jan 2005 2:04] Andy Dustman
Description:
Documentation states, "Returns the default character set for the current connection." 4.0 behaves as expected, but 4.1.8 returns the collation instead.

How to repeat:
Example with Python API (MySQL-python, aka MySQLdb):

>>> # MySQL-4.0.22
>>> import MySQLdb
>>> db=MySQLdb.connect(read_default_file="~/.my.cnf")
>>> db.character_set_name()
'latin1'

>>> # MySQL-4.1.8
>>> import MySQLdb
>>> db=MySQLdb.connect(read_default_file="~/.my.cnf")
>>> db.character_set_name()
'latin1_swedish_ci'

Suggested fix:
I'd hate to suggest a new API function, i.e. mysql_collation_name() or something like that.

One option would be to update the documentation to reflect the true behavior.

Perhaps this function should be deprecated in favor of using queries to access server variables, but I wonder if that doesn't create a chicken-and-egg problem, since you are executing SQL to find the character set name, but you don't know what character set the result is on. In practice I don't think it is a problem, but I don't do a lot with Unicode.
[14 Jan 2005 3:08] Andy Dustman
C test case:

#include <mysql.h>
#include <stdio.h>
int main(int argc, char **argv) {
    MYSQL db;
    mysql_init(&db);
    mysql_options(&db, MYSQL_READ_DEFAULT_FILE, "~/.my.cnf");
    printf("%s\n", argv[1]);
    mysql_real_connect(&db, argv[1], NULL, NULL, NULL, 0, NULL, 0);
    printf("%s\n", mysql_get_client_info());
    printf("%s\n", mysql_get_server_info(&db));
    printf("%s\n", mysql_character_set_name(&db));
}

10.0.0.2 has MySQL-4.0.22
10.0.0.4 has MySQL-4.1.8

Results on 10.0.0.4:

$ ./charset 127.0.0.1
127.0.0.1
4.1.8
4.1.8
latin1_swedish_ci
$ ./charset 10.0.0.2
10.0.0.2
4.1.8
4.0.22
latin1_swedish_ci

Results on 10.0.0.2:

$ ./charset 127.0.0.1
127.0.0.1
4.0.22
4.0.22
latin1
$ ./charset 10.0.0.4
10.0.0.4
4.0.22
4.1.8
latin1

From this we can conclude that the 4.0 client library returns the character set name and the 4.1 client library returns the collation name; the behavior is not tied to the server version.
[17 Jan 2005 5:36] Alexander Barkov
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

fixed in 4.1.9 and 5.0.3