summaryrefslogtreecommitdiffstats
path: root/src/sql/kernel/qsqlrecord.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sql/kernel/qsqlrecord.cpp')
-rw-r--r--src/sql/kernel/qsqlrecord.cpp116
1 files changed, 63 insertions, 53 deletions
diff --git a/src/sql/kernel/qsqlrecord.cpp b/src/sql/kernel/qsqlrecord.cpp
index e4d2c96c6b..89af1a52fe 100644
--- a/src/sql/kernel/qsqlrecord.cpp
+++ b/src/sql/kernel/qsqlrecord.cpp
@@ -19,6 +19,33 @@ public:
return index >= 0 && index < fields.size();
}
+ template <typename T>
+ qsizetype indexOfImpl(T name)
+ {
+ 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) {
+ tableName = name.left(idx);
+ fieldName = name.mid(idx + 1);
+ }
+ const auto cnt = fields.size();
+ for (qsizetype i = 0; i < cnt; ++i) {
+ // Check the passed in name first in case it is an alias using a dot.
+ // Then check if both the table and field match when there is a table name specified.
+ const auto &currentField = fields.at(i);
+ const auto &currentFieldName = currentField.name();
+ if (name.compare(currentFieldName, Qt::CaseInsensitive) == 0)
+ return i;
+ if (idx != -1 &&
+ tableName.compare(currentField.tableName(), Qt::CaseInsensitive) == 0 &&
+ fieldName.compare(currentFieldName, Qt::CaseInsensitive) == 0)
+ return i;
+ }
+ return -1;
+ }
+
QList<QSqlField> fields;
};
QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QSqlRecordPrivate)
@@ -75,7 +102,7 @@ QSqlRecord::QSqlRecord()
of a record in \l{constant time}.
*/
-QSqlRecord::QSqlRecord(const QSqlRecord& other)
+QSqlRecord::QSqlRecord(const QSqlRecord &other)
= default;
/*!
@@ -116,7 +143,7 @@ QSqlRecord::QSqlRecord(const QSqlRecord& other)
of a record in \l{constant time}.
*/
-QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
+QSqlRecord& QSqlRecord::operator=(const QSqlRecord &other)
= default;
/*!
@@ -166,10 +193,9 @@ QVariant QSqlRecord::value(int index) const
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()
+ \sa indexOf(), isNull()
*/
-
-QVariant QSqlRecord::value(const QString& name) const
+QVariant QSqlRecord::value(QAnyStringView name) const
{
return value(indexOf(name));
}
@@ -193,30 +219,13 @@ QString QSqlRecord::fieldName(int index) const
returned.
\sa fieldName()
-*/
-
-int QSqlRecord::indexOf(const QString& name) const
+ */
+int QSqlRecord::indexOf(QAnyStringView name) const
{
- QStringView tableName;
- QStringView fieldName(name);
- const qsizetype idx = name.indexOf(u'.');
- if (idx != -1) {
- tableName = fieldName.left(idx);
- fieldName = fieldName.mid(idx + 1);
- }
- const int cnt = count();
- for (int i = 0; i < cnt; ++i) {
- // Check the passed in name first in case it is an alias using a dot.
- // Then check if both the table and field match when there is a table name specified.
- const auto &currentField = d->fields.at(i);
- const auto &currentFieldName = currentField.name();
- if (currentFieldName.compare(name, Qt::CaseInsensitive) == 0
- || (idx != -1 && currentFieldName.compare(fieldName, Qt::CaseInsensitive) == 0
- && currentField.tableName().compare(tableName, Qt::CaseInsensitive) == 0)) {
- return i;
- }
- }
- return -1;
+ return name.visit([&](auto v)
+ {
+ return d->indexOfImpl(v);
+ });
}
/*!
@@ -229,10 +238,14 @@ QSqlField QSqlRecord::field(int index) const
return d->fields.value(index);
}
-/*! \overload
- Returns the field called \a 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(const QString &name) const
+QSqlField QSqlRecord::field(QAnyStringView name) const
{
return field(indexOf(name));
}
@@ -244,7 +257,7 @@ QSqlField QSqlRecord::field(const QString &name) const
\sa insert(), replace(), remove()
*/
-void QSqlRecord::append(const QSqlField& field)
+void QSqlRecord::append(const QSqlField &field)
{
detach();
d->fields.append(field);
@@ -255,7 +268,7 @@ void QSqlRecord::append(const QSqlField& field)
\sa append(), replace(), remove()
*/
-void QSqlRecord::insert(int pos, const QSqlField& field)
+void QSqlRecord::insert(int pos, const QSqlField &field)
{
detach();
d->fields.insert(pos, field);
@@ -268,7 +281,7 @@ void QSqlRecord::insert(int pos, const QSqlField& field)
\sa append(), insert(), remove()
*/
-void QSqlRecord::replace(int pos, const QSqlField& field)
+void QSqlRecord::replace(int pos, const QSqlField &field)
{
if (!d->contains(pos))
return;
@@ -317,13 +330,11 @@ bool QSqlRecord::isEmpty() const
return d->fields.isEmpty();
}
-
/*!
Returns \c true if there is a field in the record called \a name;
otherwise returns \c false.
*/
-
-bool QSqlRecord::contains(const QString& name) const
+bool QSqlRecord::contains(QAnyStringView name) const
{
return indexOf(name) >= 0;
}
@@ -350,15 +361,12 @@ void QSqlRecord::clearValues()
\sa isGenerated()
*/
-
-void QSqlRecord::setGenerated(const QString& name, bool generated)
+void QSqlRecord::setGenerated(QAnyStringView name, bool generated)
{
setGenerated(indexOf(name), generated);
}
/*!
- \overload
-
Sets the generated flag for the field \a index to \a generated.
\sa isGenerated()
@@ -373,10 +381,10 @@ void QSqlRecord::setGenerated(int index, bool generated)
}
/*!
- \overload
-
Returns \c true if the field \a index is null or if there is no field at
position \a index; otherwise returns \c false.
+
+ \sa setNull()
*/
bool QSqlRecord::isNull(int index) const
{
@@ -384,12 +392,14 @@ bool QSqlRecord::isNull(int index) const
}
/*!
+ \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(const QString& name) const
+bool QSqlRecord::isNull(QAnyStringView name) const
{
return isNull(indexOf(name));
}
@@ -414,25 +424,25 @@ void QSqlRecord::setNull(int index)
Sets the value of the field called \a name to null. If the field
does not exist, nothing happens.
*/
-void QSqlRecord::setNull(const QString& name)
+void QSqlRecord::setNull(QAnyStringView name)
{
setNull(indexOf(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(const QString& name) const
+bool QSqlRecord::isGenerated(QAnyStringView name) const
{
return isGenerated(indexOf(name));
}
-/*! \overload
-
+/*!
Returns \c true if the record has a field at position \a index and this
field is to be generated (the default); otherwise returns \c false.
@@ -461,7 +471,7 @@ int QSqlRecord::count() const
\sa setNull()
*/
-void QSqlRecord::setValue(int index, const QVariant& val)
+void QSqlRecord::setValue(int index, const QVariant &val)
{
if (!d->contains(index))
return;
@@ -469,15 +479,15 @@ void QSqlRecord::setValue(int index, const QVariant& val)
d->fields[index].setValue(val);
}
-
/*!
\overload
Sets the value of the field called \a name to \a val. If the field
does not exist, nothing happens.
-*/
-void QSqlRecord::setValue(const QString& name, const QVariant& val)
+ \sa setNull()
+*/
+void QSqlRecord::setValue(QAnyStringView name, const QVariant &val)
{
setValue(indexOf(name), val);
}