QSqlQuery: add boundValueName()/boundValueNames()

[ChangeLog][SQL][SqlQuery] Added two new functions
boundValueName()/boundValueNames() to return the names of the bound
values.

Fixes: QTBUG-97847
Change-Id: I8df5f15e8df13141a34d38b0a2e13b37f4e7829c
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-02-14 19:33:30 +01:00 committed by Volker Hilsheimer
parent 9db9a836fb
commit 3983babd71
5 changed files with 57 additions and 2 deletions

View File

@ -1138,6 +1138,7 @@ QVariant QSqlQuery::boundValue(const QString& placeholder) const
/*!
Returns the value for the placeholder at position \a pos.
\sa boundValues()
*/
QVariant QSqlQuery::boundValue(int pos) const
{
@ -1156,7 +1157,7 @@ QVariant QSqlQuery::boundValue(int pos) const
\snippet sqldatabase/sqldatabase.cpp 14
\sa boundValue(), bindValue(), addBindValue()
\sa boundValue(), bindValue(), addBindValue(), boundValueNames()
*/
QVariantList QSqlQuery::boundValues() const
@ -1165,6 +1166,36 @@ QVariantList QSqlQuery::boundValues() const
return values;
}
/*!
\since 6.6
Returns the names of all bound values.
The order of the list is in binding order, irrespective of whether
named or positional binding is used.
\sa boundValues(), boundValueName()
*/
QStringList QSqlQuery::boundValueNames() const
{
return d->sqlResult->boundValueNames();
}
/*!
\since 6.6
Returns the bound value name at position \a pos.
The order of the list is in binding order, irrespective of whether
named or positional binding is used.
\sa boundValueNames()
*/
QString QSqlQuery::boundValueName(int pos) const
{
return d->sqlResult->boundValueName(pos);
}
/*!
Returns the last query that was successfully executed.

View File

@ -89,6 +89,8 @@ public:
QVariant boundValue(const QString& placeholder) const;
QVariant boundValue(int pos) const;
QVariantList boundValues() const;
QStringList boundValueNames() const;
QString boundValueName(int pos) const;
QString executedQuery() const;
QVariant lastInsertId() const;
void finish();

View File

@ -837,11 +837,25 @@ void QSqlResult::resetBindCount()
d->resetBindCount();
}
/*!
Returns the names of all bound values.
\sa boundValue(), boundValueName()
*/
QStringList QSqlResult::boundValueNames() const
{
Q_D(const QSqlResult);
QList<QString> ret;
for (const QHolder &holder : std::as_const(d->holders))
ret.push_back(holder.holderName);
return ret;
}
/*!
Returns the name of the bound value at position \a index in the
current record (row).
\sa boundValue()
\sa boundValue(), boundValueNames()
*/
QString QSqlResult::boundValueName(int index) const
{

View File

@ -69,8 +69,10 @@ protected:
QSql::ParamType bindValueType(const QString& placeholder) const;
QSql::ParamType bindValueType(int pos) const;
int boundValueCount() const;
// ### Qt 7 - don't return a non-const reference from a const function
QList<QVariant> &boundValues() const;
QString executedQuery() const;
QStringList boundValueNames() const;
QString boundValueName(int pos) const;
void clear();
bool hasOutValues() const;

View File

@ -2132,6 +2132,12 @@ void tst_QSqlQuery::prepare_bind_exec()
QCOMPARE(m.size(), qsizetype(2));
QCOMPARE(m.at(0).toInt(), i);
QCOMPARE(m.at(1).toString(), values[i]);
const QStringList n = q.boundValueNames();
QCOMPARE(n.size(), 2);
QCOMPARE(n.at(0), ":id");
QCOMPARE(n.at(1), ":name");
QCOMPARE(q.boundValueName(0), ":id");
QCOMPARE(q.boundValueName(1), ":name");
}
q.bindValue(":id", 8);