From 67f04fa060be1b75b8ea60ac6eb45d1577d1fdaf Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 15 Jun 2020 09:31:31 +0200 Subject: Deprecate QVariant::Type uses in QSqlField Add metaType()/setMetaType() methods to be used instead of the type() methods taking a QVariant::Type. Change-Id: Ieaba35b73f8061cd83288dd6b50d58322db3c7ed Reviewed-by: Maurice Kalinowski --- src/sql/kernel/qsqldriver.cpp | 2 +- src/sql/kernel/qsqlfield.cpp | 79 +++++++++++++++++++++++++++++++++---------- src/sql/kernel/qsqlfield.h | 27 +++++++++++---- src/sql/kernel/qsqlresult.cpp | 4 +-- 4 files changed, 86 insertions(+), 26 deletions(-) (limited to 'src/sql') diff --git a/src/sql/kernel/qsqldriver.cpp b/src/sql/kernel/qsqldriver.cpp index 02371a209b..369330a951 100644 --- a/src/sql/kernel/qsqldriver.cpp +++ b/src/sql/kernel/qsqldriver.cpp @@ -599,7 +599,7 @@ QString QSqlDriver::formatValue(const QSqlField &field, bool trimStrings) const if (field.isNull()) r = nullTxt; else { - switch (+field.type()) { + switch (field.metaType().id()) { case QMetaType::Int: case QMetaType::UInt: if (field.value().userType() == QMetaType::Bool) diff --git a/src/sql/kernel/qsqlfield.cpp b/src/sql/kernel/qsqlfield.cpp index 86a22bae10..5a3d0a1a23 100644 --- a/src/sql/kernel/qsqlfield.cpp +++ b/src/sql/kernel/qsqlfield.cpp @@ -47,8 +47,8 @@ class QSqlFieldPrivate { public: QSqlFieldPrivate(const QString &name, - QVariant::Type type, const QString &tableName) : - ref(1), nm(name), table(tableName), def(QVariant()), type(QMetaType::Type(type)), + QMetaType type, const QString &tableName) : + ref(1), nm(name), table(tableName), def(QVariant()), type(type), req(QSqlField::Unknown), len(-1), prec(-1), tp(-1), ro(false), gen(true), autoval(false) {} @@ -86,7 +86,7 @@ public: QString nm; QString table; QVariant def; - QMetaType::Type type; + QMetaType type; QSqlField::RequiredStatus req; int len; int prec; @@ -155,30 +155,53 @@ public: */ /*! + \fn QSqlField::QSqlField(const QString &fieldName, QVariant::Type type) + Constructs an empty field called \a fieldName of variant type \a type. \sa setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly() */ -QSqlField::QSqlField(const QString &fieldName, QVariant::Type type) + +/*! + \fn QSqlField::QSqlField(const QString &fieldName, QVariant::Type type, const QString &table) + + \overload + Constructs an empty field called \a fieldName of variant type \a + type in \a table. + + \sa setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), + setGenerated(), setReadOnly() +*/ + +/*! + \fn QSqlField::QSqlField(const QString &fieldName, QVariant::Type type) + + Constructs an empty field called \a fieldName of type \a type. + + \sa setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), + setGenerated(), setReadOnly() +*/ +QSqlField::QSqlField(const QString &fieldName, QMetaType type) { d = new QSqlFieldPrivate(fieldName, type, QString()); - val = QVariant(type); + val = QVariant(QMetaType(type), nullptr); } /*! + \fn QSqlField::QSqlField(const QString &fieldName, QVariant::Type type, const QString &table) + \overload - Constructs an empty field called \a fieldName of variant type \a + Constructs an empty field called \a fieldName of type \a type in \a table. \sa setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly() */ -QSqlField::QSqlField(const QString &fieldName, QVariant::Type type, - const QString &table) +QSqlField::QSqlField(const QString &fieldName, QMetaType type, const QString &table) { d = new QSqlFieldPrivate(fieldName, type, table); - val = QVariant(type); + val = QVariant(QMetaType(type), nullptr); } /*! @@ -344,7 +367,7 @@ void QSqlField::clear() { if (isReadOnly()) return; - val = QVariant(type()); + val = QVariant(d->type, nullptr); } /*! @@ -397,9 +420,9 @@ QString QSqlField::name() const \sa setType() */ -QVariant::Type QSqlField::type() const +QMetaType QSqlField::metaType() const { - return QVariant::Type(d->type); + return d->type; } /*! @@ -408,14 +431,36 @@ QVariant::Type QSqlField::type() const \sa type(), setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly() */ -void QSqlField::setType(QVariant::Type type) +void QSqlField::setMetaType(QMetaType type) { detach(); - d->type = QMetaType::Type(type); + d->type = type; if (!val.isValid()) - val = QVariant(type); + val = QVariant(type, nullptr); } +/*! + \fn QVariant::Type QSqlField::type() const + \obsolete Use metaType() instead. + + Returns the field's type as stored in the database. + Note that the actual value might have a different type, + Numerical values that are too large to store in a long + int or double are usually stored as strings to prevent + precision loss. + + \sa metaType() +*/ + +/*! + \fn void QSqlField::setType(QVariant::Type type) + \obsolete Use setMetaType() instead. + + Set's the field's variant type to \a type. + + \sa setMetaType() +*/ + /*! Returns \c true if the field's value is read-only; otherwise returns false. @@ -525,7 +570,7 @@ bool QSqlField::isGenerated() const */ bool QSqlField::isValid() const { - return d->type != QMetaType::UnknownType; + return d->type.isValid(); } #ifndef QT_NO_DEBUG_STREAM @@ -533,7 +578,7 @@ QDebug operator<<(QDebug dbg, const QSqlField &f) { QDebugStateSaver saver(dbg); dbg.nospace(); - dbg << "QSqlField(" << f.name() << ", " << QMetaType::typeName(f.type()); + dbg << "QSqlField(" << f.name() << ", " << f.metaType().name(); dbg << ", tableName: " << (f.tableName().isEmpty() ? QStringLiteral("(not specified)") : f.tableName()); if (f.length() >= 0) dbg << ", length: " << f.length(); diff --git a/src/sql/kernel/qsqlfield.h b/src/sql/kernel/qsqlfield.h index 892420d26e..c4acc6a22c 100644 --- a/src/sql/kernel/qsqlfield.h +++ b/src/sql/kernel/qsqlfield.h @@ -54,10 +54,19 @@ class Q_SQL_EXPORT QSqlField public: enum RequiredStatus { Unknown = -1, Optional = 0, Required = 1 }; - explicit QSqlField(const QString& fieldName = QString(), - QVariant::Type type = {}); - QSqlField(const QString &fieldName, QVariant::Type type, - const QString &tableName); +#if QT_DEPRECATED_SINCE(6,0) + QT_DEPRECATED_VERSION_X_6_0("Use the constructor using a QMetaType instead") + explicit QSqlField(const QString& fieldName, QVariant::Type type) + : QSqlField(fieldName, QMetaType(type)) + {} + QT_DEPRECATED_VERSION_X_6_0("Use the constructor using a QMetaType instead") + QSqlField(const QString &fieldName, QVariant::Type type, const QString &tableName) + : QSqlField(fieldName, QMetaType(type), tableName) + {} +#endif + + explicit QSqlField(const QString& fieldName = QString(), QMetaType type = QMetaType()); + QSqlField(const QString &fieldName, QMetaType type, const QString &tableName); QSqlField(const QSqlField& other); QSqlField& operator=(const QSqlField& other); @@ -76,10 +85,16 @@ public: void setReadOnly(bool readOnly); bool isReadOnly() const; void clear(); - QVariant::Type type() const; bool isAutoValue() const; - void setType(QVariant::Type type); + QMetaType metaType() const; + void setMetaType(QMetaType type); +#if QT_DEPRECATED_SINCE(6,0) + QT_DEPRECATED_VERSION_X_6_0("Use metaType() instead") + QVariant::Type type() const { return QVariant::Type(metaType().id()); }; + QT_DEPRECATED_VERSION_X_6_0("Use setMetaType() instead") + void setType(QVariant::Type type) { setMetaType(QMetaType(int(type))); } +#endif void setRequiredStatus(RequiredStatus status); inline void setRequired(bool required) { setRequiredStatus(required ? Required : Optional); } diff --git a/src/sql/kernel/qsqlresult.cpp b/src/sql/kernel/qsqlresult.cpp index f3ab4c10cf..24ca1ffeda 100644 --- a/src/sql/kernel/qsqlresult.cpp +++ b/src/sql/kernel/qsqlresult.cpp @@ -631,7 +631,7 @@ bool QSqlResult::exec() for (i = d->holders.count() - 1; i >= 0; --i) { holder = d->holders.at(i).holderName; val = d->values.value(d->indexes.value(holder).value(0,-1)); - QSqlField f(QLatin1String(""), QVariant::Type(val.userType())); + QSqlField f(QLatin1String(""), val.metaType()); f.setValue(val); query = query.replace(d->holders.at(i).holderPos, holder.length(), driver()->formatValue(f)); @@ -645,7 +645,7 @@ bool QSqlResult::exec() if (i == -1) continue; QVariant var = d->values.value(idx); - QSqlField f(QLatin1String(""), QVariant::Type(var.userType())); + QSqlField f(QLatin1String(""), var.metaType()); if (var.isNull()) f.clear(); else -- cgit v1.2.3