-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinner_transaction.rs
More file actions
94 lines (85 loc) · 3.12 KB
/
inner_transaction.rs
File metadata and controls
94 lines (85 loc) · 3.12 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
use deadpool_postgres::Transaction as dp_Transaction;
use postgres_types::ToSql;
use tokio_postgres::{Portal, Row, ToStatement, Transaction as tp_Transaction};
use crate::exceptions::rust_errors::PSQLPyResult;
pub enum PsqlpyTransaction {
PoolTrans(dp_Transaction<'static>),
SingleConnTrans(tp_Transaction<'static>),
}
impl PsqlpyTransaction {
async fn commit(self) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => Ok(p_txid.commit().await?),
PsqlpyTransaction::SingleConnTrans(s_txid) => Ok(s_txid.commit().await?),
}
}
async fn rollback(self) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => Ok(p_txid.rollback().await?),
PsqlpyTransaction::SingleConnTrans(s_txid) => Ok(s_txid.rollback().await?),
}
}
async fn savepoint(&mut self, sp_name: &str) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => {
p_txid.savepoint(sp_name).await?;
Ok(())
}
PsqlpyTransaction::SingleConnTrans(s_txid) => {
s_txid.savepoint(sp_name).await?;
Ok(())
}
}
}
async fn release_savepoint(&self, sp_name: &str) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => {
p_txid
.batch_execute(format!("RELEASE SAVEPOINT {sp_name}").as_str())
.await?;
Ok(())
}
PsqlpyTransaction::SingleConnTrans(s_txid) => {
s_txid
.batch_execute(format!("RELEASE SAVEPOINT {sp_name}").as_str())
.await?;
Ok(())
}
}
}
async fn rollback_savepoint(&self, sp_name: &str) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => {
p_txid
.batch_execute(format!("ROLLBACK TO SAVEPOINT {sp_name}").as_str())
.await?;
Ok(())
}
PsqlpyTransaction::SingleConnTrans(s_txid) => {
s_txid
.batch_execute(format!("ROLLBACK TO SAVEPOINT {sp_name}").as_str())
.await?;
Ok(())
}
}
}
async fn bind<T>(&self, statement: &T, params: &[&(dyn ToSql + Sync)]) -> PSQLPyResult<Portal>
where
T: ?Sized + ToStatement,
{
match self {
PsqlpyTransaction::PoolTrans(p_txid) => Ok(p_txid.bind(statement, params).await?),
PsqlpyTransaction::SingleConnTrans(s_txid) => {
Ok(s_txid.bind(statement, params).await?)
}
}
}
pub async fn query_portal(&self, portal: &Portal, size: i32) -> PSQLPyResult<Vec<Row>> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => Ok(p_txid.query_portal(portal, size).await?),
PsqlpyTransaction::SingleConnTrans(s_txid) => {
Ok(s_txid.query_portal(portal, size).await?)
}
}
}
}