toasty_core/driver/operation/
update_by_key.rs

1use super::Operation;
2
3use crate::{schema::db::TableId, stmt};
4
5/// Updates one or more records identified by primary key.
6///
7/// Used by key-value drivers. SQL drivers receive an equivalent `UPDATE`
8/// statement via [`QuerySql`](super::QuerySql) instead. Supports conditional
9/// updates and optionally returns the updated records.
10///
11/// # Examples
12///
13/// ```ignore
14/// use toasty_core::driver::operation::{UpdateByKey, Operation};
15///
16/// let op = UpdateByKey {
17///     table: table_id,
18///     keys: vec![key_value],
19///     assignments: assignments,
20///     filter: None,
21///     condition: None,
22///     returning: true,
23/// };
24/// let operation: Operation = op.into();
25/// ```
26#[derive(Debug, Clone)]
27pub struct UpdateByKey {
28    /// The table to update.
29    pub table: TableId,
30
31    /// Primary key values identifying the records to update.
32    pub keys: Vec<stmt::Value>,
33
34    /// Column assignments describing how to modify the records.
35    pub assignments: stmt::Assignments,
36
37    /// Optional filter expression. When set, only records whose key is in
38    /// `keys` *and* that match this filter are updated.
39    pub filter: Option<stmt::Expr>,
40
41    /// Optional precondition that must hold for the update to be applied.
42    /// Unlike `filter`, a failed condition typically causes an error rather
43    /// than silently skipping the row.
44    pub condition: Option<stmt::Expr>,
45
46    /// When `true`, the driver returns the full record for each updated row
47    /// in the [`ExecResponse`](super::super::ExecResponse).
48    pub returning: bool,
49}
50
51impl From<UpdateByKey> for Operation {
52    fn from(value: UpdateByKey) -> Self {
53        Self::UpdateByKey(value)
54    }
55}