mirror of https://github.com/AMT-Cheif/drift.git
make transaction return value
This commit is contained in:
parent
c329c409d6
commit
c0d75f1654
|
@ -280,22 +280,23 @@ mixin QueryEngine on DatabaseConnectionUser {
|
|||
/// inside a transaction returns the parent transaction.
|
||||
@protected
|
||||
@visibleForTesting
|
||||
Future transaction(Future Function() action) async {
|
||||
Future<T> transaction<T>(Future<T> Function() action) async {
|
||||
final resolved = _resolvedEngine;
|
||||
if (resolved is Transaction) {
|
||||
return action();
|
||||
}
|
||||
|
||||
final executor = resolved.executor;
|
||||
await executor.doWhenOpened((executor) {
|
||||
return await executor.doWhenOpened((executor) {
|
||||
final transactionExecutor = executor.beginTransaction();
|
||||
final transaction = Transaction(this, transactionExecutor);
|
||||
|
||||
return _runEngineZoned(transaction, () async {
|
||||
var success = false;
|
||||
try {
|
||||
await action();
|
||||
final result = await action();
|
||||
success = true;
|
||||
return result;
|
||||
} catch (e) {
|
||||
await transactionExecutor.rollback();
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ class BeforeOpenEngine extends DatabaseConnectionUser with QueryEngine {
|
|||
|
||||
@override
|
||||
@alwaysThrows
|
||||
Future transaction(Function action) {
|
||||
Future<T> transaction<T>(Function action) {
|
||||
throw UnsupportedError("Transactions can't be started inside beforeOpen");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,4 +99,9 @@ void main() {
|
|||
verify(executor.doWhenOpened(any));
|
||||
});
|
||||
});
|
||||
|
||||
test('transaction return value', () async {
|
||||
final actual = await db.transaction(() async => 1);
|
||||
expect(actual, 1);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue