@@ -10,6 +10,8 @@ use crate::{
1010 value_converter:: { convert_parameters, PythonDTO } ,
1111} ;
1212
13+ use super :: transaction_options:: IsolationLevel ;
14+
1315/// Transaction for internal use only.
1416///
1517/// It is not exposed to python.
@@ -18,6 +20,8 @@ pub struct RustTransaction {
1820 pub is_started : Arc < tokio:: sync:: RwLock < bool > > ,
1921 pub is_done : Arc < tokio:: sync:: RwLock < bool > > ,
2022 pub rollback_savepoint : Arc < tokio:: sync:: RwLock < HashSet < String > > > ,
23+
24+ pub isolation_level : Option < IsolationLevel > ,
2125}
2226
2327impl RustTransaction {
@@ -73,6 +77,27 @@ impl RustTransaction {
7377 Ok ( PSQLDriverPyQueryResult :: new ( result) )
7478 }
7579
80+ /// Start transaction with isolation level if specified
81+ ///
82+ /// # Errors:
83+ /// May return Err Result if cannot execute querystring.
84+ pub async fn start_transaction < ' a > ( & ' a self ) -> RustPSQLDriverPyResult < ( ) > {
85+ let mut querystring = "START TRANSACTION" . to_string ( ) ;
86+
87+ if let Some ( level) = self . isolation_level {
88+ querystring. push_str ( " ISOLATION LEVEL " ) ;
89+ let level = & level. to_str_level ( ) ;
90+ querystring. push_str ( level) ;
91+ } ;
92+
93+ let db_client_arc = self . db_client . clone ( ) ;
94+ let db_client_guard = db_client_arc. read ( ) . await ;
95+
96+ db_client_guard. batch_execute ( & querystring) . await ?;
97+
98+ Ok ( ( ) )
99+ }
100+
76101 /// Start the transaction.
77102 ///
78103 /// Execute `BEGIN` commands and mark transaction as `started`.
@@ -84,7 +109,6 @@ impl RustTransaction {
84109 /// 2) Transaction is done.
85110 /// 3) Cannot execute `BEGIN` command.
86111 pub async fn inner_begin < ' a > ( & ' a self ) -> RustPSQLDriverPyResult < ( ) > {
87- let db_client_arc = self . db_client . clone ( ) ;
88112 let is_started_arc = self . is_started . clone ( ) ;
89113 let is_done_arc = self . is_done . clone ( ) ;
90114
@@ -108,8 +132,7 @@ impl RustTransaction {
108132 ) ) ;
109133 }
110134
111- let db_client_guard = db_client_arc. read ( ) . await ;
112- db_client_guard. batch_execute ( "BEGIN" ) . await ?;
135+ self . start_transaction ( ) . await ?;
113136 let mut is_started_write_guard = is_started_arc. write ( ) . await ;
114137 * is_started_write_guard = true ;
115138
0 commit comments