-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtraits.rs
More file actions
102 lines (83 loc) · 2.89 KB
/
traits.rs
File metadata and controls
102 lines (83 loc) · 2.89 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
use postgres_types::{ToSql, Type};
use tokio_postgres::{Row, Statement, ToStatement};
use crate::exceptions::rust_errors::PSQLPyResult;
use crate::options::{IsolationLevel, ReadVariant};
pub trait Connection {
fn prepare(
&self,
query: &str,
prepared: bool,
) -> impl std::future::Future<Output = PSQLPyResult<Statement>> + Send;
fn drop_prepared(
&self,
stmt: &Statement,
) -> impl std::future::Future<Output = PSQLPyResult<()>> + Send;
fn query<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl std::future::Future<Output = PSQLPyResult<Vec<Row>>>
where
T: ?Sized + ToStatement;
fn query_typed(
&self,
statement: &str,
params: &[(&(dyn ToSql + Sync), Type)],
) -> impl std::future::Future<Output = PSQLPyResult<Vec<Row>>>;
fn batch_execute(
&self,
query: &str,
) -> impl std::future::Future<Output = PSQLPyResult<()>> + Send;
fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl std::future::Future<Output = PSQLPyResult<Row>>
where
T: ?Sized + ToStatement;
}
pub trait Transaction {
fn build_start_qs(
&self,
isolation_level: Option<IsolationLevel>,
read_variant: Option<ReadVariant>,
deferrable: Option<bool>,
) -> String {
let mut querystring = "START TRANSACTION".to_string();
if let Some(level) = isolation_level {
let level = &level.to_str_level();
querystring.push_str(format!(" ISOLATION LEVEL {level}").as_str());
}
querystring.push_str(match read_variant {
Some(ReadVariant::ReadOnly) => " READ ONLY",
Some(ReadVariant::ReadWrite) => " READ WRITE",
None => "",
});
querystring.push_str(match deferrable {
Some(true) => " DEFERRABLE",
Some(false) => " NOT DEFERRABLE",
None => "",
});
querystring
}
fn _start_transaction(
&mut self,
isolation_level: Option<IsolationLevel>,
read_variant: Option<ReadVariant>,
deferrable: Option<bool>,
) -> impl std::future::Future<Output = PSQLPyResult<()>>;
fn _commit(&self) -> impl std::future::Future<Output = PSQLPyResult<()>>;
fn _rollback(&self) -> impl std::future::Future<Output = PSQLPyResult<()>>;
}
pub trait StartTransaction: Transaction {
fn start_transaction(
&mut self,
isolation_level: Option<IsolationLevel>,
read_variant: Option<ReadVariant>,
deferrable: Option<bool>,
) -> impl std::future::Future<Output = PSQLPyResult<()>>;
}
pub trait CloseTransaction: StartTransaction {
fn commit(&mut self) -> impl std::future::Future<Output = PSQLPyResult<()>>;
fn rollback(&mut self) -> impl std::future::Future<Output = PSQLPyResult<()>>;
}