11from typing import Optional , Dict , List
22
3+ import mysql .connector .errors
4+
35from .mapper_manager import MapperManager
46from .cache import Cache , CacheKey
57
@@ -98,10 +100,14 @@ def update(self, id:str, params:dict) -> int:
98100 res = self .cache .clear ()
99101
100102 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
105111
106112 def delete (self , id :str , params :dict ) -> int :
107113 '''
@@ -114,10 +120,14 @@ def delete(self, id:str, params:dict) -> int:
114120 res = self .cache .clear ()
115121
116122 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
121131
122132 def insert (self , id :str , params :dict ) -> int :
123133 '''
@@ -130,10 +140,14 @@ def insert(self, id:str, params:dict) -> int:
130140 res = self .cache .clear ()
131141
132142 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
137151
138152
139153 def SelectOne (self , unparsed_sql :str ) -> Optional [Dict ]:
@@ -220,10 +234,14 @@ def wrapper(*args, **kwargs):
220234 res = self .cache .clear ()
221235
222236 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
227245 return wrapper
228246 return decorator
229247
@@ -240,10 +258,15 @@ def wrapper(*args, **kwargs):
240258 res = self .cache .clear ()
241259
242260 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+
247270 return wrapper
248271 return decorator
249272
@@ -260,10 +283,14 @@ def wrapper(*args, **kwargs):
260283 res = self .cache .clear ()
261284
262285 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
267294
268295 return wrapper
269296
0 commit comments