From 1891c39a5fd13b65ba3566a3d9ffdb2d8f0f257a Mon Sep 17 00:00:00 2001 From: Mike Seddon Date: Wed, 17 Jul 2024 17:05:47 +1000 Subject: [PATCH] Allow setting default connection transaction behavior --- src/lib.rs | 5 +++++ src/transaction.rs | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0f7eb16..3f5c862 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -377,6 +377,7 @@ impl DatabaseName<'_> { pub struct Connection { db: RefCell, cache: StatementCache, + transaction_behavior: TransactionBehavior } unsafe impl Send for Connection {} @@ -473,6 +474,7 @@ impl Connection { InnerConnection::open_with_flags(&c_path, flags, None).map(|db| Connection { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), + transaction_behavior: TransactionBehavior::Deferred }) } @@ -497,6 +499,7 @@ impl Connection { InnerConnection::open_with_flags(&c_path, flags, Some(&c_vfs)).map(|db| Connection { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), + transaction_behavior: TransactionBehavior::Deferred }) } @@ -949,6 +952,7 @@ impl Connection { Ok(Connection { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), + transaction_behavior: TransactionBehavior::Deferred }) } @@ -997,6 +1001,7 @@ impl Connection { Ok(Connection { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), + transaction_behavior: TransactionBehavior::Deferred }) } diff --git a/src/transaction.rs b/src/transaction.rs index 218d026..40e51d9 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -1,10 +1,18 @@ use crate::{Connection, Result}; use std::ops::Deref; +impl Connection { + /// Set the default transaction behavior for the connection. + pub fn set_transaction_behavior(&mut self, behavior: TransactionBehavior) { + self.transaction_behavior = behavior; + } +} + /// Options for transaction behavior. See [BEGIN /// TRANSACTION](http://www.sqlite.org/lang_transaction.html) for details. #[derive(Copy, Clone)] #[non_exhaustive] + pub enum TransactionBehavior { /// DEFERRED means that the transaction does not actually start until the /// database is first accessed. @@ -414,7 +422,7 @@ impl Connection { /// Will return `Err` if the underlying SQLite call fails. #[inline] pub fn transaction(&mut self) -> Result> { - Transaction::new(self, TransactionBehavior::Deferred) + Transaction::new(self, self.transaction_behavior) } /// Begin a new transaction with a specified behavior.