From 7e19bb2a470465b3efec2b8cdfd0f204f949f759 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 17 Aug 2019 17:53:23 -0400 Subject: [PATCH] Add a required `QueryExecutor` argument to `transaction` callbacks. --- angel_orm/CHANGELOG.md | 2 ++ angel_orm/example/main.dart | 2 +- angel_orm/lib/src/query.dart | 4 ++-- angel_orm/lib/src/query_executor.dart | 11 +++++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/angel_orm/CHANGELOG.md b/angel_orm/CHANGELOG.md index 3de6a208..1b4e73e8 100644 --- a/angel_orm/CHANGELOG.md +++ b/angel_orm/CHANGELOG.md @@ -1,6 +1,8 @@ # 2.1.0 * Split the formerly 600+ line `src/query.dart` up into separate files. +* **BREAKING**: Add a required `QueryExecutor` argument to `transaction` +callbacks. # 2.0.1 * Apply `package:pedantic` fixes. diff --git a/angel_orm/example/main.dart b/angel_orm/example/main.dart index 719500f6..fbf6866d 100644 --- a/angel_orm/example/main.dart +++ b/angel_orm/example/main.dart @@ -34,7 +34,7 @@ class _FakeExecutor extends QueryExecutor { } @override - Future transaction(FutureOr Function() f) { + Future transaction(FutureOr Function(QueryExecutor) f) { throw UnsupportedError('Transactions are not supported.'); } } diff --git a/angel_orm/lib/src/query.dart b/angel_orm/lib/src/query.dart index 5427ef18..0a56b015 100644 --- a/angel_orm/lib/src/query.dart +++ b/angel_orm/lib/src/query.dart @@ -263,9 +263,9 @@ abstract class Query extends QueryBase { fields.map(adornWithTableName).toList()) .then((it) => it.map(deserialize).toList()); } else { - return executor.transaction(() async { + return executor.transaction((tx) async { // TODO: Can this be done with just *one* query? - var existing = await get(executor); + var existing = await get(tx); //var sql = compile(preamble: 'SELECT $tableName.id', withFields: false); return executor .query(tableName, sql, substitutionValues) diff --git a/angel_orm/lib/src/query_executor.dart b/angel_orm/lib/src/query_executor.dart index 558a4d9d..ec6448c0 100644 --- a/angel_orm/lib/src/query_executor.dart +++ b/angel_orm/lib/src/query_executor.dart @@ -11,6 +11,13 @@ abstract class QueryExecutor { String tableName, String query, Map substitutionValues, [List returningFields]); - /// Begins a database transaction. - Future transaction(FutureOr f()); + /// Enters a database transaction, performing the actions within, + /// and returning the results of [f]. + /// + /// If [f] fails, the transaction will be rolled back, and the + /// responsible exception will be re-thrown. + /// + /// Whether nested transactions are supported depends on the + /// underlying driver. + Future transaction(FutureOr Function(QueryExecutor) f); }