fire_postgres/query/
update.rs1use super::{ColumnType, Param, Query, SqlBuilder};
2
3#[derive(Debug, Clone)]
4pub struct UpdateParams<'a> {
5 params: Vec<Param<'a>>,
6}
7
8impl<'a> UpdateParams<'a> {
9 pub fn new(params: Vec<Param<'a>>) -> Self {
10 Self { params }
11 }
12
13 pub fn insert<T>(&mut self, name: &'static str, data: &'a T)
14 where
15 T: ColumnType,
16 {
17 self.params.push(Param::new(name, data));
18 }
19
20 pub fn into_query(self) -> Query<'a> {
21 let mut sql = SqlBuilder::new();
22
23 let last = self.params.len() - 1;
24 for (i, param) in self.params.iter().enumerate() {
25 sql.space_after(format!("\"{}\" =", param.name));
26 sql.param();
27
28 if i != last {
29 sql.space_after(",");
30 }
31 }
32
33 Query::new(sql, self.params)
34 }
35}
36
37#[macro_export]
50macro_rules! updt {
51 ($($tt:tt)*) => ({
52 let mut params = vec![];
53 $crate::updt_item!(params, $($tt)*);
54 $crate::query::UpdateParams::new(params)
55 })
56}
57
58#[macro_export]
59macro_rules! updt_item {
60 ($p:ident, $id:ident, $($tt:tt)+) => (
61 $crate::updt_item!($p, $id);
62 $crate::updt_item!($p, $($tt)+);
63 );
64 ($p:ident, &$id:ident, $($tt:tt)+) => (
65 $crate::updt_item!($p, &$id);
66 $crate::updt_item!($p, $($tt)+);
67 );
68 ($p:ident, $name:tt: $value:expr, $($tt:tt)+) => (
69 $crate::updt_item!($p, $name: $value);
70 $crate::updt_item!($p, $($tt)+)
71 );
72 ($p:ident, $id:ident) => (
73 $p.push($crate::query::Param::new(stringify!($id), $id));
74 );
75 ($p:ident, &$id:ident) => (
76 $p.push($crate::query::Param::new(stringify!($id), &$id));
77 );
78 ($p:ident, $name:tt: $value:expr) => (
79 $p.push($crate::query::Param::new($name, $value));
80 );
81}
82
83