From 6b096271cf0aac38d80f292f8b37edd671a8d17f Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 26 Jan 2024 23:21:27 +0100 Subject: [PATCH] QSqlRecord/QSqlQuery: Use QAnyStringView instead QStringView Change all functions taking a QStringView to take a QAnyStringView and remove all functions taking a const QStringRef since this can now be fully handled by the QAnyStringView ones. This amends f2dba1919427bcc0f510d7f60e3fafbd6f41430d and 993f31801446c1d851c7c8d54c9b55216acd0993 [ChangeLog][QtSql][QSqlRecord] All functions taking a QString were changed to take a QAnyStringView. Pick-to: 6.8 Change-Id: Ia1c968c4e2a7a93aa26d090ef6605271305c14a6 Reviewed-by: Volker Hilsheimer --- src/sql/compat/removed_api.cpp | 69 ++++++++++++ src/sql/kernel/qsqlquery.cpp | 20 +--- src/sql/kernel/qsqlquery.h | 8 +- src/sql/kernel/qsqlrecord.cpp | 101 +++--------------- src/sql/kernel/qsqlrecord.h | 36 +++++-- .../sql/kernel/qsqlrecord/tst_qsqlrecord.cpp | 22 +++- 6 files changed, 139 insertions(+), 117 deletions(-) diff --git a/src/sql/compat/removed_api.cpp b/src/sql/compat/removed_api.cpp index 9da60eceda0..2c2b353a3fe 100644 --- a/src/sql/compat/removed_api.cpp +++ b/src/sql/compat/removed_api.cpp @@ -50,3 +50,72 @@ QList &QSqlResult::boundValues() const } #endif // QT_SQL_REMOVED_SINCE(6, 6) + +#if QT_SQL_REMOVED_SINCE(6, 8) + +#include "qsqlrecord.h" +#include "qsqlfield.h" + +// #include +// // implement removed functions from qotherheader.h +// order sections alphabetically to reduce chances of merge conflicts + +bool QSqlRecord::contains(const QString &name) const +{ + return contains(QStringView(name)); +} + +QSqlField QSqlRecord::field(const QString &name) const +{ + return field(QStringView(name)); +} + +int QSqlRecord::indexOf(const QString &name) const +{ + return indexOf(QStringView(name)); +} + +bool QSqlRecord::isGenerated(const QString &name) const +{ + return isGenerated(QStringView(name)); +} + +bool QSqlRecord::isNull(const QString &name) const +{ + return isNull(QStringView(name)); +} + +void QSqlRecord::setGenerated(const QString &name, bool generated) +{ + setGenerated(QStringView(name), generated); +} + +void QSqlRecord::setNull(const QString &name) +{ + setNull(QStringView(name)); +} + +void QSqlRecord::setValue(const QString &name, const QVariant &val) +{ + setValue(QStringView(name), val); +} + +QVariant QSqlRecord::value(const QString &name) const +{ + return value(QStringView(name)); +} + + +#include "qsqlquery.h" + +bool QSqlQuery::isNull(const QString &name) const +{ + return isNull(QStringView(name)); +} + +QVariant QSqlQuery::value(const QString &name) const +{ + return value(QStringView(name)); +} + +#endif // QT_SQL_REMOVED_SINCE(6, 8) diff --git a/src/sql/kernel/qsqlquery.cpp b/src/sql/kernel/qsqlquery.cpp index 14a11165319..1ada7f9e27a 100644 --- a/src/sql/kernel/qsqlquery.cpp +++ b/src/sql/kernel/qsqlquery.cpp @@ -328,14 +328,6 @@ bool QSqlQuery::isNull(int field) const || d->sqlResult->isNull(field); } -/*! - \overload -*/ -bool QSqlQuery::isNull(const QString &name) const -{ - return isNull(QStringView(name)); -} - /*! \overload @@ -344,7 +336,7 @@ bool QSqlQuery::isNull(const QString &name) const This overload is less efficient than \l{QSqlQuery::}{isNull()} */ -bool QSqlQuery::isNull(QStringView name) const +bool QSqlQuery::isNull(QAnyStringView name) const { qsizetype index = d->sqlResult->record().indexOf(name); if (index > -1) @@ -447,14 +439,6 @@ QVariant QSqlQuery::value(int index) const return QVariant(); } -/*! - \overload -*/ -QVariant QSqlQuery::value(const QString &name) const -{ - return value(QStringView(name)); -} - /*! \overload @@ -463,7 +447,7 @@ QVariant QSqlQuery::value(const QString &name) const This overload is less efficient than \l{QSqlQuery::}{value()} */ -QVariant QSqlQuery::value(QStringView name) const +QVariant QSqlQuery::value(QAnyStringView name) const { qsizetype index = d->sqlResult->record().indexOf(name); if (index > -1) diff --git a/src/sql/kernel/qsqlquery.h b/src/sql/kernel/qsqlquery.h index 244b0262050..bd3fea9e3e4 100644 --- a/src/sql/kernel/qsqlquery.h +++ b/src/sql/kernel/qsqlquery.h @@ -55,8 +55,10 @@ public: bool isValid() const; bool isActive() const; bool isNull(int field) const; +#if QT_SQL_REMOVED_SINCE(6, 8) bool isNull(const QString &name) const; - bool isNull(QStringView name) const; +#endif + bool isNull(QAnyStringView name) const; int at() const; QString lastQuery() const; int numRowsAffected() const; @@ -71,8 +73,10 @@ public: void setForwardOnly(bool forward); bool exec(const QString& query); QVariant value(int i) const; +#if QT_SQL_REMOVED_SINCE(6, 8) QVariant value(const QString &name) const; - QVariant value(QStringView name) const; +#endif + QVariant value(QAnyStringView name) const; void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy); QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const; diff --git a/src/sql/kernel/qsqlrecord.cpp b/src/sql/kernel/qsqlrecord.cpp index 138f19e1cbc..89af1a52fe8 100644 --- a/src/sql/kernel/qsqlrecord.cpp +++ b/src/sql/kernel/qsqlrecord.cpp @@ -19,10 +19,11 @@ public: return index >= 0 && index < fields.size(); } - qsizetype indexOfImpl(QStringView name) + template + qsizetype indexOfImpl(T name) { - QStringView tableName; - QStringView fieldName; + T tableName; + T fieldName; const auto it = std::find(name.begin(), name.end(), u'.'); const auto idx = (it == name.end()) ? -1 : it - name.begin(); if (idx != -1) { @@ -186,14 +187,6 @@ QVariant QSqlRecord::value(int index) const return d->fields.value(index).value(); } -/*! - \overload -*/ -QVariant QSqlRecord::value(const QString &name) const -{ - return value(QStringView(name)); -} - /*! \overload @@ -202,7 +195,7 @@ QVariant QSqlRecord::value(const QString &name) const \sa indexOf(), isNull() */ -QVariant QSqlRecord::value(QStringView name) const +QVariant QSqlRecord::value(QAnyStringView name) const { return value(indexOf(name)); } @@ -219,14 +212,6 @@ QString QSqlRecord::fieldName(int index) const return d->fields.value(index).name(); } -/*! - \overload -*/ -int QSqlRecord::indexOf(const QString &name) const -{ - return indexOf(QStringView(name)); -} - /*! Returns the position of the field called \a name within the record, or -1 if it cannot be found. Field names are not @@ -235,9 +220,12 @@ int QSqlRecord::indexOf(const QString &name) const \sa fieldName() */ -int QSqlRecord::indexOf(QStringView name) const +int QSqlRecord::indexOf(QAnyStringView name) const { - return d->indexOfImpl(name); + return name.visit([&](auto v) + { + return d->indexOfImpl(v); + }); } /*! @@ -250,14 +238,6 @@ QSqlField QSqlRecord::field(int index) const return d->fields.value(index); } -/*! - \overload - */ -QSqlField QSqlRecord::field(const QString &name) const -{ - return field(QStringView(name)); -} - /*! \overload @@ -265,7 +245,7 @@ QSqlField QSqlRecord::field(const QString &name) const \a name is not found, function returns a \l{default-constructed value}. */ -QSqlField QSqlRecord::field(QStringView name) const +QSqlField QSqlRecord::field(QAnyStringView name) const { return field(indexOf(name)); } @@ -350,20 +330,11 @@ bool QSqlRecord::isEmpty() const return d->fields.isEmpty(); } - -/*! - \overload -*/ -bool QSqlRecord::contains(const QString &name) const -{ - return contains(QStringView(name)); -} - /*! Returns \c true if there is a field in the record called \a name; otherwise returns \c false. */ -bool QSqlRecord::contains(QStringView name) const +bool QSqlRecord::contains(QAnyStringView name) const { return indexOf(name) >= 0; } @@ -382,13 +353,6 @@ void QSqlRecord::clearValues() f.clear(); } -/*! - \overload -*/ -void QSqlRecord::setGenerated(const QString &name, bool generated) -{ - setGenerated(QStringView(name), generated); -} /*! Sets the generated flag for the field called \a name to \a generated. If the field does not exist, nothing happens. Only @@ -397,7 +361,7 @@ void QSqlRecord::setGenerated(const QString &name, bool generated) \sa isGenerated() */ -void QSqlRecord::setGenerated(QStringView name, bool generated) +void QSqlRecord::setGenerated(QAnyStringView name, bool generated) { setGenerated(indexOf(name), generated); } @@ -427,14 +391,6 @@ bool QSqlRecord::isNull(int index) const return d->fields.value(index).isNull(); } -/*! - \overload -*/ -bool QSqlRecord::isNull(const QString &name) const -{ - return isNull(QStringView(name)); -} - /*! \overload @@ -443,7 +399,7 @@ bool QSqlRecord::isNull(const QString &name) const \sa setNull() */ -bool QSqlRecord::isNull(QStringView name) const +bool QSqlRecord::isNull(QAnyStringView name) const { return isNull(indexOf(name)); } @@ -462,33 +418,17 @@ void QSqlRecord::setNull(int index) d->fields[index].clear(); } -/*! - \overload -*/ -void QSqlRecord::setNull(const QString &name) -{ - setNull(QStringView(name)); -} - /*! \overload Sets the value of the field called \a name to null. If the field does not exist, nothing happens. */ -void QSqlRecord::setNull(QStringView name) +void QSqlRecord::setNull(QAnyStringView name) { setNull(indexOf(name)); } -/*! - \overload -*/ -bool QSqlRecord::isGenerated(const QString &name) const -{ - return isGenerated(QStringView(name)); -} - /*! \overload @@ -497,7 +437,7 @@ bool QSqlRecord::isGenerated(const QString &name) const \sa setGenerated() */ -bool QSqlRecord::isGenerated(QStringView name) const +bool QSqlRecord::isGenerated(QAnyStringView name) const { return isGenerated(indexOf(name)); } @@ -539,13 +479,6 @@ void QSqlRecord::setValue(int index, const QVariant &val) d->fields[index].setValue(val); } -/*! - \overload -*/ -void QSqlRecord::setValue(const QString &name, const QVariant &val) -{ - setValue(QStringView(name), val); -} /*! \overload @@ -554,7 +487,7 @@ void QSqlRecord::setValue(const QString &name, const QVariant &val) \sa setNull() */ -void QSqlRecord::setValue(QStringView name, const QVariant &val) +void QSqlRecord::setValue(QAnyStringView name, const QVariant &val) { setValue(indexOf(name), val); } diff --git a/src/sql/kernel/qsqlrecord.h b/src/sql/kernel/qsqlrecord.h index 8f653ba5e13..aea9af72950 100644 --- a/src/sql/kernel/qsqlrecord.h +++ b/src/sql/kernel/qsqlrecord.h @@ -32,32 +32,48 @@ public: inline bool operator!=(const QSqlRecord &other) const { return !operator==(other); } QVariant value(int i) const; +#if QT_SQL_REMOVED_SINCE(6, 8) QVariant value(const QString &name) const; - QVariant value(QStringView name) const; +#endif + QVariant value(QAnyStringView name) const; void setValue(int i, const QVariant &val); +#if QT_SQL_REMOVED_SINCE(6, 8) void setValue(const QString &name, const QVariant &val); - void setValue(QStringView name, const QVariant &val); +#endif + void setValue(QAnyStringView name, const QVariant &val); void setNull(int i); +#if QT_SQL_REMOVED_SINCE(6, 8) void setNull(const QString &name); - void setNull(QStringView name); +#endif + void setNull(QAnyStringView name); bool isNull(int i) const; +#if QT_SQL_REMOVED_SINCE(6, 8) bool isNull(const QString &name) const; - bool isNull(QStringView name) const; +#endif + bool isNull(QAnyStringView name) const; +#if QT_SQL_REMOVED_SINCE(6, 8) int indexOf(const QString &name) const; - int indexOf(QStringView name) const; +#endif + int indexOf(QAnyStringView name) const; QString fieldName(int i) const; QSqlField field(int i) const; +#if QT_SQL_REMOVED_SINCE(6, 8) QSqlField field(const QString &name) const; - QSqlField field(QStringView name) const; +#endif + QSqlField field(QAnyStringView name) const; bool isGenerated(int i) const; +#if QT_SQL_REMOVED_SINCE(6, 8) bool isGenerated(const QString &name) const; - bool isGenerated(QStringView name) const; +#endif + bool isGenerated(QAnyStringView name) const; +#if QT_SQL_REMOVED_SINCE(6, 8) void setGenerated(const QString &name, bool generated); - void setGenerated(QStringView name, bool generated); +#endif + void setGenerated(QAnyStringView name, bool generated); void setGenerated(int i, bool generated); void append(const QSqlField &field); @@ -66,8 +82,10 @@ public: void remove(int pos); bool isEmpty() const; +#if QT_SQL_REMOVED_SINCE(6, 8) bool contains(const QString &name) const; - bool contains(QStringView name) const; +#endif + bool contains(QAnyStringView name) const; void clear(); void clearValues(); int count() const; diff --git a/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp b/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp index 6aeae86d7d7..8e7d5b502ee 100644 --- a/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp +++ b/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp @@ -10,7 +10,7 @@ #include -#define NUM_FIELDS 4 +#define NUM_FIELDS 5 class tst_QSqlRecord : public QObject { @@ -67,6 +67,7 @@ void tst_QSqlRecord::createTestRecord() fields[1] = std::make_unique(QStringLiteral("int"), QMetaType(QMetaType::Int), QStringLiteral("inttable")); fields[2] = std::make_unique(QStringLiteral("double"), QMetaType(QMetaType::Double), QStringLiteral("doubletable")); fields[3] = std::make_unique(QStringLiteral("bool"), QMetaType(QMetaType::Bool)); + fields[4] = std::make_unique(QStringLiteral("öäü@€"), QMetaType(QMetaType::Int)); for (const auto &field : fields) rec->append(*field); } @@ -173,9 +174,22 @@ void tst_QSqlRecord::clearValues() void tst_QSqlRecord::contains() { createTestRecord(); - for (const auto &field : fields) - QVERIFY(rec->contains(field->name())); - QVERIFY( !rec->contains( "__Harry__" ) ); + QStringList fieldNames; + for (const auto &field : fields) { + fieldNames.append(field->name()); + if (!field->tableName().isEmpty()) + fieldNames.append(field->tableName() + u'.' + field->name()); + } + for (const auto &name : std::as_const(fieldNames)) { + QVERIFY(rec->contains(name)); + const QByteArray nameBa = name.toUtf8(); + QVERIFY(rec->contains(nameBa)); + const char *nameStr = nameBa.constData(); + QVERIFY(rec->contains(nameStr)); + QVERIFY(!rec->contains(name.left(name.size() - 1))); + QVERIFY(!rec->contains(name + u'.' + name)); + } + QVERIFY(!rec->contains("__Harry__")); } void tst_QSqlRecord::count()