diff options
author | Christian Ehrlicher <[email protected]> | 2024-01-26 23:21:27 +0100 |
---|---|---|
committer | Volker Hilsheimer <[email protected]> | 2024-06-04 18:44:10 +0000 |
commit | 6b096271cf0aac38d80f292f8b37edd671a8d17f (patch) | |
tree | 5eda6ac3141f26a8cb31c2b8f410b68be11391d8 | |
parent | 4d6721781806e4ce172f836d4b58df68acc4e4b0 (diff) |
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 <[email protected]>
-rw-r--r-- | src/sql/compat/removed_api.cpp | 69 | ||||
-rw-r--r-- | src/sql/kernel/qsqlquery.cpp | 20 | ||||
-rw-r--r-- | src/sql/kernel/qsqlquery.h | 8 | ||||
-rw-r--r-- | src/sql/kernel/qsqlrecord.cpp | 101 | ||||
-rw-r--r-- | src/sql/kernel/qsqlrecord.h | 36 | ||||
-rw-r--r-- | tests/auto/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<QVariant> &QSqlResult::boundValues() const } #endif // QT_SQL_REMOVED_SINCE(6, 6) + +#if QT_SQL_REMOVED_SINCE(6, 8) + +#include "qsqlrecord.h" +#include "qsqlfield.h" + +// #include <qotherheader.h> +// // 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 @@ -330,21 +330,13 @@ bool QSqlQuery::isNull(int field) const /*! \overload -*/ -bool QSqlQuery::isNull(const QString &name) const -{ - return isNull(QStringView(name)); -} - -/*! - \overload Returns \c true if there is no field with this \a name; otherwise returns isNull(int index) for the corresponding field index. 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) @@ -449,21 +441,13 @@ QVariant QSqlQuery::value(int index) const /*! \overload -*/ -QVariant QSqlQuery::value(const QString &name) const -{ - return value(QStringView(name)); -} - -/*! - \overload Returns the value of the field called \a name in the current record. If field \a name does not exist an invalid variant is returned. 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 <typename T> + 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) { @@ -188,21 +189,13 @@ QVariant QSqlRecord::value(int index) const /*! \overload -*/ -QVariant QSqlRecord::value(const QString &name) const -{ - return value(QStringView(name)); -} - -/*! - \overload Returns the value of the field called \a name in the record. If field \a name does not exist an invalid variant is returned. \sa indexOf(), isNull() */ -QVariant QSqlRecord::value(QStringView name) const +QVariant QSqlRecord::value(QAnyStringView name) const { return value(indexOf(name)); } @@ -220,14 +213,6 @@ QString QSqlRecord::fieldName(int index) const } /*! - \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 case-sensitive. If more than one field matches, the first one is @@ -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); + }); } /*! @@ -252,20 +240,12 @@ QSqlField QSqlRecord::field(int index) const /*! \overload - */ -QSqlField QSqlRecord::field(const QString &name) const -{ - return field(QStringView(name)); -} - -/*! - \overload Returns the field called \a name. If the field called \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; } @@ -383,13 +354,6 @@ void QSqlRecord::clearValues() } /*! - \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 fields that have \a generated set to true are included in the SQL @@ -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); } @@ -429,21 +393,13 @@ bool QSqlRecord::isNull(int index) const /*! \overload -*/ -bool QSqlRecord::isNull(const QString &name) const -{ - return isNull(QStringView(name)); -} - -/*! - \overload Returns \c true if the field called \a name is null or if there is no field called \a name; otherwise returns \c false. \sa setNull() */ -bool QSqlRecord::isNull(QStringView name) const +bool QSqlRecord::isNull(QAnyStringView name) const { return isNull(indexOf(name)); } @@ -464,40 +420,24 @@ void QSqlRecord::setNull(int index) /*! \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 Returns \c true if the record has a field called \a name and this field is to be generated (the default); otherwise returns \c false. \sa setGenerated() */ -bool QSqlRecord::isGenerated(QStringView name) const +bool QSqlRecord::isGenerated(QAnyStringView name) const { return isGenerated(indexOf(name)); } @@ -541,20 +481,13 @@ void QSqlRecord::setValue(int index, const QVariant &val) /*! \overload -*/ -void QSqlRecord::setValue(const QString &name, const QVariant &val) -{ - setValue(QStringView(name), val); -} -/*! - \overload Sets the value of the field called \a name to \a val. If the field does not exist, nothing happens. \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 <qsqlrecord.h> -#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<QSqlField>(QStringLiteral("int"), QMetaType(QMetaType::Int), QStringLiteral("inttable")); fields[2] = std::make_unique<QSqlField>(QStringLiteral("double"), QMetaType(QMetaType::Double), QStringLiteral("doubletable")); fields[3] = std::make_unique<QSqlField>(QStringLiteral("bool"), QMetaType(QMetaType::Bool)); + fields[4] = std::make_unique<QSqlField>(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() |