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. /// inside a transaction returns the parent transaction.
@protected @protected
@visibleForTesting @visibleForTesting
Future transaction(Future Function() action) async { Future<T> transaction<T>(Future<T> Function() action) async {
final resolved = _resolvedEngine; final resolved = _resolvedEngine;
if (resolved is Transaction) { if (resolved is Transaction) {
return action(); return action();
} }
final executor = resolved.executor; final executor = resolved.executor;
await executor.doWhenOpened((executor) { return await executor.doWhenOpened((executor) {
final transactionExecutor = executor.beginTransaction(); final transactionExecutor = executor.beginTransaction();
final transaction = Transaction(this, transactionExecutor); final transaction = Transaction(this, transactionExecutor);
return _runEngineZoned(transaction, () async { return _runEngineZoned(transaction, () async {
var success = false; var success = false;
try { try {
await action(); final result = await action();
success = true; success = true;
return result;
} catch (e) { } catch (e) {
await transactionExecutor.rollback(); await transactionExecutor.rollback();

View File

@ -13,7 +13,7 @@ class BeforeOpenEngine extends DatabaseConnectionUser with QueryEngine {
@override @override
@alwaysThrows @alwaysThrows
Future transaction(Function action) { Future<T> transaction<T>(Function action) {
throw UnsupportedError("Transactions can't be started inside beforeOpen"); throw UnsupportedError("Transactions can't be started inside beforeOpen");
} }
} }

View File

@ -99,4 +99,9 @@ void main() {
verify(executor.doWhenOpened(any)); verify(executor.doWhenOpened(any));
}); });
}); });
test('transaction return value', () async {
final actual = await db.transaction(() async => 1);
expect(actual, 1);
});
} }