Skip to main content

sql_orm_query/
update.rs

1use crate::expr::TableRef;
2use crate::predicate::Predicate;
3use sql_orm_core::{Changeset, ColumnValue, Entity, EntityMetadata};
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct UpdateQuery {
7    pub table: TableRef,
8    pub changes: Vec<ColumnValue>,
9    pub predicate: Option<Predicate>,
10    pub allow_all_rows: bool,
11    pub entity: Option<&'static EntityMetadata>,
12}
13
14impl UpdateQuery {
15    pub fn for_entity<E: Entity, C: Changeset<E>>(changeset: &C) -> Self {
16        Self {
17            table: TableRef::for_entity::<E>(),
18            changes: changeset.changes(),
19            predicate: None,
20            allow_all_rows: false,
21            entity: Some(E::metadata()),
22        }
23    }
24
25    pub fn filter(mut self, predicate: Predicate) -> Self {
26        self.predicate = Some(match self.predicate.take() {
27            Some(existing) => Predicate::and(vec![existing, predicate]),
28            None => predicate,
29        });
30        self
31    }
32
33    pub const fn allow_all_rows(mut self) -> Self {
34        self.allow_all_rows = true;
35        self
36    }
37}