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 <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2024-01-26 23:21:27 +01:00 committed by Volker Hilsheimer
parent 4d67217818
commit 6b096271cf
6 changed files with 139 additions and 117 deletions

View File

@ -50,3 +50,72 @@ QList<QVariant> &QSqlResult::boundValues() const
} }
#endif // QT_SQL_REMOVED_SINCE(6, 6) #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)

View File

@ -328,14 +328,6 @@ bool QSqlQuery::isNull(int field) const
|| d->sqlResult->isNull(field); || d->sqlResult->isNull(field);
} }
/*!
\overload
*/
bool QSqlQuery::isNull(const QString &name) const
{
return isNull(QStringView(name));
}
/*! /*!
\overload \overload
@ -344,7 +336,7 @@ bool QSqlQuery::isNull(const QString &name) const
This overload is less efficient than \l{QSqlQuery::}{isNull()} 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); qsizetype index = d->sqlResult->record().indexOf(name);
if (index > -1) if (index > -1)
@ -447,14 +439,6 @@ QVariant QSqlQuery::value(int index) const
return QVariant(); return QVariant();
} }
/*!
\overload
*/
QVariant QSqlQuery::value(const QString &name) const
{
return value(QStringView(name));
}
/*! /*!
\overload \overload
@ -463,7 +447,7 @@ QVariant QSqlQuery::value(const QString &name) const
This overload is less efficient than \l{QSqlQuery::}{value()} 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); qsizetype index = d->sqlResult->record().indexOf(name);
if (index > -1) if (index > -1)

View File

@ -55,8 +55,10 @@ public:
bool isValid() const; bool isValid() const;
bool isActive() const; bool isActive() const;
bool isNull(int field) const; bool isNull(int field) const;
#if QT_SQL_REMOVED_SINCE(6, 8)
bool isNull(const QString &name) const; bool isNull(const QString &name) const;
bool isNull(QStringView name) const; #endif
bool isNull(QAnyStringView name) const;
int at() const; int at() const;
QString lastQuery() const; QString lastQuery() const;
int numRowsAffected() const; int numRowsAffected() const;
@ -71,8 +73,10 @@ public:
void setForwardOnly(bool forward); void setForwardOnly(bool forward);
bool exec(const QString& query); bool exec(const QString& query);
QVariant value(int i) const; QVariant value(int i) const;
#if QT_SQL_REMOVED_SINCE(6, 8)
QVariant value(const QString &name) const; QVariant value(const QString &name) const;
QVariant value(QStringView name) const; #endif
QVariant value(QAnyStringView name) const;
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy); void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy);
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const; QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;

View File

@ -19,10 +19,11 @@ public:
return index >= 0 && index < fields.size(); return index >= 0 && index < fields.size();
} }
qsizetype indexOfImpl(QStringView name) template <typename T>
qsizetype indexOfImpl(T name)
{ {
QStringView tableName; T tableName;
QStringView fieldName; T fieldName;
const auto it = std::find(name.begin(), name.end(), u'.'); const auto it = std::find(name.begin(), name.end(), u'.');
const auto idx = (it == name.end()) ? -1 : it - name.begin(); const auto idx = (it == name.end()) ? -1 : it - name.begin();
if (idx != -1) { if (idx != -1) {
@ -186,14 +187,6 @@ QVariant QSqlRecord::value(int index) const
return d->fields.value(index).value(); return d->fields.value(index).value();
} }
/*!
\overload
*/
QVariant QSqlRecord::value(const QString &name) const
{
return value(QStringView(name));
}
/*! /*!
\overload \overload
@ -202,7 +195,7 @@ QVariant QSqlRecord::value(const QString &name) const
\sa indexOf(), isNull() \sa indexOf(), isNull()
*/ */
QVariant QSqlRecord::value(QStringView name) const QVariant QSqlRecord::value(QAnyStringView name) const
{ {
return value(indexOf(name)); return value(indexOf(name));
} }
@ -219,14 +212,6 @@ QString QSqlRecord::fieldName(int index) const
return d->fields.value(index).name(); 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 Returns the position of the field called \a name within the
record, or -1 if it cannot be found. Field names are not 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() \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); return d->fields.value(index);
} }
/*!
\overload
*/
QSqlField QSqlRecord::field(const QString &name) const
{
return field(QStringView(name));
}
/*! /*!
\overload \overload
@ -265,7 +245,7 @@ QSqlField QSqlRecord::field(const QString &name) const
\a name is not found, function returns \a name is not found, function returns
a \l{default-constructed value}. a \l{default-constructed value}.
*/ */
QSqlField QSqlRecord::field(QStringView name) const QSqlField QSqlRecord::field(QAnyStringView name) const
{ {
return field(indexOf(name)); return field(indexOf(name));
} }
@ -350,20 +330,11 @@ bool QSqlRecord::isEmpty() const
return d->fields.isEmpty(); 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; Returns \c true if there is a field in the record called \a name;
otherwise returns \c false. otherwise returns \c false.
*/ */
bool QSqlRecord::contains(QStringView name) const bool QSqlRecord::contains(QAnyStringView name) const
{ {
return indexOf(name) >= 0; return indexOf(name) >= 0;
} }
@ -382,13 +353,6 @@ void QSqlRecord::clearValues()
f.clear(); 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 Sets the generated flag for the field called \a name to \a
generated. If the field does not exist, nothing happens. Only generated. If the field does not exist, nothing happens. Only
@ -397,7 +361,7 @@ void QSqlRecord::setGenerated(const QString &name, bool generated)
\sa isGenerated() \sa isGenerated()
*/ */
void QSqlRecord::setGenerated(QStringView name, bool generated) void QSqlRecord::setGenerated(QAnyStringView name, bool generated)
{ {
setGenerated(indexOf(name), generated); setGenerated(indexOf(name), generated);
} }
@ -427,14 +391,6 @@ bool QSqlRecord::isNull(int index) const
return d->fields.value(index).isNull(); return d->fields.value(index).isNull();
} }
/*!
\overload
*/
bool QSqlRecord::isNull(const QString &name) const
{
return isNull(QStringView(name));
}
/*! /*!
\overload \overload
@ -443,7 +399,7 @@ bool QSqlRecord::isNull(const QString &name) const
\sa setNull() \sa setNull()
*/ */
bool QSqlRecord::isNull(QStringView name) const bool QSqlRecord::isNull(QAnyStringView name) const
{ {
return isNull(indexOf(name)); return isNull(indexOf(name));
} }
@ -462,33 +418,17 @@ void QSqlRecord::setNull(int index)
d->fields[index].clear(); d->fields[index].clear();
} }
/*!
\overload
*/
void QSqlRecord::setNull(const QString &name)
{
setNull(QStringView(name));
}
/*! /*!
\overload \overload
Sets the value of the field called \a name to null. If the field Sets the value of the field called \a name to null. If the field
does not exist, nothing happens. does not exist, nothing happens.
*/ */
void QSqlRecord::setNull(QStringView name) void QSqlRecord::setNull(QAnyStringView name)
{ {
setNull(indexOf(name)); setNull(indexOf(name));
} }
/*!
\overload
*/
bool QSqlRecord::isGenerated(const QString &name) const
{
return isGenerated(QStringView(name));
}
/*! /*!
\overload \overload
@ -497,7 +437,7 @@ bool QSqlRecord::isGenerated(const QString &name) const
\sa setGenerated() \sa setGenerated()
*/ */
bool QSqlRecord::isGenerated(QStringView name) const bool QSqlRecord::isGenerated(QAnyStringView name) const
{ {
return isGenerated(indexOf(name)); return isGenerated(indexOf(name));
} }
@ -539,13 +479,6 @@ void QSqlRecord::setValue(int index, const QVariant &val)
d->fields[index].setValue(val); d->fields[index].setValue(val);
} }
/*!
\overload
*/
void QSqlRecord::setValue(const QString &name, const QVariant &val)
{
setValue(QStringView(name), val);
}
/*! /*!
\overload \overload
@ -554,7 +487,7 @@ void QSqlRecord::setValue(const QString &name, const QVariant &val)
\sa setNull() \sa setNull()
*/ */
void QSqlRecord::setValue(QStringView name, const QVariant &val) void QSqlRecord::setValue(QAnyStringView name, const QVariant &val)
{ {
setValue(indexOf(name), val); setValue(indexOf(name), val);
} }

View File

@ -32,32 +32,48 @@ public:
inline bool operator!=(const QSqlRecord &other) const { return !operator==(other); } inline bool operator!=(const QSqlRecord &other) const { return !operator==(other); }
QVariant value(int i) const; QVariant value(int i) const;
#if QT_SQL_REMOVED_SINCE(6, 8)
QVariant value(const QString &name) const; QVariant value(const QString &name) const;
QVariant value(QStringView name) const; #endif
QVariant value(QAnyStringView name) const;
void setValue(int i, const QVariant &val); void setValue(int i, const QVariant &val);
#if QT_SQL_REMOVED_SINCE(6, 8)
void setValue(const QString &name, const QVariant &val); 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); void setNull(int i);
#if QT_SQL_REMOVED_SINCE(6, 8)
void setNull(const QString &name); void setNull(const QString &name);
void setNull(QStringView name); #endif
void setNull(QAnyStringView name);
bool isNull(int i) const; bool isNull(int i) const;
#if QT_SQL_REMOVED_SINCE(6, 8)
bool isNull(const QString &name) const; 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(const QString &name) const;
int indexOf(QStringView name) const; #endif
int indexOf(QAnyStringView name) const;
QString fieldName(int i) const; QString fieldName(int i) const;
QSqlField field(int i) const; QSqlField field(int i) const;
#if QT_SQL_REMOVED_SINCE(6, 8)
QSqlField field(const QString &name) const; QSqlField field(const QString &name) const;
QSqlField field(QStringView name) const; #endif
QSqlField field(QAnyStringView name) const;
bool isGenerated(int i) const; bool isGenerated(int i) const;
#if QT_SQL_REMOVED_SINCE(6, 8)
bool isGenerated(const QString &name) const; 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(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 setGenerated(int i, bool generated);
void append(const QSqlField &field); void append(const QSqlField &field);
@ -66,8 +82,10 @@ public:
void remove(int pos); void remove(int pos);
bool isEmpty() const; bool isEmpty() const;
#if QT_SQL_REMOVED_SINCE(6, 8)
bool contains(const QString &name) const; bool contains(const QString &name) const;
bool contains(QStringView name) const; #endif
bool contains(QAnyStringView name) const;
void clear(); void clear();
void clearValues(); void clearValues();
int count() const; int count() const;

View File

@ -10,7 +10,7 @@
#include <qsqlrecord.h> #include <qsqlrecord.h>
#define NUM_FIELDS 4 #define NUM_FIELDS 5
class tst_QSqlRecord : public QObject 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[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[2] = std::make_unique<QSqlField>(QStringLiteral("double"), QMetaType(QMetaType::Double), QStringLiteral("doubletable"));
fields[3] = std::make_unique<QSqlField>(QStringLiteral("bool"), QMetaType(QMetaType::Bool)); 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) for (const auto &field : fields)
rec->append(*field); rec->append(*field);
} }
@ -173,9 +174,22 @@ void tst_QSqlRecord::clearValues()
void tst_QSqlRecord::contains() void tst_QSqlRecord::contains()
{ {
createTestRecord(); createTestRecord();
for (const auto &field : fields) QStringList fieldNames;
QVERIFY(rec->contains(field->name())); for (const auto &field : fields) {
QVERIFY( !rec->contains( "__Harry__" ) ); 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() void tst_QSqlRecord::count()