pub struct Transaction { /* private fields */ }Expand description
Transaction represents a database transaction
Provides ACID guarantees for a series of database operations. Must be explicitly committed or rolled back.
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn execute<P: Params>(&mut self, sql: &str, params: P) -> Result<i64>
pub fn execute<P: Params>(&mut self, sql: &str, params: P) -> Result<i64>
Execute a SQL statement within the transaction
§Parameters
Parameters can be passed using:
- Empty tuple
()for no parameters - Tuple syntax
(1, "Alice", 30)for multiple parameters params!macroparams![1, "Alice", 30]
§Examples
ⓘ
let mut tx = db.begin()?;
tx.execute("INSERT INTO users VALUES ($1, $2)", (1, "Alice"))?;
tx.execute("UPDATE accounts SET balance = balance - $1 WHERE user_id = $2", (100, 1))?;
tx.commit()?;Sourcepub fn execute_prepared<P: Params>(
&mut self,
statement: &Statement,
params: P,
) -> Result<i64>
pub fn execute_prepared<P: Params>( &mut self, statement: &Statement, params: P, ) -> Result<i64>
Execute a pre-parsed statement with parameters.
Avoids re-parsing SQL on every call — ideal for batch operations where the same statement is executed many times with different params.
Use Parser::new(sql).parse_program() to pre-parse the SQL once.
Sourcepub fn query_opt<T: FromValue, P: Params>(
&mut self,
sql: &str,
params: P,
) -> Result<Option<T>>
pub fn query_opt<T: FromValue, P: Params>( &mut self, sql: &str, params: P, ) -> Result<Option<T>>
Sourcepub fn execute_named(&mut self, sql: &str, params: NamedParams) -> Result<i64>
pub fn execute_named(&mut self, sql: &str, params: NamedParams) -> Result<i64>
Sourcepub fn query_named(&mut self, sql: &str, params: NamedParams) -> Result<Rows>
pub fn query_named(&mut self, sql: &str, params: NamedParams) -> Result<Rows>
Execute a query with named parameters within the transaction
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Transaction
impl !RefUnwindSafe for Transaction
impl Send for Transaction
impl !Sync for Transaction
impl Unpin for Transaction
impl UnsafeUnpin for Transaction
impl !UnwindSafe for Transaction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CompactArcDrop for T
impl<T> CompactArcDrop for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more