mirror of
https://github.com/isar/rusqlite.git
synced 2025-03-31 19:12:58 +08:00
Merge pull request #1532 from seddonm1/feat/set-transaction-behavior
Allow setting default connection transaction behavior
This commit is contained in:
commit
ba019d747e
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -414,7 +414,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.
|
||||
@ -464,7 +464,7 @@ impl Connection {
|
||||
/// Will return `Err` if the underlying SQLite call fails. The specific
|
||||
/// error returned if transactions are nested is currently unspecified.
|
||||
pub fn unchecked_transaction(&self) -> Result<Transaction<'_>> {
|
||||
Transaction::new_unchecked(self, TransactionBehavior::Deferred)
|
||||
Transaction::new_unchecked(self, self.transaction_behavior)
|
||||
}
|
||||
|
||||
/// Begin a new savepoint with the default behavior (DEFERRED).
|
||||
@ -518,6 +518,34 @@ impl Connection {
|
||||
) -> Result<TransactionState> {
|
||||
self.db.borrow().txn_state(db_name)
|
||||
}
|
||||
|
||||
/// Set the default transaction behavior for the connection.
|
||||
///
|
||||
/// ## Note
|
||||
///
|
||||
/// This will only apply to transactions initiated by [`transaction`](Connection::transaction)
|
||||
/// or [`unchecked_transaction`](Connection::unchecked_transaction).
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use rusqlite::{Connection, Result, TransactionBehavior};
|
||||
/// # fn do_queries_part_1(_conn: &Connection) -> Result<()> { Ok(()) }
|
||||
/// # fn do_queries_part_2(_conn: &Connection) -> Result<()> { Ok(()) }
|
||||
/// fn perform_queries(conn: &mut Connection) -> Result<()> {
|
||||
/// conn.set_transaction_behavior(TransactionBehavior::Immediate);
|
||||
///
|
||||
/// let tx = conn.transaction()?;
|
||||
///
|
||||
/// do_queries_part_1(&tx)?; // tx causes rollback if this fails
|
||||
/// do_queries_part_2(&tx)?; // tx causes rollback if this fails
|
||||
///
|
||||
/// tx.commit()
|
||||
/// }
|
||||
/// ```
|
||||
pub fn set_transaction_behavior(&mut self, behavior: TransactionBehavior) {
|
||||
self.transaction_behavior = behavior;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user