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
//! `Transaction` is a structure representing an interactive transaction.

use crate::{Client, ResultSet, Statement, SyncClient};
use anyhow::Result;

pub struct Transaction<'a> {
    pub(crate) id: u64,
    pub(crate) client: &'a Client,
}

impl<'a> Transaction<'a> {
    pub async fn new(client: &'a Client, id: u64) -> Result<Transaction<'a>> {
        client
            .execute_in_transaction(id, Statement::from("BEGIN"))
            .await?;
        Ok(Self { id, client })
    }

    /// Executes a statement within the current transaction.
    /// # Example
    ///
    /// ```rust,no_run
    ///   # async fn f() -> anyhow::Result<()> {
    ///   # use crate::libsql_client::{Statement, args};
    ///   let mut db = libsql_client::Client::from_env().await?;
    ///   let tx = db.transaction().await?;
    ///   tx.execute(Statement::with_args("INSERT INTO users (name) VALUES (?)", args!["John"])).await?;
    ///   let res = tx.execute(Statement::with_args("INSERT INTO users (name) VALUES (?)", args!["Jane"])).await;
    ///   if res.is_err() {
    ///     tx.rollback().await?;
    ///   } else {
    ///     tx.commit().await?;
    ///   }
    ///   # Ok(())
    ///   # }
    /// ```
    pub async fn execute(&self, stmt: impl Into<Statement>) -> Result<ResultSet> {
        self.client
            .execute_in_transaction(self.id, stmt.into())
            .await
    }

    /// Commits the transaction to the database.
    pub async fn commit(self) -> Result<()> {
        self.client.commit_transaction(self.id).await
    }

    /// Rolls back the transaction, cancelling any of its side-effects.
    pub async fn rollback(self) -> Result<()> {
        self.client.rollback_transaction(self.id).await
    }
}

pub struct SyncTransaction<'a> {
    pub(crate) id: u64,
    pub(crate) client: &'a SyncClient,
}

impl<'a> SyncTransaction<'a> {
    pub fn new(client: &'a SyncClient, id: u64) -> Result<SyncTransaction<'a>> {
        client.execute_in_transaction(id, Statement::from("BEGIN"))?;
        Ok(Self { id, client })
    }

    /// Executes a statement within the current transaction.
    /// # Example
    ///
    /// ```rust,no_run
    ///   # fn f() -> anyhow::Result<()> {
    ///   # use crate::libsql_client::{Statement, args};
    ///   let mut db = libsql_client::SyncClient::from_env()?;
    ///   let tx = db.transaction()?;
    ///   tx.execute(Statement::with_args("INSERT INTO users (name) VALUES (?)", args!["John"]))?;
    ///   let res = tx.execute(Statement::with_args("INSERT INTO users (name) VALUES (?)", args!["Jane"]));
    ///   if res.is_err() {
    ///     tx.rollback()?;
    ///   } else {
    ///     tx.commit()?;
    ///   }
    ///   # Ok(())
    ///   # }
    /// ```
    pub fn execute(&self, stmt: impl Into<Statement>) -> Result<ResultSet> {
        self.client.execute_in_transaction(self.id, stmt.into())
    }

    /// Commits the transaction to the database.
    pub fn commit(self) -> Result<()> {
        self.client.commit_transaction(self.id)
    }

    /// Rolls back the transaction, cancelling any of its side-effects.
    pub fn rollback(self) -> Result<()> {
        self.client.rollback_transaction(self.id)
    }
}