SQL/PostgreSQL: cleanup usage of QT_CONFIG(datestring)

Creating a QString from a QDate/QTime works even when
QT_CONFIG(datestring) is not defined, so no need to ifdef it out.

Pick-to: 6.7
Change-Id: Ib3594036f309393b612d3fbf21f51be9c36a9391
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Christian Ehrlicher 2024-03-13 19:56:04 +01:00
parent f76af5f78c
commit f813b76fb2

View File

@ -641,23 +641,18 @@ QVariant QPSQLResult::data(int i)
} }
return dbl; return dbl;
} }
#if QT_CONFIG(datestring)
case QMetaType::QDate: case QMetaType::QDate:
#if QT_CONFIG(datestring)
return QVariant(QDate::fromString(QString::fromLatin1(val), Qt::ISODate)); return QVariant(QDate::fromString(QString::fromLatin1(val), Qt::ISODate));
#else
return QVariant(QString::fromLatin1(val));
#endif
case QMetaType::QTime: case QMetaType::QTime:
#if QT_CONFIG(datestring)
return QVariant(QTime::fromString(QString::fromLatin1(val), Qt::ISODate)); return QVariant(QTime::fromString(QString::fromLatin1(val), Qt::ISODate));
#else
return QVariant(QString::fromLatin1(val));
#endif
case QMetaType::QDateTime: case QMetaType::QDateTime:
#if QT_CONFIG(datestring)
return QVariant(QDateTime::fromString(QString::fromLatin1(val), return QVariant(QDateTime::fromString(QString::fromLatin1(val),
Qt::ISODate).toLocalTime()); Qt::ISODate).toLocalTime());
#else #else
case QMetaType::QDate:
case QMetaType::QTime:
case QMetaType::QDateTime:
return QVariant(QString::fromLatin1(val)); return QVariant(QString::fromLatin1(val));
#endif #endif
case QMetaType::QByteArray: { case QMetaType::QByteArray: {
@ -1437,32 +1432,28 @@ QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const
r = nullStr(); r = nullStr();
} else { } else {
switch (field.metaType().id()) { switch (field.metaType().id()) {
case QMetaType::QDateTime: case QMetaType::QDateTime: {
#if QT_CONFIG(datestring) const auto dt = field.value().toDateTime();
if (field.value().toDateTime().isValid()) { if (dt.isValid()) {
// we force the value to be considered with a timezone information, and we force it to be UTC // we force the value to be considered with a timezone information, and we force it to be UTC
// this is safe since postgresql stores only the UTC value and not the timezone offset (only used // this is safe since postgresql stores only the UTC value and not the timezone offset (only used
// while parsing), so we have correct behavior in both case of with timezone and without tz // while parsing), so we have correct behavior in both case of with timezone and without tz
r = QStringLiteral("TIMESTAMP WITH TIME ZONE ") + u'\'' + r = QStringLiteral("TIMESTAMP WITH TIME ZONE ") + u'\'' +
QLocale::c().toString(field.value().toDateTime().toUTC(), u"yyyy-MM-ddThh:mm:ss.zzz") + QLocale::c().toString(dt.toUTC(), u"yyyy-MM-ddThh:mm:ss.zzz") +
u'Z' + u'\''; u'Z' + u'\'';
} else { } else {
r = nullStr(); r = nullStr();
} }
#else
r = nullStr();
#endif // datestring
break; break;
case QMetaType::QTime: }
#if QT_CONFIG(datestring) case QMetaType::QTime: {
if (field.value().toTime().isValid()) { const auto t = field.value().toTime();
r = u'\'' + field.value().toTime().toString(u"hh:mm:ss.zzz") + u'\''; if (t.isValid())
} else r = u'\'' + QLocale::c().toString(t, u"hh:mm:ss.zzz") + u'\'';
#endif else
{
r = nullStr(); r = nullStr();
}
break; break;
}
case QMetaType::QString: case QMetaType::QString:
r = QSqlDriver::formatValue(field, trimStrings); r = QSqlDriver::formatValue(field, trimStrings);
if (d->hasBackslashEscape) if (d->hasBackslashEscape)