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>
bb10
Edward Welbourne 2022-02-16 11:51:19 +01:00
parent d2ceb2e353
commit dcd87049bb
1 changed files with 4 additions and 2 deletions

View File

@ -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: