Skip to main content

toasty_core/driver/operation/
update_by_key.rs

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