summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-08-09 13:31:48 -0700
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-08-10 19:34:15 +0000
commit11e5f1fca6b5229a00ad54abb29b8213e4e54a8f (patch)
treea51a9239ddc0d96256ea3cc1db8cd67bfcd30204
parentc11a4d34321e2d615c22aa068dbb185656e9d6c9 (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. Task-number: QTBUG-55444 Change-Id: I4a40ccbd3321467a8429fffd1699bd829f342124 Reviewed-by: Andy Shaw <andy.shaw@qt.io> (cherry picked from commit 472520afb9081856a2556c7df221c084a42a2d42) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/plugins/sqldrivers/mysql/qsql_mysql.cpp34
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 ea8a15ebfb..289f91a961 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));