Skip to main content

TransactionScope

Struct TransactionScope 

Source
pub struct TransactionScope<S: 'static> { /* private fields */ }
Expand description

Transaction with mutation support and without downgrade support.

This type can be created using Transaction::scope.

Implementations§

Source§

impl<S> TransactionScope<S>

Source

pub fn mutable<'t, T: OptTable<Schema = S>>( &'t mut self, val: impl IntoExpr<'static, S, Typ = T>, ) -> T::Mutable<'t>

Retrieves a Mutable or Option<Mutable> from the database.

The Transaction is borrowed mutably until the Mutable is dropped.

txn.scope(|txn| {
let baz_id = txn.insert(Player {number: 1, name: "Baz".to_owned(), score: 0}).unwrap();

let mut tmp = txn.mutable(baz_id);
tmp.score += 50;
tmp.name = format!("{}{}", tmp.name, tmp.score);

if let Some(mut player) = txn.mutable(Player.number(1)) {
    player.score += 100;
}
Source

pub fn mutable_vec<'t, T: Table<Schema = S>>( &'t mut self, val: impl IntoJoinable<'static, S, Typ = TableRow<T>>, ) -> Vec<Mutable<'t, T>>

Retrieve multiple Mutable rows from the database.

Refer to crate::args::Rows::join for the kind of the parameter that is supported here. This may be useful when you need mutable access to multiple rows (potentially at the same time).

Getting a lazy Iterator over mutable rows instead of a Vec is not possible, because mutating while iterating can result in duplicate rows.

for mut user in txn.mutable_vec(User.age(20)) {
    user.age += 1;
}

Methods from Deref<Target = Transaction<S>>§

Source

pub fn scope<O>(&mut self, f: impl FnOnce(&mut TransactionScope<S>) -> O) -> O

Create a transaction scope that limits the lifetime of crate::Mutable.

Source

pub fn query<'t, R>(&'t self, f: impl FnOnce(&mut Query<'t, '_, S>) -> R) -> 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: OptTable<Schema = S>>( &'t self, val: impl IntoExpr<'static, S, Typ = T>, ) -> T::Lazy<'t>

Retrieve a crate::Lazy or Option<Lazy> from the database.

This is very similar to Self::query_one, except that it retrieves crate::Lazy instead of TableRow. As such it only works with table valued rust_query::Expr.

let cat = txn.insert_ok(Author {
    name: "Cat".to_owned()
});
let blog_post = txn.insert_ok(Page {
    content: "Hello world!".to_owned(),
    title: "Hi".to_owned(),
    author: cat,
});
let lazy_post = txn.lazy(blog_post);

println!("{}:", lazy_post.title);
println!("{}", lazy_post.content);
println!("written by: {}", lazy_post.author.name);
Source

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

This retrieves an iterator of crate::Lazy values.

Refer to Rows::join for the kind of the parameter that is supported here. Refer to Transaction::lazy for the single row version.

Source

pub fn insert<T: Table<Schema = S>>( &mut self, val: 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:

let res = txn.insert(User {
    name: "Bob".to_owned(),
});
assert!(res.is_ok());
let res = txn.insert(User {
    name: "Bob".to_owned(),
});
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: 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: 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".to_owned(),
}).unwrap();
let bob2 = txn.find_or_insert(User {
    name: "Bob".to_owned(), // this will conflict with the existing row.
});
assert_eq!(bob, bob2);
Source

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

Convert the Transaction into a TransactionWeak to allow deletions.

Trait Implementations§

Source§

impl<S> Deref for TransactionScope<S>

Source§

type Target = Transaction<S>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S> DerefMut for TransactionScope<S>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<S> !Freeze for TransactionScope<S>

§

impl<S> !RefUnwindSafe for TransactionScope<S>

§

impl<S> !Send for TransactionScope<S>

§

impl<S> !Sync for TransactionScope<S>

§

impl<S> !UnwindSafe for TransactionScope<S>

§

impl<S> Unpin for TransactionScope<S>
where PhantomData<&'static Transaction<S>>: Unpin,

§

impl<S> UnsafeUnpin for TransactionScope<S>
where PhantomData<&'static Transaction<S>>: UnsafeUnpin,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.