diff --git a/src/sql/kernel/qsqlquery.cpp b/src/sql/kernel/qsqlquery.cpp index 3a764c13618..f93ca168d1d 100644 --- a/src/sql/kernel/qsqlquery.cpp +++ b/src/sql/kernel/qsqlquery.cpp @@ -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. diff --git a/src/sql/kernel/qsqlquery.h b/src/sql/kernel/qsqlquery.h index ad389fee7e5..9d019d5cd2a 100644 --- a/src/sql/kernel/qsqlquery.h +++ b/src/sql/kernel/qsqlquery.h @@ -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(); diff --git a/src/sql/kernel/qsqlresult.cpp b/src/sql/kernel/qsqlresult.cpp index fb8bc3a1e5b..1b7daa8d0f6 100644 --- a/src/sql/kernel/qsqlresult.cpp +++ b/src/sql/kernel/qsqlresult.cpp @@ -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 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 { diff --git a/src/sql/kernel/qsqlresult.h b/src/sql/kernel/qsqlresult.h index 2d157cbe87c..c16564a5162 100644 --- a/src/sql/kernel/qsqlresult.h +++ b/src/sql/kernel/qsqlresult.h @@ -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 &boundValues() const; QString executedQuery() const; + QStringList boundValueNames() const; QString boundValueName(int pos) const; void clear(); bool hasOutValues() const; diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp index 5c0f28e14b3..c92e6d46080 100644 --- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp @@ -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);