-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from libsql/transactions
libsql/core: Add support for transactions
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use crate::{Connection, Result}; | ||
use std::ops::Deref; | ||
|
||
pub enum TransactionBehavior { | ||
Deferred, | ||
Immediate, | ||
Exclusive, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub enum DropBehavior { | ||
Rollback, | ||
Commit, | ||
Ignore, | ||
Panic, | ||
} | ||
|
||
pub struct Transaction { | ||
conn: Connection, | ||
drop_behavior: DropBehavior, | ||
} | ||
|
||
impl Drop for Transaction { | ||
fn drop(&mut self) { | ||
if self.conn.is_autocommit() { | ||
return; | ||
} | ||
match self.drop_behavior { | ||
DropBehavior::Rollback => { | ||
self.do_rollback().unwrap(); | ||
} | ||
DropBehavior::Commit => { | ||
self.do_commit().unwrap(); | ||
} | ||
DropBehavior::Ignore => {} | ||
DropBehavior::Panic => { | ||
if !thread::panicking() { | ||
panic!("Transaction dropped without being committed or rolled back"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Transaction { | ||
pub fn drop_behavior(&self) -> DropBehavior { | ||
self.drop_behavior | ||
} | ||
|
||
pub fn set_drop_behavior(&mut self, drop_behavior: DropBehavior) { | ||
self.drop_behavior = drop_behavior | ||
} | ||
|
||
/// Begin a new transaction in the given mode. | ||
pub(crate) fn begin(conn: Connection, tx_behavior: TransactionBehavior) -> Result<Self> { | ||
let begin_stmt = match tx_behavior { | ||
TransactionBehavior::Deferred => "BEGIN DEFERRED", | ||
TransactionBehavior::Immediate => "BEGIN IMMEDIATE", | ||
TransactionBehavior::Exclusive => "BEGIN EXCLUSIVE", | ||
}; | ||
let _ = conn.execute(begin_stmt, ())?; | ||
Ok(Self { conn, drop_behavior: DropBehavior::Rollback }) | ||
} | ||
|
||
/// Commit the transaction. | ||
pub fn commit(self) -> Result<()> { | ||
self.do_commit() | ||
} | ||
|
||
fn do_commit(&self) -> Result<()> { | ||
let _ = self.conn.execute("COMMIT", ())?; | ||
Ok(()) | ||
} | ||
|
||
/// Rollback the transaction. | ||
pub fn rollback(self) -> Result<()> { | ||
self.do_rollback() | ||
} | ||
|
||
fn do_rollback(&self) -> Result<()> { | ||
let _ = self.conn.execute("ROLLBACK", ())?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Deref for Transaction { | ||
type Target = Connection; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Connection { | ||
&self.conn | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters