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
mod builder;
mod portal;

use super::{
    client::ClientBorrowMut,
    driver::codec::AsParams,
    error::Error,
    iter::slice_iter,
    prepare::Prepare,
    query::{Query, RowStream},
    statement::{Statement, StatementGuarded},
    types::{ToSql, Type},
};

pub use builder::TransactionBuilder;
pub use portal::Portal;

pub struct Transaction<'a, C>
where
    C: Prepare + Query + ClientBorrowMut,
{
    client: &'a mut C,
    save_point: SavePoint,
    state: State,
}

enum SavePoint {
    None,
    Auto { depth: u32 },
    Custom { name: String, depth: u32 },
}

impl SavePoint {
    fn nest_save_point(&self, name: Option<String>) -> Self {
        match *self {
            Self::None => match name {
                Some(name) => SavePoint::Custom { name, depth: 1 },
                None => SavePoint::Auto { depth: 1 },
            },
            Self::Auto { depth } | Self::Custom { depth, .. } => match name {
                Some(name) => SavePoint::Custom { name, depth },
                None => SavePoint::Auto { depth: depth + 1 },
            },
        }
    }

    fn save_point_query(&self) -> String {
        match self {
            Self::None => "SAVEPOINT".to_string(),
            Self::Auto { depth } => format!("SAVEPOINT sp_{depth}"),
            Self::Custom { name, .. } => format!("SAVEPOINT {name}"),
        }
    }

    fn commit_query(&self) -> String {
        match self {
            Self::None => "COMMIT".to_string(),
            Self::Auto { depth } => format!("RELEASE sp_{depth}"),
            Self::Custom { name, .. } => format!("RELEASE {name}"),
        }
    }

    fn rollback_query(&self) -> String {
        match self {
            Self::None => "ROLLBACK".to_string(),
            Self::Auto { depth } => format!("ROLLBACK TO sp_{depth}"),
            Self::Custom { name, .. } => format!("ROLLBACK TO {name}"),
        }
    }
}

enum State {
    WantRollback,
    Finish,
}

impl<C> Drop for Transaction<'_, C>
where
    C: Prepare + Query + ClientBorrowMut,
{
    fn drop(&mut self) {
        match self.state {
            State::WantRollback => self.do_rollback(),
            State::Finish => {}
        }
    }
}

impl<C> Transaction<'_, C>
where
    C: Prepare + Query + ClientBorrowMut,
{
    pub fn builder() -> TransactionBuilder {
        TransactionBuilder::new()
    }

    /// function the same as [`Client::prepare`]
    ///
    /// [`Client::prepare`]: crate::client::Client::prepare
    pub async fn prepare(&self, query: &str, types: &[Type]) -> Result<StatementGuarded<C>, Error> {
        self.client
            ._prepare(query, types)
            .await
            .map(|stmt| stmt.into_guarded(self.client))
    }

    /// function the same as [`Client::query`]
    ///
    /// [`Client::query`]: crate::client::Client::query
    #[inline]
    pub fn query<'a>(&self, stmt: &'a Statement, params: &[&(dyn ToSql + Sync)]) -> Result<RowStream<'a>, Error> {
        self.client._query(stmt, params)
    }

    /// function the same as [`Client::query_raw`]
    ///
    /// [`Client::query_raw`]: crate::client::Client::query_raw
    #[inline]
    pub fn query_raw<'a, I>(&self, stmt: &'a Statement, params: I) -> Result<RowStream<'a>, Error>
    where
        I: AsParams,
    {
        self.client._query_raw(stmt, params)
    }

    /// Binds a statement to a set of parameters, creating a [`Portal`] which can be incrementally queried.
    ///
    /// Portals only last for the duration of the transaction in which they are created, and can only be used on the
    /// connection that created them.
    pub async fn bind<'p>(
        &'p self,
        statement: &'p Statement,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<Portal<'p, C>, Error> {
        self.bind_raw(statement, slice_iter(params)).await
    }

    /// A maximally flexible version of [`Transaction::bind`].
    pub async fn bind_raw<'p, I>(&'p self, statement: &'p Statement, params: I) -> Result<Portal<'p, C>, Error>
    where
        I: AsParams,
    {
        Portal::new(self.client, statement, params).await
    }

    /// Like [`Client::transaction`], but creates a nested transaction via a savepoint.
    ///     
    /// [`Client::transaction`]: crate::client::Client::transaction
    pub async fn transaction(&mut self) -> Result<Transaction<C>, Error> {
        self._save_point(None).await
    }

    /// Like [`Client::transaction`], but creates a nested transaction via a savepoint with the specified name.
    ///
    /// [`Client::transaction`]: crate::client::Client::transaction
    pub async fn save_point<I>(&mut self, name: I) -> Result<Transaction<C>, Error>
    where
        I: Into<String>,
    {
        self._save_point(Some(name.into())).await
    }

    /// Consumes the transaction, committing all changes made within it.
    pub async fn commit(mut self) -> Result<(), Error> {
        self.state = State::Finish;
        let query = self.save_point.commit_query();
        self.client._execute_raw::<_, crate::ZeroParam>(&query, []).await?;
        Ok(())
    }

    /// Rolls the transaction back, discarding all changes made within it.
    ///
    /// This is equivalent to [`Transaction`]'s [`Drop`] implementation, but provides any error encountered to the caller.
    pub async fn rollback(mut self) -> Result<(), Error> {
        self.state = State::Finish;
        let query = self.save_point.rollback_query();
        self.client._execute_raw::<_, crate::ZeroParam>(&query, []).await?;
        Ok(())
    }

    fn new(client: &mut C) -> Transaction<C> {
        Transaction {
            client,
            save_point: SavePoint::None,
            state: State::WantRollback,
        }
    }

    async fn _save_point(&mut self, name: Option<String>) -> Result<Transaction<C>, Error> {
        let save_point = self.save_point.nest_save_point(name);
        self.client
            ._execute_raw::<_, crate::ZeroParam>(&save_point.save_point_query(), [])
            .await?;

        Ok(Transaction {
            client: self.client,
            save_point,
            state: State::WantRollback,
        })
    }

    fn do_rollback(&mut self) {
        let query = self.save_point.rollback_query();
        drop(self.client._execute_raw::<_, crate::ZeroParam>(&query, []));
    }
}