@@ -18,7 +18,7 @@ use crate::{
18
18
19
19
use super :: {
20
20
cursor:: Cursor ,
21
- transaction_options:: { IsolationLevel , ReadVariant } ,
21
+ transaction_options:: { IsolationLevel , ReadVariant , SynchronousCommit } ,
22
22
} ;
23
23
use crate :: common:: ObjectQueryTrait ;
24
24
use std:: { collections:: HashSet , sync:: Arc } ;
@@ -30,6 +30,7 @@ pub trait TransactionObjectTrait {
30
30
isolation_level : Option < IsolationLevel > ,
31
31
read_variant : Option < ReadVariant > ,
32
32
defferable : Option < bool > ,
33
+ synchronous_commit : Option < SynchronousCommit > ,
33
34
) -> impl std:: future:: Future < Output = RustPSQLDriverPyResult < ( ) > > + Send ;
34
35
fn commit ( & self ) -> impl std:: future:: Future < Output = RustPSQLDriverPyResult < ( ) > > + Send ;
35
36
fn rollback ( & self ) -> impl std:: future:: Future < Output = RustPSQLDriverPyResult < ( ) > > + Send ;
@@ -41,6 +42,7 @@ impl TransactionObjectTrait for Object {
41
42
isolation_level : Option < IsolationLevel > ,
42
43
read_variant : Option < ReadVariant > ,
43
44
deferrable : Option < bool > ,
45
+ synchronous_commit : Option < SynchronousCommit > ,
44
46
) -> RustPSQLDriverPyResult < ( ) > {
45
47
let mut querystring = "START TRANSACTION" . to_string ( ) ;
46
48
@@ -60,12 +62,28 @@ impl TransactionObjectTrait for Object {
60
62
Some ( false ) => " NOT DEFERRABLE" ,
61
63
None => "" ,
62
64
} ) ;
65
+
63
66
self . batch_execute ( & querystring) . await . map_err ( |err| {
64
67
RustPSQLDriverError :: TransactionBeginError ( format ! (
65
68
"Cannot execute statement to start transaction, err - {err}"
66
69
) )
67
70
} ) ?;
68
71
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
+
69
87
Ok ( ( ) )
70
88
}
71
89
async fn commit ( & self ) -> RustPSQLDriverPyResult < ( ) > {
@@ -93,6 +111,7 @@ pub struct Transaction {
93
111
is_done : bool ,
94
112
95
113
isolation_level : Option < IsolationLevel > ,
114
+ synchronous_commit : Option < SynchronousCommit > ,
96
115
read_variant : Option < ReadVariant > ,
97
116
deferrable : Option < bool > ,
98
117
@@ -107,6 +126,7 @@ impl Transaction {
107
126
is_started : bool ,
108
127
is_done : bool ,
109
128
isolation_level : Option < IsolationLevel > ,
129
+ synchronous_commit : Option < SynchronousCommit > ,
110
130
read_variant : Option < ReadVariant > ,
111
131
deferrable : Option < bool > ,
112
132
savepoints_map : HashSet < String > ,
@@ -116,6 +136,7 @@ impl Transaction {
116
136
is_started,
117
137
is_done,
118
138
isolation_level,
139
+ synchronous_commit,
119
140
read_variant,
120
141
deferrable,
121
142
savepoints_map,
@@ -149,18 +170,26 @@ impl Transaction {
149
170
}
150
171
151
172
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
+ } ) ;
164
193
165
194
if is_started {
166
195
return Err ( RustPSQLDriverError :: TransactionBeginError (
@@ -176,7 +205,12 @@ impl Transaction {
176
205
177
206
if let Some ( db_client) = db_client {
178
207
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
+ )
180
214
. await ?;
181
215
182
216
Python :: with_gil ( |gil| {
@@ -558,18 +592,26 @@ impl Transaction {
558
592
/// 2) Transaction is done.
559
593
/// 3) Cannot execute `BEGIN` command.
560
594
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
+ } ) ;
573
615
574
616
if let Some ( db_client) = db_client {
575
617
if is_started {
@@ -584,7 +626,12 @@ impl Transaction {
584
626
) ) ;
585
627
}
586
628
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
+ )
588
635
. await ?;
589
636
590
637
pyo3:: Python :: with_gil ( |gil| {
0 commit comments