From 26450fe6a6340b09b1e3fa49c35028aa15ef223f Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Thu, 15 Mar 2012 11:23:27 +0100 Subject: [PATCH] QSqlTableModel::removeRows() enforce edit strategy For OnFieldChange and OnRowChange, we don't want more than one row in the cache with uncommitted changes. This could happen if deletion in the database fails while other changes are pending. Chosen solution is to return false if other rows have pending changes. Also, we only allow 1 row removed at a time. Updated test, changes and documentation. Change-Id: I68baf6d221789b4754e891535070011c759a2155 Reviewed-by: Honglei Zhang --- dist/changes-5.0.0 | 3 +++ src/sql/models/qsqltablemodel.cpp | 19 +++++++++++++++---- .../qsqltablemodel/tst_qsqltablemodel.cpp | 9 ++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index ebe8ad5331..03dc414e7f 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -413,6 +413,9 @@ model methods setData() or setRecord(). before doing anything. Previously, it would remove what it could and ignore the rest of the range. +* removeRows(), for OnFieldChange and OnRowChange, allows only 1 row to be +removed and only if there are no other changed rows. + * setRecord() and insertRecord() -Only use fields where generated flag is set to true. This is is consistent with the meaning of the flag. diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 153c2e80f9..d8d691fa0c 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -1021,11 +1021,18 @@ bool QSqlTableModel::removeColumns(int column, int count, const QModelIndex &par an invalid model index. When the edit strategy is OnManualSubmit, deletion of rows from - the database is delayed until submitAll() is called; otherwise, - deletions are immediate. + the database is delayed until submitAll() is called. - Inserted but not yet submitted rows in the range to be removed - are immediately removed from the model. + For OnFieldChange and OnRowChange, only one row may be deleted + at a time and only if no other row has a cached change. Deletions + are submitted immediately to the database. The model retains a + blank row for successfully deleted row until refreshed with select(). + + After failed deletion, the operation is not reverted in the model. + The application may resubmit or revert. + + Inserted but not yet successfully submitted rows in the range to be + removed are immediately removed from the model. Before a row is deleted from the database, the beforeDelete() signal is emitted. @@ -1047,6 +1054,10 @@ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent) else if (!count) return true; + if (d->strategy != OnManualSubmit) + if (count > 1 || (d->cache.value(row).submitted() && isDirty())) + return false; + // Iterate backwards so we don't have to worry about removed rows causing // higher cache entries to shift downwards. for (int idx = row + count - 1; idx >= row; --idx) { diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index 8bc71257e3..afe2c59144 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -592,6 +592,8 @@ void tst_QSqlTableModel::insertRowFailure() QCOMPARE(model.data(model.index(1, 1)).toString(), QString("blah")); QFAIL_SQL(model, insertRow(2)); QCOMPARE(model.rowCount(), 2); + QFAIL_SQL(model, removeRow(1)); + QCOMPARE(model.rowCount(), 2); } else { QVERIFY_SQL(model, setData(model.index(1, 1), QString("eggs"))); QCOMPARE(model.data(model.index(1, 1)).toString(), QString("eggs")); @@ -599,6 +601,8 @@ void tst_QSqlTableModel::insertRowFailure() QCOMPARE(model.data(model.index(1, 1)).toString(), QString("spam")); QVERIFY_SQL(model, insertRow(2)); QCOMPARE(model.rowCount(), 3); + QVERIFY_SQL(model, removeRow(1)); + QCOMPARE(model.rowCount(), 3); } // restore empty table @@ -795,8 +799,10 @@ void tst_QSqlTableModel::removeRows() QVERIFY(!model.removeRows(1, 0)); // zero count QVERIFY(!model.removeRows(5, 1)); // past end (DOESN'T causes a beforeDelete to be emitted) QVERIFY(!model.removeRows(1, 0, model.index(2, 0))); // can't pass a valid modelindex + QFAIL_SQL(model, removeRows(0, 2)); // more than 1 row on OnFieldChange - QVERIFY_SQL(model, removeRows(0, 2)); + QVERIFY_SQL(model, removeRows(0, 1)); + QVERIFY_SQL(model, removeRows(1, 1)); QCOMPARE(beforeDeleteSpy.count(), 2); QVERIFY(beforeDeleteSpy.at(0).at(0).toInt() == 0); QVERIFY(beforeDeleteSpy.at(1).at(0).toInt() == 1); @@ -1079,6 +1085,7 @@ void tst_QSqlTableModel::isDirty() QFAIL_SQL(model, setData(model.index(1, 1), QString("sam i am"))); QFAIL_SQL(model, setRecord(1, model.record(1))); QFAIL_SQL(model, insertRow(1)); + QFAIL_SQL(model, removeRow(1)); QFAIL_SQL(model, isDirty(model.index(1, 1))); model.revertAll();