From 1bd883dbc15c4016f78d421afac2ac9f31ec4874 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Tue, 17 Dec 2024 19:54:55 +0100 Subject: [PATCH] QSqlQueryModel: add new function to refresh the model data This function re-executes the query used by QSqlQueryModel and refreshes the data from the database. The query must not use bound values as these values are not preserved. [ChangeLog][QtSql][QSqlQueryModel] Added refresh() to refresh the model data from the database. Pick-to: 6.9 Task-number: QTBUG-123603 Change-Id: I3f1d779e07b88565abe825c31cfc4d7d1b2312c4 Reviewed-by: Volker Hilsheimer --- src/sql/models/qsqlquerymodel.cpp | 23 ++++--------------- src/sql/models/qsqlquerymodel.h | 3 +-- .../qsqlquerymodel/tst_qsqlquerymodel.cpp | 16 ++++++------- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/sql/models/qsqlquerymodel.cpp b/src/sql/models/qsqlquerymodel.cpp index fb338d16dfa..99aa82428e4 100644 --- a/src/sql/models/qsqlquerymodel.cpp +++ b/src/sql/models/qsqlquerymodel.cpp @@ -473,29 +473,16 @@ void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db) \since 6.9 Re-executes the current query to fetch the data from the same database connection. - \note \c refreshQuery() is not applicable when the query contains bound values. + \note \c refresh() is not applicable when the query contains bound values. \sa setQuery(QSqlQuery &&query), QSqlQuery::boundValue() */ -void QSqlQueryModel::refreshQuery() +void QSqlQueryModel::refresh() { Q_D(QSqlQueryModel); - setQuery(d->query.executedQuery()); -} - -/*! - \overload - \since 6.9 - Re-executes the current query to fetch the data from the given database connection \a db. - - \note \c refreshQuery(const QSqlDatabase &db) is not applicable when the query contains bound values. - - \sa setQuery(const QString &query, const QSqlDatabase &db), QSqlQuery::boundValue() -*/ -void QSqlQueryModel::refreshQuery(const QSqlDatabase &db) -{ - Q_D(QSqlQueryModel); - setQuery(d->query.executedQuery(), db); + const auto connName = d->query.driver() + ? d->query.driver()->connectionName() : QString(); + setQuery(d->query.executedQuery(), QSqlDatabase::database(connName)); } /*! diff --git a/src/sql/models/qsqlquerymodel.h b/src/sql/models/qsqlquerymodel.h index f9fddbb9752..f168d0bda13 100644 --- a/src/sql/models/qsqlquerymodel.h +++ b/src/sql/models/qsqlquerymodel.h @@ -46,8 +46,7 @@ class Q_SQL_EXPORT QSqlQueryModel: public QAbstractTableModel #endif void setQuery(QSqlQuery &&query); void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase()); - void refreshQuery(); - void refreshQuery(const QSqlDatabase &db); + void refresh(); #if QT_SQL_REMOVED_SINCE(6, 5) QSqlQuery query() const; #endif diff --git a/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp b/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp index 81aed273e18..2b68b1397bf 100644 --- a/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp +++ b/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp @@ -59,10 +59,10 @@ private slots: void task_180617(); void task_180617_data() { generic_data(); } void task_QTBUG_4963_setHeaderDataWithProxyModel(); - void refreshQuery_data() { generic_data(); } - void refreshQuery(); - void refreshQueryWithBoundValues_data() { generic_data(); } - void refreshQueryWithBoundValues(); + void refresh_data() { generic_data(); } + void refresh(); + void refreshWithBoundValues_data() { generic_data(); } + void refreshWithBoundValues(); private: void generic_data(const QString &engine = QString()); void dropTestTables(const QSqlDatabase &db); @@ -663,7 +663,7 @@ void tst_QSqlQueryModel::task_QTBUG_4963_setHeaderDataWithProxyModel() // And it should not crash. } -void tst_QSqlQueryModel::refreshQuery() +void tst_QSqlQueryModel::refresh() { QFETCH(QString, dbName); QSqlDatabase db = QSqlDatabase::database(dbName); @@ -677,7 +677,7 @@ void tst_QSqlQueryModel::refreshQuery() QSqlQuery(db).exec("UPDATE " + qTableName("test", __FILE__, QSqlDatabase::database(dbName)) + " SET name = 'updated_harry' WHERE id = 1"); - model.refreshQuery(db); + model.refresh(); QCOMPARE(model.rowCount(), 2); QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); QCOMPARE(model.data(model.index(0, 1)).toString(), QString("updated_harry")); @@ -688,7 +688,7 @@ void tst_QSqlQueryModel::refreshQuery() + " SET name = 'harry' WHERE id = 1"); } -void tst_QSqlQueryModel::refreshQueryWithBoundValues() +void tst_QSqlQueryModel::refreshWithBoundValues() { QFETCH(QString, dbName); QSqlDatabase db = QSqlDatabase::database(dbName); @@ -707,7 +707,7 @@ void tst_QSqlQueryModel::refreshQueryWithBoundValues() QSqlQuery(db).exec("UPDATE " + qTableName("test", __FILE__, QSqlDatabase::database(dbName)) + " SET name = 'updated_harry' WHERE id = 1"); - model.refreshQuery(db); + model.refresh(); QCOMPARE(model.rowCount(), 0); QCOMPARE_NE(model.data(model.index(0, 0)).toString(), QString("updated_harry")); QCOMPARE(model.data(model.index(0, 0)).toString(), QString(""));