apply method
Inserts, deletes, and updates database rows in a batch.
This method writes the mapping table edits to the database. These edits
must have been previously accumulated in a Transaction object and must
be passed as the transaction argument. If savepoint is true, the
edits of the transaction are written but are not committed.
If savepoint is false, which is the default behavior, the edits of the
transaction are written and are committed within a single SQLite
transaction together with all previously written but uncommitted edits, if
any, made by recent calls to apply with savepoint set to true.
If an Exception is thrown during the writing of an edit of the
transaction, then all edits of the transaction are rolled back and the
exception is rethrown. However, previously written but uncommitted edits,
if any, made by recent calls to apply with savepoint set to true,
are preserved via the SQLite savepoint mechanism and may still be
committed later by a successful apply method call.
The method returns the number of edits of the transaction that were
actually written and were not ignored by the database. The database may
ignore an edit if it deletes or updates a non-existent row, or inserts a
row with a key that is already used by an existing row.
If calling apply results in edits written to the mapping table, then the corresponding FTS indexes are synchronized automatically.
Implementation
int apply(Transaction transaction, {bool savepoint = false}) {
checkActive();
int count = 0;
try {
for (var entry in transaction._entries)
count += switch (entry) {
_TxUpdate _ => update(entry.key, entry.value) ? 1 : 0,
_TxDelete _ => delete(entry.key) ? 1 : 0,
_TxInsert _ => insert(entry.key, entry.value) ? 1 : 0,
};
} on Exception {
rollbackToSavepoint();
rethrow;
}
if (savepoint)
createSavepoint();
else
commitTransaction();
return count;
}