Skip to content

Commit

Permalink
QSqlQueryModel: add new function to refresh the model data
Browse files Browse the repository at this point in the history
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 <volker.hilsheimer@qt.io>
  • Loading branch information
chehrlic committed Dec 20, 2024
1 parent 5b07e3d commit 1bd883d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
23 changes: 5 additions & 18 deletions src/sql/models/qsqlquerymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/*!
Expand Down
3 changes: 1 addition & 2 deletions src/sql/models/qsqlquerymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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"));
Expand All @@ -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);
Expand All @@ -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(""));
Expand Down

0 comments on commit 1bd883d

Please sign in to comment.