1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use super::*;
impl Update {
/// Creates an update statement for a table or view source.
pub(crate) fn table(target: impl Into<Source>) -> Self {
Self {
ctes: Vec::new(),
target: target.into(),
assignments: Vec::new(),
from: Vec::new(),
filter: None,
returning: Vec::new(),
}
}
/// Adds a CTE to the update statement.
#[inline]
pub fn with(mut self, cte: Cte) -> Self {
self.ctes.push(cte);
self
}
/// Adds or replaces one `SET` assignment.
///
/// Later assignments for the same database column replace earlier ones, so
/// service code can layer server-owned values after a request changeset.
#[inline]
pub fn set(mut self, assignment: Assignment) -> Self {
push_assignment(&mut self.assignments, assignment);
self
}
/// Adds or replaces multiple `SET` assignments.
///
/// Later assignments for the same database column replace earlier ones.
pub fn set_many(mut self, assignments: impl IntoAssignments) -> Self {
extend_assignments(&mut self.assignments, assignments.into_assignments());
self
}
/// Adds one `SET` assignment only when `condition` is true.
#[inline]
pub fn set_if(self, condition: bool, assignment: Assignment) -> Self {
if condition {
self.set(assignment)
} else {
self
}
}
/// Adds one `SET` assignment built from an optional value.
pub fn set_option<T>(self, value: Option<T>, f: impl FnOnce(T) -> Assignment) -> Self {
match value {
Some(value) => self.set(f(value)),
None => self,
}
}
/// Applies assignments produced by a partial update [`Changeset`] DTO.
///
/// Assignments are merged with replacement semantics. Call `patch(&dto)`
/// first, then `set(...)` for authenticated/server-owned values that must
/// override request data.
pub fn patch(mut self, changes: impl Changeset) -> Self {
extend_assignments(&mut self.assignments, changes.changeset_assignments());
self
}
/// Adds a `FROM` source.
pub fn from(mut self, source: impl Into<Source>) -> Self {
self.from.push(source.into());
self
}
/// Adds one or more fields to `RETURNING`.
///
/// Use a field for one returned column or tuple syntax for a small
/// heterogeneous field list: `.returning((users::ID, users::EMAIL))`.
pub fn returning(mut self, fields: impl IntoColumns) -> Self {
self.returning.extend(fields.into_columns().items);
self
}
/// Adds an aliased expression to `RETURNING`.
pub fn returning_as(mut self, expr: impl Into<ValueExpr>, alias: impl Into<String>) -> Self {
self.returning.push(SelectItem {
expr: expr.into(),
alias: Some(alias.into()),
});
self
}
/// Replaces `RETURNING` with every field exposed by the target source.
#[inline]
pub fn returning_all(mut self) -> Self {
self.returning.clear();
push_returning_fields(&self.target, &mut self.returning);
self
}
}
impl_filter_methods!(Update);