mirror of https://github.com/AMT-Cheif/drift.git
Ability to rollback transactions
This commit is contained in:
parent
2ee548e5d8
commit
147499c1f7
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue