Skip to main content

sql_orm_query/
update.rs

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