[CONC-462] mysql_optionsv option MYSQL_SET_CHARSET_NAME does not work for auto detection (MYSQL_AUTODETECT_CHARSET_NAME) Created: 2020-03-12  Updated: 2020-03-12  Resolved: 2020-03-12

Status: Closed
Project: MariaDB Connector/C
Component/s: None
Affects Version/s: 3.1.7
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: David Ritter Assignee: Georg Richter
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

Windows



 Description   

With MySQL 6.1.x a call to mysql_options with the attribute MYSQL_SET_CHARSET_NAME and the value MYSQL_AUTODETECT_CHARSET_NAME would result in the connection correctly identifying the client character set.

When using the MariaDB C client 3.1.7 it appears this option is ignored and the client character set remains set to the server character set value.

Test Case

#include <stdio.h>
#include <mysql.h>
 
#include <windows.h>
 
void
checkClientCharacterSet(MYSQL* mysql) {
    mysql_query(mysql, "SHOW SESSION VARIABLES LIKE 'character_set_client'");
    MYSQL_RES* result = mysql_store_result(mysql);
    int totalrows = mysql_num_rows(result);
    int numfields = mysql_num_fields(result);
    MYSQL_ROW row = mysql_fetch_row(result);
    fprintf(stdout, "%s \t %s\n", row[0], row[1]);
}
 
int
main(int argc, char *argv[]) {
    if (argc <2) {
        fprintf(stderr, "Please enter server host name\n");
        return 1;
    }
    // Set to Windows UTF-8 code page
    // Note without this using the default code page of 437 the character_set_client will return 'latin1'
    SetConsoleCP(65001);
 
    mysql_library_init(0, NULL, NULL);
 
    MYSQL* mysql_ = mysql_init(NULL);
 
    //mysql_optionsv(mysql_, MYSQL_SET_CHARSET_NAME, "utf8"); // This works
    mysql_optionsv(mysql_, MYSQL_SET_CHARSET_NAME, MYSQL_AUTODETECT_CHARSET_NAME);
 
    if(!mysql_real_connect(mysql_, argv[1], "user", "pass", "db", 3306, NULL, 0)) {
        fprintf(stderr, "Connection to %s failed\n", argv[1]);
        mysql_close(mysql_);
        return 1;
    }
 
    checkClientCharacterSet(mysql_);
 
    mysql_close(mysql_);
    mysql_library_end();
 
    return 0;
}

Output

MySQL 6.1.x

  • The following output is observed (when the code page is left to the default of 437):

    character_set_client    latin1
     

  • The following output is observed (when setting the code page to 65001):

    character_set_client    utf8
    

MariaDB 3.1.7

  • The following output is observed (when the code page is left to the default of 437):

    character_set_client    latin1
     

  • The following output is observed (when setting the code page to 65001):

    character_set_client    latin1
    



 Comments   
Comment by Georg Richter [ 2020-03-12 ]

The correct name for the character set is "auto", but not "MYSQL_AUTODETECT_CHARSET_NAME" - this is valid for MySQL and MariaDB:

In the sources of MySQL you will find:

./include/mysql_com.h:#define MYSQL_AUTODETECT_CHARSET_NAME "auto"

Your example seems to work with MySQL, since if the specified character set could not be found, the default character set will be used. For MySQL it's utf8mb4, for MariaDB it's latin1.

Feel free to file a bug on bugs.mysql.com, since the documentation is wrong or misleading.

Comment by David Ritter [ 2020-03-12 ]

My apologies. That is a typo on my part. I had been using the macro MYSQL_AUTODETECT_CHARSET_NAME and then switched to the string "auto" and when I posted the example forgot to remove the quotes when I switched it back. (I have updated the example accordingly)

The output I pasted is from using the actual macro value and not the string "MYSQL_AUTODETECT_CHARSET_NAME"

Additionally I am using a MySQL 5.7 server where latin1 was still the default character set.

Do you still feel MySQL is in error?

Comment by David Ritter [ 2020-03-12 ]

I have found the change in Connector/C version 3.0 that change the behavior.
https://github.com/mariadb-corporation/mariadb-connector-c/commit/9a50a7d48164d42dafadfed5f01cc9b9dae6a882#diff-e93c0a52c99e14ccfee84638fe2db9d3

Generated at Thu Feb 08 03:05:29 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.