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 are added with set,
insert, and remove.
The type parameter T is the returning type — it determines what
exec() produces, not which model is being updated. For example,
Update<User> returns the updated User (single-row update), 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.
By default, an update returns the changed records. Call
set_returning_none to suppress this.
Implementations§
Source§impl<T> Update<T>
impl<T> Update<T>
Sourcepub fn new(selection: Query<T>) -> Update<T>
pub fn new(selection: Query<T>) -> Update<T>
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 const fn from_untyped(untyped: Update) -> Update<T>
pub const fn from_untyped(untyped: Update) -> Update<T>
Wrap a raw untyped stmt::Update.
§Examples
use toasty::stmt::{List, Query, Update};
// Round-trip through an untyped update
let update = Update::<List<User>>::new(Query::<List<User>>::all());
let raw = update.into_untyped();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());
// Set field at index 1 (name) to "Bob"
update.set(1, toasty_core::stmt::Value::from("Bob"));Sourcepub fn insert(&mut self, field: impl Into<Projection>, expr: impl Into<Expr>)
pub fn insert(&mut self, field: impl Into<Projection>, expr: impl Into<Expr>)
Append a value to a collection field (e.g., a has-many relation).
§Examples
use toasty::stmt::{List, Query, Update};
let mut update = Update::<List<User>>::new(Query::<List<User>>::all());
update.insert(1, toasty_core::stmt::Value::from("new_tag"));Sourcepub fn remove(&mut self, field: impl Into<Projection>, expr: impl Into<Expr>)
pub fn remove(&mut self, field: impl Into<Projection>, expr: impl Into<Expr>)
Remove a value from a collection field.
§Examples
use toasty::stmt::{List, Query, Update};
let mut update = Update::<List<User>>::new(Query::<List<User>>::all());
update.remove(1, toasty_core::stmt::Value::from("old_tag"));Sourcepub fn set_returning_none(&mut self)
pub fn set_returning_none(&mut self)
Don’t return anything.
§Examples
use toasty::stmt::{List, Query, Update};
let mut update = Update::<List<User>>::new(Query::<List<User>>::all());
update.set_returning_none();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> Update<T>where
T: Load,
impl<T> Update<T>where
T: Load,
Sourcepub async fn exec(
self,
executor: &mut dyn Executor,
) -> Result<<T as Load>::Output, Error>
pub async fn exec( self, executor: &mut dyn Executor, ) -> Result<<T as Load>::Output, Error>
Execute this update statement against the given executor.
Returns the updated records unless
set_returning_none was called.
§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();