/*! Submits all pending changes and returns true on success. Returns false on error, detailed error information can be obtained with lastError(). On success the model will be repopulated. Any views presenting it will lose their selections. Note: In OnManualSubmit mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted again without losing data. \sa revertAll(), lastError() */ bool QSqlTableModel::submitAll() { Q_D(QSqlTableModel); switch (d->strategy) { case OnFieldChange: if (d->insertIndex == -1) return true; // else fall through case OnRowChange: if (d->editBuffer.isEmpty()) return true; if (d->insertIndex != -1) { if (!insertRowIntoTable(d->editBuffer)) return false; d->bottom = d->bottom.sibling(d->bottom.row() + 1, d->bottom.column()); } else { if (!updateRowInTable(d->editIndex, d->editBuffer)) return false; } d->clearEditBuffer(); d->editIndex = -1; d->insertIndex = -1; return select(); case OnManualSubmit: for (QSqlTableModelPrivate::CacheMap::ConstIterator it = d->cache.constBegin(); it != d->cache.constEnd(); ++it) { switch (it.value().op) { case QSqlTableModelPrivate::Insert: if (!insertRowIntoTable(it.value().rec)) return false; d->bottom = d->bottom.sibling(d->bottom.row() + 1, d->bottom.column()); break; case QSqlTableModelPrivate::Update: if (!updateRowInTable(it.key(), it.value().rec)) return false; break; case QSqlTableModelPrivate::Delete: if (!deleteRowFromTable(it.key())) return false; break; case QSqlTableModelPrivate::None: Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation"); break; } } d->clearCache(); return select(); } return false; }
/*! Submits all pending changes and returns \c true on success. Returns \c false on error, detailed error information can be obtained with lastError(). In OnManualSubmit, on success the model will be repopulated. Any views presenting it will lose their selections. Note: In OnManualSubmit mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted without losing data. \sa revertAll(), lastError() */ bool QSqlTableModel::submitAll() { Q_D(QSqlTableModel); bool success = true; foreach (int row, d->cache.keys()) { // be sure cache *still* contains the row since overridden selectRow() could have called select() QSqlTableModelPrivate::CacheMap::iterator it = d->cache.find(row); if (it == d->cache.end()) continue; QSqlTableModelPrivate::ModifiedRow &mrow = it.value(); if (mrow.submitted()) continue; switch (mrow.op()) { case QSqlTableModelPrivate::Insert: success = insertRowIntoTable(mrow.rec()); break; case QSqlTableModelPrivate::Update: success = updateRowInTable(row, mrow.rec()); break; case QSqlTableModelPrivate::Delete: success = deleteRowFromTable(row); break; case QSqlTableModelPrivate::None: Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation"); break; } if (success) { if (d->strategy != OnManualSubmit && mrow.op() == QSqlTableModelPrivate::Insert) { int c = mrow.rec().indexOf(d->autoColumn); if (c != -1 && !mrow.rec().isGenerated(c)) mrow.setValue(c, d->editQuery.lastInsertId()); } mrow.setSubmitted(); if (d->strategy != OnManualSubmit) success = selectRow(row); } if (!success) break; } if (success) { if (d->strategy == OnManualSubmit) success = select(); } return success; }
/*! Submits all pending changes and returns true on success. Returns false on error, detailed error information can be obtained with lastError(). In OnManualSubmit, on success the model will be repopulated. Any views presenting it will lose their selections. Note: In OnManualSubmit mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted without losing data. \sa revertAll(), lastError() */ bool QSqlTableModel::submitAll() { Q_D(QSqlTableModel); bool success = true; for (QSqlTableModelPrivate::CacheMap::Iterator it = d->cache.begin(); it != d->cache.end(); ++it) { if (it.value().submitted()) continue; switch (it.value().op()) { case QSqlTableModelPrivate::Insert: success = insertRowIntoTable(it.value().rec()); break; case QSqlTableModelPrivate::Update: success = updateRowInTable(it.key(), it.value().rec()); break; case QSqlTableModelPrivate::Delete: success = deleteRowFromTable(it.key()); break; case QSqlTableModelPrivate::None: Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation"); break; } if (success) { it.value().setSubmitted(); if (d->strategy != OnManualSubmit) success = selectRow(it.key()); } if (!success) break; } if (success) { if (d->strategy == OnManualSubmit) success = select(); } return success; }