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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::clause;
use crate::expr::Expr;
use crate::item::Ident;
use crate::ops::and;
/// `UPDATE` statement builder.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Update<'a> {
pub(crate) with: Option<clause::With<'a>>,
pub(crate) table: clause::Update<'a>,
pub(crate) set: clause::Set<'a>,
pub(crate) from: Option<clause::From<'a>>,
pub(crate) filter: Option<clause::Where<'a>>,
pub(crate) returns: Option<clause::Returning<'a>>,
}
stmt_common!(Update);
crate::macros::gen_display!(Update<'_>);
impl<'a> Update<'a> {
pub fn set<C, V>(mut self, column: C, value: V) -> Update<'a>
where
C: Into<Ident<'a>>,
V: Into<Expr<'a>>,
{
self.set.0.push((column.into(), value.into()));
self
}
pub fn set_values<V>(mut self, values: V) -> Update<'a>
where
V: Into<clause::Set<'a>>,
{
self.set.0.extend(values.into().0);
self
}
pub fn from<T>(mut self, tables: T) -> Update<'a>
where
T: Into<clause::From<'a>>,
{
self.from = match self.from.take() {
Some(mut inner) => {
inner.0.extend(tables.into().0);
Some(inner)
}
None => Some(tables.into()),
};
self
}
/// Set condition to `WHERE` clause.
///
/// Successive calls combine new condition with previous condition with
/// [`and`](crate::ops::and).
///
/// # Examples
///
/// ```
/// use qians_xql::update;
/// use qians_xql::and;
/// use qians_xql::ge;
///
/// let query1 = update("book")
/// .set("id", 1)
/// .filter(and(ge("id", 1), ge("year", 1970)));
///
/// let query2 = update("book")
/// .set("id", 1)
/// .filter(ge("id", 1))
/// .filter(ge("year", 1970));
///
/// assert_eq!(query1, query2);
/// ```
pub fn filter<E>(mut self, expr: E) -> Update<'a>
where
E: Into<Expr<'a>>,
{
self.filter = match self.filter.take() {
Some(inner) => Some(and(inner.0, expr.into()).into()),
None => Some(expr.into().into()),
};
self
}
/// Set/Add field(s) to `RETURNING` clause.
///
/// Successive calls combine adds more field into the clause.
///
/// # Examples
///
/// ```
/// use qians_xql::update;
///
/// let query1 = update("book")
/// .set("id", 1)
/// .returning(["id", "name"]);
///
/// let query2 = update("book")
/// .set("id", 1)
/// .returning(["id"])
/// .returning(["name"]);
///
/// assert_eq!(query1, query2);
/// ```
pub fn returning<T>(mut self, returns: T) -> Update<'a>
where
T: Into<clause::Returning<'a>>,
{
self.returns = match self.returns.take() {
Some(mut inner) => {
inner.0.extend(returns.into().0);
Some(inner)
}
None => Some(returns.into()),
};
self
}
}
#[test]
#[cfg(test)]
fn test() {
let someone = &"someone".to_string();
let query = crate::stmt::update("user")
.set_values([("id", 1), ("age", 30)])
.set("name", someone)
.from(["data"])
.filter(crate::ops::eq(("user", "id"), ("data", "id")))
.returning(["id", "age"]);
assert_eq!(query.to_string(), "UPDATE user SET id = 1, age = 30, name = 'someone' FROM data WHERE user.id = data.id RETURNING id, age");
}