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
use conn::Conn;
use lib_futures::Future;
use self::futures::*;
use self::futures::query_result::*;
use std::fmt;
use value::FromRow;
use value::Params;
pub mod futures;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum IsolationLevel {
ReadUncommitted,
ReadCommitted,
RepeatableRead,
Serializable,
}
impl fmt::Display for IsolationLevel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
IsolationLevel::ReadUncommitted => write!(f, "READ UNCOMMITTED"),
IsolationLevel::ReadCommitted => write!(f, "READ COMMITTED"),
IsolationLevel::RepeatableRead => write!(f, "REPEATABLE READ"),
IsolationLevel::Serializable => write!(f, "SERIALIZABLE"),
}
}
}
pub struct Transaction {
conn: Conn,
}
impl Transaction {
fn new(mut conn: Conn) -> Transaction {
conn.in_transaction = true;
Transaction { conn: conn }
}
fn clean_conn(mut conn: Conn) -> Conn {
conn.in_transaction = false;
conn
}
fn new_raw(conn: Conn) -> Transaction {
Transaction { conn: conn }
}
pub fn commit(self) -> Commit {
new_commit(self)
}
pub fn rollback(self) -> Rollback {
new_rollback(self)
}
pub fn query<Q: AsRef<str>>(self, query: Q) -> TransQuery {
self.conn.query(query).map(new_text)
}
pub fn first<R, Q>(self, query: Q) -> TransFirst<R>
where R: FromRow,
Q: AsRef<str>,
{
fn map<R: FromRow>((row, conn): (Option<R>, Conn)) -> (Option<R>, Transaction) {
(row, Transaction::new_raw(conn))
}
self.conn.first(query).map(map)
}
pub fn prep_exec<Q: AsRef<str>, P: Into<Params>>(self, query: Q, params: P) -> TransPrepExec {
self.conn.prep_exec(query, params).map(new_bin)
}
pub fn first_exec<R, Q, P>(self, query: Q, params: P) -> TransFirstExec<R>
where R: FromRow,
Q: AsRef<str>,
P: Into<Params>,
{
fn map<R: FromRow>((row, conn): (Option<R>, Conn)) -> (Option<R>, Transaction) {
(row, Transaction::new_raw(conn))
}
self.conn.first_exec(query, params).map(map)
}
pub fn batch_exec<Q, P>(self, query: Q, params_vec: Vec<P>) -> TransBatchExec
where Q: AsRef<str>,
P: Into<Params>,
{
self.conn.batch_exec(query, params_vec).map(Transaction::new_raw)
}
}