QSqlQueryModel: New method to re-execute the current query

Add refreshQuery() and refreshQuery(const QSqlDatabase &db) to refresh the model data.

[ChangeLog][QtSql][QSqlQueryModel] Added refreshQuery() and
refreshQuery(const QSqlDatabase &db) to refresh the model data.

Fixes: QTBUG-123603
Change-Id: I6738006dd8ca2fc82f3b4ad88b663c7b353a09f6
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Dheerendra Purohit 2024-11-05 20:14:44 +05:30 committed by Dheerendra Purohit
parent 365dd7d14c
commit 18ec9c7b62
3 changed files with 89 additions and 1 deletions

View File

@ -469,6 +469,35 @@ void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
setQuery(QSqlQuery(query, 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.
\sa setQuery(QSqlQuery &&query), QSqlQuery::boundValue()
*/
void QSqlQueryModel::refreshQuery()
{
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);
}
/*!
Clears the model and releases any acquired resource.
*/

View File

@ -46,6 +46,8 @@ public:
#endif
void setQuery(QSqlQuery &&query);
void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
void refreshQuery();
void refreshQuery(const QSqlDatabase &db);
#if QT_SQL_REMOVED_SINCE(6, 5)
QSqlQuery query() const;
#endif

View File

@ -59,7 +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();
private:
void generic_data(const QString &engine = QString());
void dropTestTables(const QSqlDatabase &db);
@ -660,5 +663,59 @@ void tst_QSqlQueryModel::task_QTBUG_4963_setHeaderDataWithProxyModel()
// And it should not crash.
}
void tst_QSqlQueryModel::refreshQuery()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
QSqlQueryModel model;
model.setQuery("SELECT * FROM " + qTableName("test", __FILE__, db), db);
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
QSqlQuery(db).exec("UPDATE " + qTableName("test", __FILE__, QSqlDatabase::database(dbName))
+ " SET name = 'updated_harry' WHERE id = 1");
model.refreshQuery(db);
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("updated_harry"));
QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
// Resetting the name to "harry" in the table
QSqlQuery(db).exec("UPDATE " + qTableName("test", __FILE__, QSqlDatabase::database(dbName))
+ " SET name = 'harry' WHERE id = 1");
}
void tst_QSqlQueryModel::refreshQueryWithBoundValues()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
QSqlQueryModel model;
QSqlQuery query(db);
query.prepare("SELECT name FROM " + qTableName("test", __FILE__, db) + " WHERE id = :id");
query.bindValue(":id", 1);
query.exec();
model.setQuery(query);
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.data(model.index(0, 0)).toString(), QString("harry"));
QSqlQuery(db).exec("UPDATE " + qTableName("test", __FILE__, QSqlDatabase::database(dbName))
+ " SET name = 'updated_harry' WHERE id = 1");
model.refreshQuery(db);
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(""));
// Resetting the name to "harry" in the table
QSqlQuery(db).exec("UPDATE " + qTableName("test", __FILE__, QSqlDatabase::database(dbName))
+ " SET name = 'harry' WHERE id = 1");
}
QTEST_MAIN(tst_QSqlQueryModel)
#include "tst_qsqlquerymodel.moc"