1
1
from typing import Optional , Dict , List
2
2
3
+ import mysql .connector .errors
4
+
3
5
from .mapper_manager import MapperManager
4
6
from .cache import Cache , CacheKey
5
7
@@ -98,10 +100,14 @@ def update(self, id:str, params:dict) -> int:
98
100
res = self .cache .clear ()
99
101
100
102
with self .conn .cursor (prepared = True ) as cursor :
101
- cursor .execute (sql , param_list )
102
- affected_rows = cursor .rowcount
103
- self .conn .commit ()
104
- return affected_rows
103
+ try :
104
+ cursor .execute (sql , param_list )
105
+ affected_rows = cursor .rowcount
106
+ self .conn .commit ()
107
+ return affected_rows
108
+ except mysql .connector .errors .Error as e :
109
+ self .conn .rollback ()
110
+ raise e
105
111
106
112
def delete (self , id :str , params :dict ) -> int :
107
113
'''
@@ -114,10 +120,14 @@ def delete(self, id:str, params:dict) -> int:
114
120
res = self .cache .clear ()
115
121
116
122
with self .conn .cursor (prepared = True ) as cursor :
117
- cursor .execute (sql , param_list )
118
- affected_rows = cursor .rowcount
119
- self .conn .commit ()
120
- return affected_rows
123
+ try :
124
+ cursor .execute (sql , param_list )
125
+ affected_rows = cursor .rowcount
126
+ self .conn .commit ()
127
+ return affected_rows
128
+ except mysql .connector .errors .Error as e :
129
+ self .conn .rollback ()
130
+ raise e
121
131
122
132
def insert (self , id :str , params :dict ) -> int :
123
133
'''
@@ -130,10 +140,14 @@ def insert(self, id:str, params:dict) -> int:
130
140
res = self .cache .clear ()
131
141
132
142
with self .conn .cursor (prepared = True ) as cursor :
133
- cursor .execute (sql , param_list )
134
- self .conn .commit ()
135
- last_id = cursor .lastrowid
136
- return last_id
143
+ try :
144
+ cursor .execute (sql , param_list )
145
+ self .conn .commit ()
146
+ last_id = cursor .lastrowid
147
+ return last_id
148
+ except mysql .connector .errors .Error as e :
149
+ self .conn .rollback ()
150
+ raise e
137
151
138
152
139
153
def SelectOne (self , unparsed_sql :str ) -> Optional [Dict ]:
@@ -220,10 +234,14 @@ def wrapper(*args, **kwargs):
220
234
res = self .cache .clear ()
221
235
222
236
with self .conn .cursor (prepared = True ) as cursor :
223
- cursor .execute (sql , param_list )
224
- self .conn .commit ()
225
- last_id = cursor .lastrowid
226
- return last_id
237
+ try :
238
+ cursor .execute (sql , param_list )
239
+ self .conn .commit ()
240
+ last_id = cursor .lastrowid
241
+ return last_id
242
+ except mysql .connector .errors .Error as e :
243
+ self .conn .rollback ()
244
+ raise e
227
245
return wrapper
228
246
return decorator
229
247
@@ -240,10 +258,15 @@ def wrapper(*args, **kwargs):
240
258
res = self .cache .clear ()
241
259
242
260
with self .conn .cursor (prepared = True ) as cursor :
243
- cursor .execute (sql , param_list )
244
- affected_rows = cursor .rowcount
245
- self .conn .commit ()
246
- return affected_rows
261
+ try :
262
+ cursor .execute (sql , param_list )
263
+ affected_rows = cursor .rowcount
264
+ self .conn .commit ()
265
+ return affected_rows
266
+ except mysql .connector .errors .Error as e :
267
+ self .conn .rollback ()
268
+ raise e
269
+
247
270
return wrapper
248
271
return decorator
249
272
@@ -260,10 +283,14 @@ def wrapper(*args, **kwargs):
260
283
res = self .cache .clear ()
261
284
262
285
with self .conn .cursor (prepared = True ) as cursor :
263
- cursor .execute (sql , param_list )
264
- affected_rows = cursor .rowcount
265
- self .conn .commit ()
266
- return affected_rows
286
+ try :
287
+ cursor .execute (sql , param_list )
288
+ affected_rows = cursor .rowcount
289
+ self .conn .commit ()
290
+ return affected_rows
291
+ except mysql .connector .errors .Error as e :
292
+ self .conn .rollback ()
293
+ raise e
267
294
268
295
return wrapper
269
296
0 commit comments