@@ -2,13 +2,17 @@ import 'dart:async';
2
2
3
3
import 'package:dartseid_orm/src/core.dart' ;
4
4
import 'package:dartseid_orm/src/query.dart' ;
5
+ import 'package:meta/meta.dart' ;
5
6
6
7
abstract interface class DormAdapterBase {
7
- final dynamic _connection;
8
+ @protected
9
+ final dynamic connection;
10
+ @protected
11
+ late Dorm dorm;
8
12
9
13
DormAdapterBase ({
10
- required dynamic connection,
11
- }) : _connection = connection ;
14
+ required this . connection,
15
+ });
12
16
13
17
Future <void > init ();
14
18
Future <Map <String , dynamic >> operate ({
@@ -28,6 +32,9 @@ abstract interface class DormAdapterBase {
28
32
int ? limit,
29
33
int ? skip,
30
34
});
35
+
36
+ void setDormInstance (Dorm dorm);
37
+
31
38
DormTransaction transaction ();
32
39
}
33
40
@@ -37,47 +44,13 @@ abstract class DormTransaction {
37
44
bool isRolledBack = false ;
38
45
39
46
Future <void > commit () async {
40
- if (! isStarted) {
41
- throw DormException (
42
- message: "Cannot Commit a transaction that has not been started" ,
43
- originalError: null ,
44
- );
45
- }
46
- if (isCommitted) {
47
- throw DormException (
48
- message: "Cannot Commit a transaction that is already committed" ,
49
- originalError: null ,
50
- );
51
- }
52
- if (isRolledBack) {
53
- throw DormException (
54
- message: "Cannot Commit a transaction that has been rolled back" ,
55
- originalError: null ,
56
- );
57
- }
47
+ _checkPrecondition ("Commit" );
58
48
await _commit ();
59
49
isCommitted = true ;
60
50
}
61
51
62
52
Future <void > rollback () async {
63
- if (! isStarted) {
64
- throw DormException (
65
- message: "Cannot Rollback a transaction that has not been started" ,
66
- originalError: null ,
67
- );
68
- }
69
- if (isCommitted) {
70
- throw DormException (
71
- message: "Cannot Rollback a transaction that is already committed" ,
72
- originalError: null ,
73
- );
74
- }
75
- if (isRolledBack) {
76
- throw DormException (
77
- message: "Cannot Rollback a transaction that is already rolled back" ,
78
- originalError: null ,
79
- );
80
- }
53
+ _checkPrecondition ("Rollback" );
81
54
await _rollback ();
82
55
isRolledBack = true ;
83
56
}
@@ -86,6 +59,7 @@ abstract class DormTransaction {
86
59
FutureOr <T > Function (DormTransaction trx)? callback,
87
60
]) async {
88
61
try {
62
+ _checkStartPreCondition ();
89
63
await _start ();
90
64
isStarted = true ;
91
65
if (callback == null ) {
@@ -107,4 +81,40 @@ abstract class DormTransaction {
107
81
Future <void > _rollback ();
108
82
109
83
Future <void > _start ();
84
+
85
+ void _checkStartPreCondition () {
86
+ if (isCommitted) {
87
+ throw DormException (
88
+ message: "Cannot Start a transaction that is already committed" ,
89
+ originalError: null ,
90
+ );
91
+ }
92
+ if (isRolledBack) {
93
+ throw DormException (
94
+ message: "Cannot Start a transaction that is already rolled back" ,
95
+ originalError: null ,
96
+ );
97
+ }
98
+ }
99
+
100
+ void _checkPrecondition (String op) {
101
+ if (! isStarted) {
102
+ throw DormException (
103
+ message: "Cannot $op a transaction that has not been started" ,
104
+ originalError: null ,
105
+ );
106
+ }
107
+ if (isCommitted) {
108
+ throw DormException (
109
+ message: "Cannot $op a transaction that is already committed" ,
110
+ originalError: null ,
111
+ );
112
+ }
113
+ if (isRolledBack) {
114
+ throw DormException (
115
+ message: "Cannot $op a transaction that has been rolled back" ,
116
+ originalError: null ,
117
+ );
118
+ }
119
+ }
110
120
}
0 commit comments