summaryrefslogtreecommitdiffstats
path: root/src/sql
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2022-02-16 11:51:19 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2022-02-21 20:37:28 +0100
commitdcd87049bb9e810ce4bd8343e93af3f8cea6aaf5 (patch)
treee297429bb7855d3c039cee6a617fd39504382949 /src/sql
parentd2ceb2e353304aeb91db4982afd8adeefdae3c3b (diff)
Treat invalid Q(Date)?Time as null when used as an SQL value
In Qt 5, QVariant::isNull() was true when the contained object had an isNull() that was true; this is no longer true in Qt 6, so we now have QSqlResultPrivate::isVariantNull() to test is-or-contains null. However, for date-times, QSqlDriver::formatValue() treats invalid QDateTime as NULL, since it needs its toString(Qt::ISODate), which will be empty if invalid. As QDateTime's isValid() is more stringent than its !isNull(), this can lead to one that's neither valid nor null, such as QDateTime(QDate(), QTime(0, 0)), producing an invalid entry in a database. Do the same for QTime, as its isValid() is more stringent than !isNull(), and its toString() likewise returns empty on invalid (although it's not clear it's possible to construct one that's neither valid nor null). For QDate valid and null are simply complementary. [ChangeLog][QSql] Handling of QDateTime and QTime values passed to SQL now consistently treats invalid as null. Some values of these types are neither valid nor null, which could lead to invalid data being given to the SQL database. Invalid values are now treated as null to prevent this. Task-number: QTBUG-98471 Pick-to: 6.3 Change-Id: I145411280d6bcc53dc0dc5f4a1cb938d995fd6bc Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/sql')
-rw-r--r--src/sql/kernel/qsqlresult.cpp6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/sql/kernel/qsqlresult.cpp b/src/sql/kernel/qsqlresult.cpp
index 568520cd5c..cdf1479e64 100644
--- a/src/sql/kernel/qsqlresult.cpp
+++ b/src/sql/kernel/qsqlresult.cpp
@@ -632,11 +632,13 @@ bool QSqlResultPrivate::isVariantNull(const QVariant &variant)
case qMetaTypeId<QByteArray>():
return static_cast<const QByteArray*>(variant.constData())->isNull();
case qMetaTypeId<QDateTime>():
- return static_cast<const QDateTime*>(variant.constData())->isNull();
+ // We treat invalid date-time as null, since its ISODate would be empty.
+ return !static_cast<const QDateTime*>(variant.constData())->isValid();
case qMetaTypeId<QDate>():
return static_cast<const QDate*>(variant.constData())->isNull();
case qMetaTypeId<QTime>():
- return static_cast<const QTime*>(variant.constData())->isNull();
+ // As for QDateTime, QTime can be invalid without being null.
+ return !static_cast<const QTime*>(variant.constData())->isValid();
case qMetaTypeId<QUuid>():
return static_cast<const QUuid*>(variant.constData())->isNull();
default: