diff options
author | Christian Ehrlicher <[email protected]> | 2023-02-14 19:33:30 +0100 |
---|---|---|
committer | Volker Hilsheimer <[email protected]> | 2023-04-07 14:25:28 +0200 |
commit | 3983babd71f334274cc606e2cf76288ff3e0d4a2 (patch) | |
tree | 900ccf53806f913f070ed6dcaa0abad1c64bca0a | |
parent | 9db9a836fbe3a040e6b8e1ee73c7ca2c62a6d888 (diff) |
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 <[email protected]>
-rw-r--r-- | src/sql/kernel/qsqlquery.cpp | 33 | ||||
-rw-r--r-- | src/sql/kernel/qsqlquery.h | 2 | ||||
-rw-r--r-- | src/sql/kernel/qsqlresult.cpp | 16 | ||||
-rw-r--r-- | src/sql/kernel/qsqlresult.h | 2 | ||||
-rw-r--r-- | tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp | 6 |
5 files changed, 57 insertions, 2 deletions
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 @@ -1166,6 +1167,36 @@ QVariantList QSqlQuery::boundValues() const } /*! + \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. In most cases this function returns the same string as lastQuery(). 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 @@ -838,10 +838,24 @@ void QSqlResult::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 { 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<QVariant> &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); |