From 3f1e8d85cce8e8385476f189fb0a129bde7b8d7d Mon Sep 17 00:00:00 2001 From: Philip Seeger Date: Mon, 6 Jun 2016 23:26:02 +0200 Subject: MySQL: Use charset utf8mb4 to allow 4-byte characters In MySQL, the character set named utf8 uses a maximum of 3 bytes per character and contains only BMP characters. It does not support supplementary characters. In version 5.5.3, a new UTF-8 character set called utf8mb4 has been introduced, which supports 4-byte characters. [ChangeLog][QtSql][QSqlDatabase] When connecting to a MySQL server whose version is 5.5.3 or higher, the default connection charset is now utf8mb4 instead of utf8 to allow 4-byte UTF-8 encodings. Change-Id: I718bd23212afd67367b39d4ce7da2a99ae0f1cca Reviewed-by: Oswald Buddenhagen Reviewed-by: Liang Qi Reviewed-by: Andy Shaw --- src/plugins/sqldrivers/mysql/qsql_mysql.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/plugins/sqldrivers/mysql') diff --git a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp index b13e10e3a5..30a0a04f44 100644 --- a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp +++ b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp @@ -1414,8 +1414,13 @@ bool QMYSQLDriver::open(const QString& db, } #if (MYSQL_VERSION_ID >= 40113 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50007 - // force the communication to be utf8 - mysql_set_character_set(d->mysql, "utf8"); + if (mysql_get_client_version() >= 50503 && mysql_get_server_version(d->mysql) >= 50503) { + // force the communication to be utf8mb4 (only utf8mb4 supports 4-byte characters) + mysql_set_character_set(d->mysql, "utf8mb4"); + } else { + // force the communication to be utf8 + mysql_set_character_set(d->mysql, "utf8"); + } #endif #ifndef QT_NO_TEXTCODEC d->tc = codec(d->mysql); -- cgit v1.2.3