make transaction return value

This commit is contained in:
Ryzhkov Sergey 2019-10-10 16:52:09 +03:00
parent c329c409d6
commit c0d75f1654
3 changed files with 10 additions and 4 deletions

View File

@ -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();

View File

@ -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");
}
}

View File

@ -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);
});
}