pub struct Update<T> { /* private fields */ }Expand description
A typed update statement.
Update modifies records matching a selection, typically derived from a
Query. Field assignments can be added with set or
populated by generated update builders.
The type parameter T is the returning type—it determines what exec()
produces, not which model is being updated. For example, Update<User>
returns one updated User, while Update<List<User>> returns the updated
records as Vec<User>.
Generated update-builders wrap this type and expose typed setter methods.
You rarely construct Update by hand.
Implementations§
Source§impl<T> Update<T>
impl<T> Update<T>
Sourcepub fn new(selection: Query<T>) -> Self
pub fn new(selection: Query<T>) -> Self
Create a new update statement from the given query selection.
All records matched by selection will be updated. By default the
update returns the changed records (Returning::Changed).
§Examples
use toasty::stmt::{List, Query, Update};
let update = Update::<List<User>>::new(Query::<List<User>>::all());Sourcepub fn as_untyped_mut(&mut self) -> &mut Update
pub fn as_untyped_mut(&mut self) -> &mut Update
Get a mutable reference to the underlying untyped update.
§Examples
use toasty::stmt::{List, Query, Update};
let mut update = Update::<List<User>>::new(Query::<List<User>>::all());
let raw = update.as_untyped_mut();
// Inspect or modify the raw update
assert!(raw.returning.is_some());Sourcepub fn set_assignments(&mut self, assignments: Assignments)
pub fn set_assignments(&mut self, assignments: Assignments)
Replace all field assignments with assignments.
§Examples
use toasty::stmt::{List, Query, Update};
let mut update = Update::<List<User>>::new(Query::<List<User>>::all());
update.set_assignments(toasty_core::stmt::Assignments::default());Sourcepub fn set(&mut self, field: impl Into<Projection>, expr: impl Into<Expr>)
pub fn set(&mut self, field: impl Into<Projection>, expr: impl Into<Expr>)
Assign a value to a field.
field identifies which field to update and expr is the new value.
§Examples
use toasty::stmt::{List, Query, Update};
let mut update = Update::<List<User>>::new(Query::<List<User>>::all());
update.set(1, toasty_core::stmt::Value::from("Bob"));Sourcepub fn into_untyped(self) -> Statement
pub fn into_untyped(self) -> Statement
Consume this typed update and return the untyped core statement.
§Examples
use toasty::stmt::{List, Query, Update};
let update = Update::<List<User>>::new(Query::<List<User>>::all());
let _raw = update.into_untyped();Source§impl<T: Load> Update<T>
impl<T: Load> Update<T>
Sourcepub async fn exec(self, executor: &mut dyn Executor) -> Result<T::Output>
pub async fn exec(self, executor: &mut dyn Executor) -> Result<T::Output>
Execute this update statement against the given executor.
§Examples
use toasty::stmt::{List, Query, Update};
let mut update = Update::<List<User>>::new(Query::<List<User>>::all());
update.set(1, toasty_core::stmt::Value::from("Bob"));
let _users: Vec<User> = update.exec(&mut db).await.unwrap();