@@ -18,7 +18,7 @@ use crate::{
1818
1919use super :: {
2020 cursor:: Cursor ,
21- transaction_options:: { IsolationLevel , ReadVariant } ,
21+ transaction_options:: { IsolationLevel , ReadVariant , SynchronousCommit } ,
2222} ;
2323use crate :: common:: ObjectQueryTrait ;
2424use std:: { collections:: HashSet , sync:: Arc } ;
@@ -30,6 +30,7 @@ pub trait TransactionObjectTrait {
3030 isolation_level : Option < IsolationLevel > ,
3131 read_variant : Option < ReadVariant > ,
3232 defferable : Option < bool > ,
33+ synchronous_commit : Option < SynchronousCommit > ,
3334 ) -> impl std:: future:: Future < Output = RustPSQLDriverPyResult < ( ) > > + Send ;
3435 fn commit ( & self ) -> impl std:: future:: Future < Output = RustPSQLDriverPyResult < ( ) > > + Send ;
3536 fn rollback ( & self ) -> impl std:: future:: Future < Output = RustPSQLDriverPyResult < ( ) > > + Send ;
@@ -41,6 +42,7 @@ impl TransactionObjectTrait for Object {
4142 isolation_level : Option < IsolationLevel > ,
4243 read_variant : Option < ReadVariant > ,
4344 deferrable : Option < bool > ,
45+ synchronous_commit : Option < SynchronousCommit > ,
4446 ) -> RustPSQLDriverPyResult < ( ) > {
4547 let mut querystring = "START TRANSACTION" . to_string ( ) ;
4648
@@ -60,12 +62,28 @@ impl TransactionObjectTrait for Object {
6062 Some ( false ) => " NOT DEFERRABLE" ,
6163 None => "" ,
6264 } ) ;
65+
6366 self . batch_execute ( & querystring) . await . map_err ( |err| {
6467 RustPSQLDriverError :: TransactionBeginError ( format ! (
6568 "Cannot execute statement to start transaction, err - {err}"
6669 ) )
6770 } ) ?;
6871
72+ if let Some ( synchronous_commit) = synchronous_commit {
73+ let str_synchronous_commit = synchronous_commit. to_str_level ( ) ;
74+
75+ let synchronous_commit_query =
76+ format ! ( "SET LOCAL synchronous_commit = '{str_synchronous_commit}'" ) ;
77+
78+ self . batch_execute ( & synchronous_commit_query)
79+ . await
80+ . map_err ( |err| {
81+ RustPSQLDriverError :: TransactionBeginError ( format ! (
82+ "Cannot set synchronous_commit parameter, err - {err}"
83+ ) )
84+ } ) ?;
85+ }
86+
6987 Ok ( ( ) )
7088 }
7189 async fn commit ( & self ) -> RustPSQLDriverPyResult < ( ) > {
@@ -93,6 +111,7 @@ pub struct Transaction {
93111 is_done : bool ,
94112
95113 isolation_level : Option < IsolationLevel > ,
114+ synchronous_commit : Option < SynchronousCommit > ,
96115 read_variant : Option < ReadVariant > ,
97116 deferrable : Option < bool > ,
98117
@@ -107,6 +126,7 @@ impl Transaction {
107126 is_started : bool ,
108127 is_done : bool ,
109128 isolation_level : Option < IsolationLevel > ,
129+ synchronous_commit : Option < SynchronousCommit > ,
110130 read_variant : Option < ReadVariant > ,
111131 deferrable : Option < bool > ,
112132 savepoints_map : HashSet < String > ,
@@ -116,6 +136,7 @@ impl Transaction {
116136 is_started,
117137 is_done,
118138 isolation_level,
139+ synchronous_commit,
119140 read_variant,
120141 deferrable,
121142 savepoints_map,
@@ -149,18 +170,26 @@ impl Transaction {
149170 }
150171
151172 async fn __aenter__ < ' a > ( self_ : Py < Self > ) -> RustPSQLDriverPyResult < Py < Self > > {
152- let ( is_started, is_done, isolation_level, read_variant, deferrable, db_client) =
153- pyo3:: Python :: with_gil ( |gil| {
154- let self_ = self_. borrow ( gil) ;
155- (
156- self_. is_started ,
157- self_. is_done ,
158- self_. isolation_level ,
159- self_. read_variant ,
160- self_. deferrable ,
161- self_. db_client . clone ( ) ,
162- )
163- } ) ;
173+ let (
174+ is_started,
175+ is_done,
176+ isolation_level,
177+ synchronous_commit,
178+ read_variant,
179+ deferrable,
180+ db_client,
181+ ) = pyo3:: Python :: with_gil ( |gil| {
182+ let self_ = self_. borrow ( gil) ;
183+ (
184+ self_. is_started ,
185+ self_. is_done ,
186+ self_. isolation_level ,
187+ self_. synchronous_commit ,
188+ self_. read_variant ,
189+ self_. deferrable ,
190+ self_. db_client . clone ( ) ,
191+ )
192+ } ) ;
164193
165194 if is_started {
166195 return Err ( RustPSQLDriverError :: TransactionBeginError (
@@ -176,7 +205,12 @@ impl Transaction {
176205
177206 if let Some ( db_client) = db_client {
178207 db_client
179- . start_transaction ( isolation_level, read_variant, deferrable)
208+ . start_transaction (
209+ isolation_level,
210+ read_variant,
211+ deferrable,
212+ synchronous_commit,
213+ )
180214 . await ?;
181215
182216 Python :: with_gil ( |gil| {
@@ -558,18 +592,26 @@ impl Transaction {
558592 /// 2) Transaction is done.
559593 /// 3) Cannot execute `BEGIN` command.
560594 pub async fn begin ( self_ : Py < Self > ) -> RustPSQLDriverPyResult < ( ) > {
561- let ( is_started, is_done, isolation_level, read_variant, deferrable, db_client) =
562- pyo3:: Python :: with_gil ( |gil| {
563- let self_ = self_. borrow ( gil) ;
564- (
565- self_. is_started ,
566- self_. is_done ,
567- self_. isolation_level ,
568- self_. read_variant ,
569- self_. deferrable ,
570- self_. db_client . clone ( ) ,
571- )
572- } ) ;
595+ let (
596+ is_started,
597+ is_done,
598+ isolation_level,
599+ synchronous_commit,
600+ read_variant,
601+ deferrable,
602+ db_client,
603+ ) = pyo3:: Python :: with_gil ( |gil| {
604+ let self_ = self_. borrow ( gil) ;
605+ (
606+ self_. is_started ,
607+ self_. is_done ,
608+ self_. isolation_level ,
609+ self_. synchronous_commit ,
610+ self_. read_variant ,
611+ self_. deferrable ,
612+ self_. db_client . clone ( ) ,
613+ )
614+ } ) ;
573615
574616 if let Some ( db_client) = db_client {
575617 if is_started {
@@ -584,7 +626,12 @@ impl Transaction {
584626 ) ) ;
585627 }
586628 db_client
587- . start_transaction ( isolation_level, read_variant, deferrable)
629+ . start_transaction (
630+ isolation_level,
631+ read_variant,
632+ deferrable,
633+ synchronous_commit,
634+ )
588635 . await ?;
589636
590637 pyo3:: Python :: with_gil ( |gil| {
0 commit comments