diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-08-09 13:31:48 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2021-08-10 10:43:03 -0700 |
commit | 472520afb9081856a2556c7df221c084a42a2d42 (patch) | |
tree | 3cc5362ab9447c50721ae2ea1ffe1078d205060c /src/plugins/sqldrivers | |
parent | aa93b4835a966be021a8f8a4d1872a7fd606c5d4 (diff) |
MySQL: properly fix setting the character set
Commit e4bd73dc54542fe16121825c2a369b7f863e0de8 moved the
mysql_set_character_set() call above the mysql_real_connect() but that
doesn't actually work, as there's no connection to send the "SET NAMES"
statement on.
So do it in two steps: first, by setting the charset in the MYSQL
structure, then by asking the server to match.
Pick-to: 6.2
Task-number: QTBUG-55444
Change-Id: I4a40ccbd3321467a8429fffd1699bd829f342124
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'src/plugins/sqldrivers')
-rw-r--r-- | src/plugins/sqldrivers/mysql/qsql_mysql.cpp | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp index ed304d807d..250b39c930 100644 --- a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp +++ b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp @@ -1235,9 +1235,22 @@ bool QMYSQLDriver::open(const QString& db, } // try utf8 with non BMP first, utf8 (BMP only) if that fails - if (mysql_set_character_set(d->mysql, "utf8mb4")) - if (mysql_set_character_set(d->mysql, "utf8")) - qWarning() << "MySQL: Unable to set the client character set to utf8."; + static const char wanted_charsets[][8] = { "utf8mb4", "utf8" }; +#ifdef MARIADB_VERSION_ID + MARIADB_CHARSET_INFO *cs = nullptr; + for (const char *p : wanted_charsets) { + cs = mariadb_get_charset_by_name(p); + if (cs) { + d->mysql->charset = cs; + break; + } + } +#else + // dummy + struct { + const char *csname; + } *cs = nullptr; +#endif if (!sslKey.isNull() || !sslCert.isNull() || !sslCA.isNull() || !sslCAPath.isNull() || !sslCipher.isNull()) { @@ -1265,6 +1278,21 @@ bool QMYSQLDriver::open(const QString& db, unixSocket.isNull() ? nullptr : unixSocket.toUtf8().constData(), optionFlags); + // now ask the server to match the charset we selected + if (!cs || mysql_set_character_set(d->mysql, cs->csname)) { + bool ok = false; + for (const char *p : wanted_charsets) { + if (mysql_set_character_set(d->mysql, p)) { + ok = true; + break; + } + } + if (!ok) + qWarning("MySQL: Unable to set the client character set to utf8 (\"%s\"). Using '%s' instead.", + mysql_error(d->mysql), + mysql_character_set_name(d->mysql)); + } + if (mysql == d->mysql) { if (!db.isEmpty() && mysql_select_db(d->mysql, db.toUtf8().constData())) { setLastError(qMakeError(tr("Unable to open database '%1'").arg(db), QSqlError::ConnectionError, d)); |