Ability to rollback transactions

This commit is contained in:
Simon Binder 2019-07-23 08:49:51 +02:00
parent 2ee548e5d8
commit 147499c1f7
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 25 additions and 2 deletions

View File

@ -87,4 +87,8 @@ abstract class TransactionExecutor extends QueryExecutor {
/// Completes the transaction. No further queries may be sent to to this
/// [QueryExecutor] after this method was called.
Future<void> send();
/// Cancels this transaction. No further queries may be sent ot this
/// [QueryExecutor] after this method was called.
Future<void> rollback();
}

View File

@ -106,8 +106,15 @@ class NoTransactionDelegate extends TransactionDelegate {
/// The statement that commits a transaction on this database engine.
final String commit;
const NoTransactionDelegate(
{this.start = 'BEGIN TRANSACTION', this.commit = 'COMMIT TRANSACTION'});
/// The statement that will perform a rollback of a transaction on this
/// database engine.
final String rollback;
const NoTransactionDelegate({
this.start = 'BEGIN TRANSACTION',
this.commit = 'COMMIT TRANSACTION',
this.rollback = 'ROLLBACK TRANSACTION',
});
}
/// A [TransactionDelegate] for database APIs which do support creating and

View File

@ -98,6 +98,7 @@ class _TransactionExecutor extends TransactionExecutor
Completer<bool> _openingCompleter;
String _sendOnCommit;
String _sendOnRollback;
Future get completed => _sendCalled.future;
@ -130,6 +131,7 @@ class _TransactionExecutor extends TransactionExecutor
impl = _db.delegate;
await impl.runCustom(transactionManager.start, const []);
_sendOnCommit = transactionManager.commit;
_sendOnRollback = transactionManager.rollback;
transactionStarted.complete();
@ -162,6 +164,16 @@ class _TransactionExecutor extends TransactionExecutor
_sendCalled.complete();
}
@override
Future<void> rollback() async {
if (_sendOnRollback != null) {
await impl.runCustom(_sendOnRollback, const []);
}
_sendCalled.completeError(
Exception('artificial exception to rollback the transaction'));
}
}
class _BeforeOpeningExecutor extends QueryExecutor