pub struct TransactionMut<'t, S> { /* private fields */ }
Expand description
Same as Transaction, but allows inserting new rows.
TransactionMut always uses the latest version of the database, with the effects of all previous TransactionMuts applied.
To make mutations to the database permanent you need to use TransactionMut::commit. This is to make sure that if a function panics while holding a mutable transaction, it will roll back those changes.
Implementations§
Source§impl<'t, S: 'static> TransactionMut<'t, S>
impl<'t, S: 'static> TransactionMut<'t, S>
Sourcepub fn insert<T: Table<Schema = S>>(
&mut self,
val: impl TableInsert<'t, T = T>,
) -> Result<TableRow<'t, T>, T::Conflict<'t>>
pub fn insert<T: Table<Schema = S>>( &mut self, val: impl TableInsert<'t, T = T>, ) -> Result<TableRow<'t, T>, T::Conflict<'t>>
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");
Sourcepub fn insert_ok<T: Table<Schema = S, Conflict<'t> = Infallible>>(
&mut self,
val: impl TableInsert<'t, T = T>,
) -> TableRow<'t, T>
pub fn insert_ok<T: Table<Schema = S, Conflict<'t> = Infallible>>( &mut self, val: impl TableInsert<'t, T = T>, ) -> TableRow<'t, T>
This is a convenience function to make using TransactionMut::insert easier for tables without unique constraints.
The new row is added to the table and the row reference is returned.
Sourcepub fn find_or_insert<T: Table<Schema = S, Conflict<'t> = TableRow<'t, T>>>(
&mut self,
val: impl TableInsert<'t, T = T>,
) -> TableRow<'t, T>
pub fn find_or_insert<T: Table<Schema = S, Conflict<'t> = TableRow<'t, T>>>( &mut self, val: impl TableInsert<'t, T = T>, ) -> TableRow<'t, T>
This is a convenience function to make using TransactionMut::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);
Sourcepub fn update<T: Table<Schema = S>>(
&mut self,
row: impl IntoExpr<'t, S, Typ = T>,
val: T::Update<'t>,
) -> Result<(), T::Conflict<'t>>
pub fn update<T: Table<Schema = S>>( &mut self, row: impl IntoExpr<'t, S, Typ = T>, val: T::Update<'t>, ) -> Result<(), T::Conflict<'t>>
Try updating a row in the database to have new column values.
Updating can fail just like TransactionMut::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();
Sourcepub fn update_ok<T: Table<Schema = S>>(
&mut self,
row: impl IntoExpr<'t, S, Typ = T>,
val: T::UpdateOk<'t>,
)
pub fn update_ok<T: Table<Schema = S>>( &mut self, row: impl IntoExpr<'t, S, Typ = T>, val: T::UpdateOk<'t>, )
This is a convenience function to use TransactionMut::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.
Sourcepub fn commit(self)
pub fn commit(self)
Make the changes made in this TransactionMut permanent.
If the TransactionMut is dropped without calling this function, then the changes are rolled back.
Sourcepub fn downgrade(self) -> TransactionWeak<'t, S>
pub fn downgrade(self) -> TransactionWeak<'t, S>
Convert the TransactionMut into a TransactionWeak to allow deletions.
Methods from Deref<Target = Transaction<'t, S>>§
Sourcepub fn query<F, R>(&self, f: F) -> R
pub fn query<F, R>(&self, f: F) -> 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()]);
Sourcepub fn query_one<'e, O>(&self, val: impl IntoSelect<'t, 't, S, Out = O>) -> O
pub fn query_one<'e, O>(&self, val: impl IntoSelect<'t, 't, 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.
Trait Implementations§
Auto Trait Implementations§
impl<'t, S> Freeze for TransactionMut<'t, S>
impl<'t, S> !RefUnwindSafe for TransactionMut<'t, S>
impl<'t, S> !Send for TransactionMut<'t, S>
impl<'t, S> !Sync for TransactionMut<'t, S>
impl<'t, S> Unpin for TransactionMut<'t, S>where
S: Unpin,
impl<'t, S> !UnwindSafe for TransactionMut<'t, S>
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
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);