summaryrefslogtreecommitdiffstats
path: root/src/plugins/sqldrivers/mysql/qsql_mysql.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-10-14 08:45:55 -0700
committerThiago Macieira <thiago.macieira@intel.com>2021-10-19 07:55:55 -0700
commit9ecf72704d752b3b5eec7364652266a09a16c728 (patch)
tree5c3afd7e33405904c5bb727dbf6f5371828b445b /src/plugins/sqldrivers/mysql/qsql_mysql.cpp
parent11f5c07c1b312d70197bbada3842d72c1c97c5ff (diff)
MySQL: only set the charset if the connection has succeeded
No point otherwise. But do it before trying to select the database, in case the database name has non-US-ASCII characters. Task-number: QTBUG-97054 Pick-to: 6.2 6.2.1 Change-Id: Iea05060bc2c046928536fffd16adf036367b07bb Reviewed-by: Liang Qi <liang.qi@qt.io>
Diffstat (limited to 'src/plugins/sqldrivers/mysql/qsql_mysql.cpp')
-rw-r--r--src/plugins/sqldrivers/mysql/qsql_mysql.cpp27
1 files changed, 14 insertions, 13 deletions
diff --git a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp
index 899689af11..caf406da46 100644
--- a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp
+++ b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp
@@ -1304,6 +1304,15 @@ bool QMYSQLDriver::open(const QString& db,
unixSocket.isNull() ? nullptr : unixSocket.toUtf8().constData(),
optionFlags);
+ if (mysql != d->mysql) {
+ setLastError(qMakeError(tr("Unable to connect"),
+ QSqlError::ConnectionError, d));
+ mysql_close(d->mysql);
+ d->mysql = nullptr;
+ setOpenError(true);
+ return false;
+ }
+
// now ask the server to match the charset we selected
if (!cs || mysql_set_character_set(d->mysql, cs->csname) != 0) {
bool ok = false;
@@ -1319,24 +1328,16 @@ bool QMYSQLDriver::open(const QString& db,
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));
- mysql_close(d->mysql);
- setOpenError(true);
- return false;
- }
- if (reconnect)
- mysql_options(d->mysql, MYSQL_OPT_RECONNECT, &reconnect);
- } else {
- setLastError(qMakeError(tr("Unable to connect"),
- QSqlError::ConnectionError, d));
+ if (!db.isEmpty() && mysql_select_db(d->mysql, db.toUtf8().constData())) {
+ setLastError(qMakeError(tr("Unable to open database '%1'").arg(db), QSqlError::ConnectionError, d));
mysql_close(d->mysql);
- d->mysql = nullptr;
setOpenError(true);
return false;
}
+ if (reconnect)
+ mysql_options(d->mysql, MYSQL_OPT_RECONNECT, &reconnect);
+
d->preparedQuerysEnabled = checkPreparedQueries(d->mysql);
#if QT_CONFIG(thread)