-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtransaction.rs
More file actions
364 lines (323 loc) · 11.6 KB
/
transaction.rs
File metadata and controls
364 lines (323 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use std::sync::Arc;
use futures::future;
use pyo3::{
pyclass, pymethods,
types::{PyAnyMethods, PyList, PyTuple},
Py, PyAny, PyErr, PyResult,
};
use tokio::sync::RwLock;
use tokio_postgres::Config;
use crate::{
connection::{
structs::PSQLPyConnection,
traits::{CloseTransaction, Connection, StartTransaction as _},
},
exceptions::rust_errors::{PSQLPyResult, RustPSQLDriverError},
options::{IsolationLevel, ReadVariant},
query_result::{PSQLDriverPyQueryResult, PSQLDriverSinglePyQueryResult},
};
#[pyclass(subclass)]
#[derive(Debug)]
pub struct Transaction {
pub conn: Option<Arc<RwLock<PSQLPyConnection>>>,
pub pg_config: Arc<Config>,
isolation_level: Option<IsolationLevel>,
read_variant: Option<ReadVariant>,
deferrable: Option<bool>,
}
impl Transaction {
#[must_use]
pub fn new(
conn: Option<Arc<RwLock<PSQLPyConnection>>>,
pg_config: Arc<Config>,
isolation_level: Option<IsolationLevel>,
read_variant: Option<ReadVariant>,
deferrable: Option<bool>,
) -> Self {
Self {
conn,
pg_config,
isolation_level,
read_variant,
deferrable,
}
}
}
#[pymethods]
impl Transaction {
#[must_use]
pub fn __aiter__(self_: Py<Self>) -> Py<Self> {
self_
}
fn __await__(self_: Py<Self>) -> Py<Self> {
self_
}
async fn __aenter__(self_: Py<Self>) -> PSQLPyResult<Py<Self>> {
let (isolation_level, read_variant, deferrable, conn) = pyo3::Python::with_gil(|gil| {
let self_ = self_.borrow(gil);
(
self_.isolation_level,
self_.read_variant,
self_.deferrable,
self_.conn.clone(),
)
});
let Some(conn) = conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let mut write_conn_g = conn.write().await;
write_conn_g
.start_transaction(isolation_level, read_variant, deferrable)
.await?;
Ok(self_)
}
#[allow(clippy::needless_pass_by_value)]
async fn __aexit__(
self_: Py<Self>,
_exception_type: Py<PyAny>,
exception: Py<PyAny>,
_traceback: Py<PyAny>,
) -> PSQLPyResult<()> {
let (conn, is_exception_none, py_err) = pyo3::Python::with_gil(|gil| {
let self_ = self_.borrow(gil);
(
self_.conn.clone(),
exception.is_none(gil),
PyErr::from_value(exception.into_bound(gil)),
)
});
let Some(conn) = conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let mut write_conn_g = conn.write().await;
if is_exception_none {
write_conn_g.commit().await?;
pyo3::Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.conn = None;
});
Ok(())
} else {
write_conn_g.rollback().await?;
pyo3::Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.conn = None;
});
Err(RustPSQLDriverError::RustPyError(py_err))
}
}
/// Begin the transaction.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
pub async fn begin(&mut self) -> PSQLPyResult<()> {
let conn = &self.conn;
let Some(conn) = conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let mut write_conn_g = conn.write().await;
write_conn_g
.start_transaction(self.isolation_level, self.read_variant, self.deferrable)
.await?;
Ok(())
}
/// Execute querystring with parameters.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
#[pyo3(signature = (querystring, parameters=None, prepared=None))]
pub async fn execute(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> PSQLPyResult<PSQLDriverPyQueryResult> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g.execute(querystring, parameters, prepared).await
}
/// Execute querystring with parameters.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
#[pyo3(signature = (querystring, parameters=None, prepared=None))]
pub async fn fetch(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> PSQLPyResult<PSQLDriverPyQueryResult> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g.execute(querystring, parameters, prepared).await
}
/// Execute querystring with parameters and return single value.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
/// Or if query returns more than one value.
#[pyo3(signature = (querystring, parameters=None, prepared=None))]
pub async fn fetch_val(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> PSQLPyResult<Py<PyAny>> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g
.fetch_val(querystring, parameters, prepared)
.await
}
/// Executes a sequence of SQL statements using the simple query protocol.
///
/// Statements should be separated by semicolons.
/// If an error occurs, execution of the sequence will stop at that point.
/// This is intended for use when, for example, initializing a database schema.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
pub async fn execute_batch(&self, querystring: String) -> PSQLPyResult<()> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g.batch_execute(&querystring).await
}
/// Executes one query with different parameters.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
#[pyo3(signature = (querystring, parameters=None, prepared=None))]
pub async fn execute_many(
&self,
querystring: String,
parameters: Option<Vec<Py<PyAny>>>,
prepared: Option<bool>,
) -> PSQLPyResult<()> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g
.execute_many(querystring, parameters, prepared)
.await
}
/// Executes query and return one row.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
#[pyo3(signature = (querystring, parameters=None, prepared=None))]
pub async fn fetch_row(
&self,
querystring: String,
parameters: Option<Py<PyAny>>,
prepared: Option<bool>,
) -> PSQLPyResult<PSQLDriverSinglePyQueryResult> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g
.fetch_row(querystring, parameters, prepared)
.await
}
/// Create new savepoint in a transaction.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
pub async fn create_savepoint(&mut self, savepoint_name: String) -> PSQLPyResult<()> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g
.batch_execute(format!("SAVEPOINT {savepoint_name}").as_str())
.await?;
Ok(())
}
/// Release a savepoint in a transaction.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
pub async fn release_savepoint(&mut self, savepoint_name: String) -> PSQLPyResult<()> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g
.batch_execute(format!("RELEASE SAVEPOINT {savepoint_name}").as_str())
.await?;
Ok(())
}
/// Rollback to a savepoint in a transaction.
///
/// # Errors
/// Can return error if there is a problem with DB communication.
pub async fn rollback_savepoint(&mut self, savepoint_name: String) -> PSQLPyResult<()> {
let Some(conn) = &self.conn else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let read_conn_g = conn.read().await;
read_conn_g
.batch_execute(format!("ROLLBACK TO SAVEPOINT {savepoint_name}").as_str())
.await?;
Ok(())
}
/// Execute many queries in a transaction.
///
/// More information in a documentation:
/// `<https://psqlpy-python.github.io/components/transaction.html#pipeline>`
///
/// # Errors
/// Can return error if there is a problem with DB communication.
#[allow(for_loops_over_fallibles)]
#[pyo3(signature = (queries=None, prepared=None))]
pub async fn pipeline(
self_: Py<Self>,
queries: Option<Py<PyList>>,
prepared: Option<bool>,
) -> PSQLPyResult<Vec<PSQLDriverPyQueryResult>> {
let db_client = pyo3::Python::with_gil(|gil| {
let self_ = self_.borrow(gil);
self_.conn.clone()
});
if let Some(db_client) = db_client {
let conn_read_g = db_client.read().await;
let mut futures = vec![];
if let Some(queries) = queries {
let gil_result = pyo3::Python::with_gil(|gil| -> PyResult<()> {
for single_query in queries.into_bound(gil).try_iter() {
let query_tuple = single_query.downcast::<PyTuple>().map_err(|err| {
RustPSQLDriverError::PyToRustValueConversionError(format!(
"Cannot cast to tuple: {err}",
))
})?;
let querystring = query_tuple.get_item(0)?.extract::<String>()?;
let params = match query_tuple.get_item(1) {
Ok(param) => Some(param.into()),
Err(_) => None,
};
futures.push(conn_read_g.execute(querystring, params, prepared));
}
Ok(())
});
match gil_result {
Ok(()) => {}
Err(e) => {
// Handle PyO3 error, convert to your error type as needed
return Err(RustPSQLDriverError::from(e)); // Adjust according to your error types
}
}
}
return future::try_join_all(futures).await;
}
Err(RustPSQLDriverError::TransactionClosedError)
}
}