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
use super::{Query, SqlBuilder, Param};
#[derive(Debug, Clone)]
pub struct UpdateParams<'a> {
params: Vec<Param<'a>>
}
impl<'a> UpdateParams<'a> {
pub fn new(params: Vec<Param<'a>>) -> Self {
Self {params}
}
pub fn into_query(self) -> Query<'a> {
let mut sql = SqlBuilder::new();
let last = self.params.len() - 1;
for (i, param) in self.params.iter().enumerate() {
sql.space_after(format!("\"{}\" =", param.name));
sql.param();
if i != last {
sql.space_after(",");
}
}
Query::new(sql, self.params)
}
}
#[macro_export]
macro_rules! updt {
($($tt:tt)*) => ({
let mut params = vec![];
$crate::updt_item!(params, $($tt)*);
$crate::query::UpdateParams::new(params)
})
}
#[macro_export]
macro_rules! updt_item {
($p:ident, $id:ident, $($tt:tt)+) => (
$crate::updt_item!($p, $id);
$crate::updt_item!($p, $($tt)+);
);
($p:ident, &$id:ident, $($tt:tt)+) => (
$crate::updt_item!($p, &$id);
$crate::updt_item!($p, $($tt)+);
);
($p:ident, $name:tt: $value:expr, $($tt:tt)+) => (
$crate::updt_item!($p, $name: $value);
$crate::updt_item!($p, $($tt)+)
);
($p:ident, $id:ident) => (
$p.push($crate::query::Param::new(stringify!($id), $id));
);
($p:ident, &$id:ident) => (
$p.push($crate::query::Param::new(stringify!($id), &$id));
);
($p:ident, $name:tt: $value:expr) => (
$p.push($crate::query::Param::new($name, $value));
);
}