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 <honglei.zhang@nokia.com>
This commit is contained in:
Mark Brand 2012-03-15 11:23:27 +01:00 committed by Qt by Nokia
parent 108748404b
commit 26450fe6a6
3 changed files with 26 additions and 5 deletions

3
dist/changes-5.0.0 vendored
View File

@ -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.

View File

@ -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) {

View File

@ -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();