Skip to main content

Transaction

Struct Transaction 

Source
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

Source

pub fn id(&self) -> i64

Get the transaction ID

Source

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! macro params![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()?;
Source

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.

Source

pub fn query<P: Params>(&mut self, sql: &str, params: P) -> Result<Rows>

Execute a query within the transaction

§Examples
let mut tx = db.begin()?;
for row in tx.query("SELECT * FROM users WHERE age > $1", (18,))? {
    let row = row?;
    println!("{}", row.get::<String>("name")?);
}
tx.commit()?;
Source

pub fn query_one<T: FromValue, P: Params>( &mut self, sql: &str, params: P, ) -> Result<T>

Execute a query and return a single value

§Examples
let mut tx = db.begin()?;
let count: i64 = tx.query_one("SELECT COUNT(*) FROM users", ())?;
tx.commit()?;
Source

pub fn query_opt<T: FromValue, P: Params>( &mut self, sql: &str, params: P, ) -> Result<Option<T>>

Execute a query and return an optional single value

§Examples
let mut tx = db.begin()?;
let name: Option<String> = tx.query_opt("SELECT name FROM users WHERE id = $1", (999,))?;
tx.commit()?;
Source

pub fn execute_named(&mut self, sql: &str, params: NamedParams) -> Result<i64>

Execute a SQL statement with named parameters within the transaction

§Examples
use stoolap::named_params;

let mut tx = db.begin()?;
tx.execute_named(
    "INSERT INTO users VALUES (:id, :name)",
    named_params!{ id: 1, name: "Alice" }
)?;
tx.commit()?;
Source

pub fn query_named(&mut self, sql: &str, params: NamedParams) -> Result<Rows>

Execute a query with named parameters within the transaction

Source

pub fn commit(&mut self) -> Result<()>

Commit the transaction

All changes made within the transaction become permanent.

Source

pub fn rollback(&mut self) -> Result<()>

Roll back the transaction

All changes made within the transaction are discarded.

Trait Implementations§

Source§

impl Drop for Transaction

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CompactArcDrop for T

Source§

unsafe fn drop_and_dealloc(ptr: *mut u8)

Drop the contained data and deallocate the header+data allocation. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V