@@ -57,6 +57,21 @@ impl RustTransaction {
5757 }
5858 }
5959
60+ fn check_is_transaction_ready ( & self ) -> RustPSQLDriverPyResult < ( ) > {
61+ if !self . is_started {
62+ return Err ( RustPSQLDriverError :: DataBaseTransactionError (
63+ "Transaction is not started, please call begin() on transaction" . into ( ) ,
64+ ) ) ;
65+ }
66+ if self . is_done {
67+ return Err ( RustPSQLDriverError :: DataBaseTransactionError (
68+ "Transaction is already committed or rolled back" . into ( ) ,
69+ ) ) ;
70+ }
71+
72+ Ok ( ( ) )
73+ }
74+
6075 /// Execute querystring with parameters.
6176 ///
6277 /// Method doesn't acquire lock on any structure fields.
@@ -76,17 +91,7 @@ impl RustTransaction {
7691 parameters : Vec < PythonDTO > ,
7792 prepared : bool ,
7893 ) -> RustPSQLDriverPyResult < PSQLDriverPyQueryResult > {
79- if !self . is_started {
80- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
81- "Transaction is not started, please call begin() on transaction" . into ( ) ,
82- ) ) ;
83- }
84- if self . is_done {
85- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
86- "Transaction is already committed or rolled back" . into ( ) ,
87- ) ) ;
88- }
89-
94+ self . check_is_transaction_ready ( ) ?;
9095 self . connection
9196 . inner_execute ( querystring, parameters, prepared)
9297 . await
@@ -113,17 +118,7 @@ impl RustTransaction {
113118 parameters : Vec < PythonDTO > ,
114119 prepared : bool ,
115120 ) -> RustPSQLDriverPyResult < Vec < Row > > {
116- if !self . is_started {
117- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
118- "Transaction is not started, please call begin() on transaction" . into ( ) ,
119- ) ) ;
120- }
121- if self . is_done {
122- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
123- "Transaction is already committed or rolled back" . into ( ) ,
124- ) ) ;
125- }
126-
121+ self . check_is_transaction_ready ( ) ?;
127122 self . connection
128123 . inner_execute_raw ( querystring, parameters, prepared)
129124 . await
@@ -148,16 +143,7 @@ impl RustTransaction {
148143 parameters : Vec < Vec < PythonDTO > > ,
149144 prepared : bool ,
150145 ) -> RustPSQLDriverPyResult < ( ) > {
151- if !self . is_started {
152- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
153- "Transaction is not started, please call begin() on transaction" . into ( ) ,
154- ) ) ;
155- }
156- if self . is_done {
157- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
158- "Transaction is already committed or rolled back" . into ( ) ,
159- ) ) ;
160- }
146+ self . check_is_transaction_ready ( ) ?;
161147 if parameters. is_empty ( ) {
162148 return Err ( RustPSQLDriverError :: DataBaseTransactionError (
163149 "No parameters passed to execute_many" . into ( ) ,
@@ -192,17 +178,7 @@ impl RustTransaction {
192178 parameters : Vec < PythonDTO > ,
193179 prepared : bool ,
194180 ) -> RustPSQLDriverPyResult < PSQLDriverSinglePyQueryResult > {
195- if !self . is_started {
196- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
197- "Transaction is not started, please call begin() on transaction" . into ( ) ,
198- ) ) ;
199- }
200- if self . is_done {
201- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
202- "Transaction is already committed or rolled back" . into ( ) ,
203- ) ) ;
204- }
205-
181+ self . check_is_transaction_ready ( ) ?;
206182 self . connection
207183 . inner_fetch_row ( querystring, parameters, prepared)
208184 . await
@@ -302,17 +278,7 @@ impl RustTransaction {
302278 /// 2) Transaction is done
303279 /// 3) Cannot execute `COMMIT` command
304280 pub async fn inner_commit ( & mut self ) -> RustPSQLDriverPyResult < ( ) > {
305- if !self . is_started {
306- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
307- "Can not commit not started transaction" . into ( ) ,
308- ) ) ;
309- }
310-
311- if self . is_done {
312- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
313- "Transaction is already committed or rolled back" . into ( ) ,
314- ) ) ;
315- }
281+ self . check_is_transaction_ready ( ) ?;
316282 let db_client_guard = self . connection . db_client . read ( ) . await ;
317283 db_client_guard. batch_execute ( "COMMIT;" ) . await ?;
318284 self . is_done = true ;
@@ -332,17 +298,7 @@ impl RustTransaction {
332298 /// 3) Specified savepoint name is exists
333299 /// 4) Can not execute SAVEPOINT command
334300 pub async fn inner_savepoint ( & self , savepoint_name : String ) -> RustPSQLDriverPyResult < ( ) > {
335- if !self . is_started {
336- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
337- "Can not commit not started transaction" . into ( ) ,
338- ) ) ;
339- }
340-
341- if self . is_done {
342- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
343- "Transaction is already committed or rolled back" . into ( ) ,
344- ) ) ;
345- } ;
301+ self . check_is_transaction_ready ( ) ?;
346302
347303 let is_savepoint_name_exists = {
348304 let rollback_savepoint_read_guard = self . rollback_savepoint . read ( ) . await ;
@@ -373,17 +329,7 @@ impl RustTransaction {
373329 /// 2) Transaction is done
374330 /// 3) Can not execute ROLLBACK command
375331 pub async fn inner_rollback ( & mut self ) -> RustPSQLDriverPyResult < ( ) > {
376- if !self . is_started {
377- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
378- "Can not commit not started transaction" . into ( ) ,
379- ) ) ;
380- } ;
381-
382- if self . is_done {
383- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
384- "Transaction is already committed or rolled back" . into ( ) ,
385- ) ) ;
386- } ;
332+ self . check_is_transaction_ready ( ) ?;
387333 let db_client_guard = self . connection . db_client . read ( ) . await ;
388334 db_client_guard. batch_execute ( "ROLLBACK" ) . await ?;
389335 self . is_done = true ;
@@ -401,16 +347,7 @@ impl RustTransaction {
401347 /// 3) Specified savepoint name doesn't exist
402348 /// 4) Can not execute ROLLBACK TO SAVEPOINT command
403349 pub async fn inner_rollback_to ( & self , rollback_name : String ) -> RustPSQLDriverPyResult < ( ) > {
404- if !self . is_started {
405- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
406- "Can not commit not started transaction" . into ( ) ,
407- ) ) ;
408- } ;
409- if self . is_done {
410- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
411- "Transaction is already committed or rolled back" . into ( ) ,
412- ) ) ;
413- } ;
350+ self . check_is_transaction_ready ( ) ?;
414351
415352 let rollback_savepoint_arc = self . rollback_savepoint . clone ( ) ;
416353 let is_rollback_exists = {
@@ -444,16 +381,7 @@ impl RustTransaction {
444381 & self ,
445382 rollback_name : String ,
446383 ) -> RustPSQLDriverPyResult < ( ) > {
447- if !self . is_started {
448- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
449- "Can not commit not started transaction" . into ( ) ,
450- ) ) ;
451- } ;
452- if self . is_done {
453- return Err ( RustPSQLDriverError :: DataBaseTransactionError (
454- "Transaction is already committed or rolled back" . into ( ) ,
455- ) ) ;
456- } ;
384+ self . check_is_transaction_ready ( ) ?;
457385
458386 let mut rollback_savepoint_guard = self . rollback_savepoint . write ( ) . await ;
459387 let is_rollback_exists = rollback_savepoint_guard. remove ( & rollback_name) ;
0 commit comments