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
use std::ops::Deref;

use crate::Result;

use super::Connection;

/// Transaction types that correlate to sqlite3 transactions and
/// additional ones introduced by libsql.
#[derive(Debug)]
pub enum TransactionBehavior {
    Deferred,
    Immediate,
    Exclusive,
    ReadOnly,
}

/// A transaction on some connection.
pub struct Transaction {
    pub(crate) inner: Box<dyn Tx + Send + Sync>,
    pub(crate) conn: Connection,
    /// An optional action executed whenever a transaction needs to be dropped.
    pub(crate) close: Option<Box<dyn FnOnce() + Send + Sync + 'static>>,
}

impl Transaction {
    /// Consume this transaction and commit.
    pub async fn commit(mut self) -> Result<()> {
        self.inner.commit().await
    }

    /// Consume this transaction and rollback.
    pub async fn rollback(mut self) -> Result<()> {
        self.inner.rollback().await
    }
}

impl Deref for Transaction {
    type Target = Connection;

    #[inline]
    fn deref(&self) -> &Connection {
        &self.conn
    }
}

impl Drop for Transaction {
    fn drop(&mut self) {
        if let Some(close) = self.close.take() {
            close();
        }
    }
}

#[async_trait::async_trait]
pub(crate) trait Tx {
    async fn commit(&mut self) -> Result<()>;
    async fn rollback(&mut self) -> Result<()>;
}