Transaction

Struct Transaction 

Source
pub struct Transaction<S> { /* private fields */ }
Expand description

Transaction can be used to query and update the database.

From the perspective of a Transaction each other Transaction is fully applied or not at all. Futhermore, the effects of Transactions have a global order. So if we have mutations A and then B, it is impossible for a Transaction to see the effect of B without seeing the effect of A.

Implementations§

Source§

impl<S> Transaction<S>

Source

pub fn query<'t, F, R>(&'t self, f: F) -> R
where F: for<'inner> FnOnce(&mut Query<'t, 'inner, S>) -> R,

Execute a query with multiple results.

let user_names = txn.query(|rows| {
    let user = rows.join(User);
    rows.into_vec(&user.name)
});
assert_eq!(user_names, vec!["Alice".to_owned()]);
Source

pub fn query_one<O: 'static>( &self, val: impl IntoSelect<'static, S, Out = O>, ) -> O

Retrieve a single result from the database.

let res = txn.query_one("test".into_expr());
assert_eq!(res, "test");

Instead of using Self::query_one in a loop, it is better to call Self::query and return all results at once.

Source

pub fn lazy<'t, T: MyTyp>( &'t self, val: impl IntoExpr<'static, S, Typ = T>, ) -> T::Lazy<'t>

Source

pub fn lazy_iter<'t, T: Table<Schema = S>>( &'t self, val: impl Joinable<'static, Typ = T>, ) -> LazyIter<'t, T>

Source§

impl<S: 'static> Transaction<S>

Source

pub fn insert<T: Table<Schema = S>>( &mut self, val: impl TableInsert<T = T>, ) -> Result<TableRow<T>, T::Conflict>

Try inserting a value into the database.

Returns Ok with a reference to the new inserted value or an Err with conflict information. The type of conflict information depends on the number of unique constraints on the table:

  • 0 unique constraints => Infallible
  • 1 unique constraint => Expr reference to the conflicting table row.
  • 2+ unique constraints => () no further information is provided.
let res = txn.insert(User {
    name: "Bob",
});
assert!(res.is_ok());
let res = txn.insert(User {
    name: "Bob",
});
assert!(res.is_err(), "there is a unique constraint on the name");
Source

pub fn insert_ok<T: Table<Schema = S, Conflict = Infallible>>( &mut self, val: impl TableInsert<T = T>, ) -> TableRow<T>

This is a convenience function to make using Transaction::insert easier for tables without unique constraints.

The new row is added to the table and the row reference is returned.

Source

pub fn find_or_insert<T: Table<Schema = S, Conflict = TableRow<T>>>( &mut self, val: impl TableInsert<T = T>, ) -> TableRow<T>

This is a convenience function to make using Transaction::insert easier for tables with exactly one unique constraints.

The new row is inserted and the reference to the row is returned OR an existing row is found which conflicts with the new row and a reference to the conflicting row is returned.

let bob = txn.insert(User {
    name: "Bob",
}).unwrap();
let bob2 = txn.find_or_insert(User {
    name: "Bob", // this will conflict with the existing row.
});
assert_eq!(bob, bob2);
Source

pub fn update<T: Table<Schema = S>>( &mut self, row: impl IntoExpr<'static, S, Typ = T>, val: T::Update, ) -> Result<(), T::Conflict>

Try updating a row in the database to have new column values.

Updating can fail just like Transaction::insert because of unique constraint conflicts. This happens when the new values are in conflict with an existing different row.

When the update succeeds, this function returns Ok, when it fails it returns Err with one of three conflict types:

  • 0 unique constraints => Infallible
  • 1 unique constraint => Expr reference to the conflicting table row.
  • 2+ unique constraints => () no further information is provided.
let bob = txn.insert(User {
    name: "Bob",
}).unwrap();
txn.update(bob, User {
    name: Update::set("New Bob"),
}).unwrap();
Source

pub fn update_ok<T: Table<Schema = S>>( &mut self, row: impl IntoExpr<'static, S, Typ = T>, val: T::UpdateOk, )

This is a convenience function to use Transaction::update for updates that can not cause unique constraint violations.

This method can be used for all tables, it just does not allow modifying columns that are part of unique constraints.

Source

pub fn downgrade(&'static mut self) -> &'static mut TransactionWeak<S>

Convert the Transaction into a TransactionWeak to allow deletions.

Auto Trait Implementations§

§

impl<S> Freeze for Transaction<S>

§

impl<S> RefUnwindSafe for Transaction<S>
where S: RefUnwindSafe,

§

impl<S> !Send for Transaction<S>

§

impl<S> !Sync for Transaction<S>

§

impl<S> Unpin for Transaction<S>
where S: Unpin,

§

impl<S> UnwindSafe for Transaction<S>
where S: UnwindSafe,

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