Allow setting default connection transaction behavior

This commit is contained in:
Mike Seddon 2024-07-17 17:05:47 +10:00
parent eebbbb0086
commit 1891c39a5f
2 changed files with 14 additions and 1 deletions

View File

@ -377,6 +377,7 @@ impl DatabaseName<'_> {
pub struct Connection {
db: RefCell<InnerConnection>,
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
})
}

View File

@ -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<'_>> {
Transaction::new(self, TransactionBehavior::Deferred)
Transaction::new(self, self.transaction_behavior)
}
/// Begin a new transaction with a specified behavior.