QSqlDatabase: fix historical mistake of defaultConnection being non-const

This was likely never intended, and was a mistake in the API design,
missing the second const after the pointer. It should have been:
    static const char *const defaultConnection;

This commit fixes that mistake by replacing the variable with a
constexpr one that can't be modified.

We can actually do better by moving the name to the header, so it will
get emitted in each library, application, or plugin instead of trying to
load from QtSql.

[ChangeLog][Potentially Source-Incompatible Changes] The static
QSqlDatabase::connectionName member variable has changed from a
non-const pointer to a constexpr array. Modifying the pointer was most
likely a mistake in code and would produce a misbehaving application.

Change-Id: Icf2eddc865c2d6dde0c7fffda06e55e215e5f26d
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Thiago Macieira 2025-01-26 10:04:21 -08:00
parent 03f6f176b1
commit 16aae5a4b8
3 changed files with 21 additions and 3 deletions

View File

@ -119,3 +119,15 @@ QVariant QSqlQuery::value(const QString &name) const
}
#endif // QT_SQL_REMOVED_SINCE(6, 8)
#if QT_SQL_REMOVED_SINCE(6, 10)
#include "qsqldatabase.h"
const char *QSqlDatabase::defaultConnection = QSqlDatabaseDefaultConnectionName::defaultConnection;
// #include <qotherheader.h>
// // implement removed functions from qotherheader.h
// order sections alphabetically to reduce chances of merge conflicts
#endif // QT_SQL_REMOVED_SINCE(6, 10)

View File

@ -36,8 +36,6 @@ using namespace Qt::StringLiterals;
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
(QSqlDriverFactoryInterface_iid, "/sqldrivers"_L1))
const char *QSqlDatabase::defaultConnection = "qt_sql_default_connection";
namespace {
struct QtSqlGlobals
{

View File

@ -34,7 +34,13 @@ public:
QSqlDriver *createObject() const override { return new T; }
};
class Q_SQL_EXPORT QSqlDatabase
struct QSqlDatabaseDefaultConnectionName
{
// separate class because of the static inline constexpr variable
static constexpr const char defaultConnection[] = "qt_sql_default_connection";
};
class Q_SQL_EXPORT QSqlDatabase : public QSqlDatabaseDefaultConnectionName
{
Q_GADGET
Q_PROPERTY(QSql::NumericalPrecisionPolicy numericalPrecisionPolicy READ numericalPrecisionPolicy WRITE setNumericalPrecisionPolicy)
@ -87,7 +93,9 @@ public:
QSqlDriver* driver() const;
#if QT_SQL_REMOVED_SINCE(6, 10)
static const char *defaultConnection;
#endif
static QSqlDatabase addDatabase(const QString& type,
const QString& connectionName = QLatin1StringView(defaultConnection));