Skip to main content

welds/query/clause/
basic.rs

1use super::{AsFieldName, ClauseColVal, ClauseColValEqual, ClauseColValIn};
2use std::marker::PhantomData;
3use welds_connections::Param;
4
5#[derive(Clone)]
6pub struct Basic<T> {
7    col: &'static str,
8    field: &'static str,
9    _t: PhantomData<T>,
10}
11
12impl<T> AsFieldName<T> for Basic<T> {
13    fn colname(&self) -> &'static str {
14        self.col
15    }
16    fn fieldname(&self) -> &'static str {
17        self.field
18    }
19}
20impl<T: Clone> Copy for Basic<T> {}
21
22impl<T> Basic<T>
23where
24    T: 'static + Clone + Send + Sync,
25{
26    pub fn new(col: &'static str, field: &'static str) -> Self {
27        Self {
28            col: col.into(),
29            field: field.into(),
30            _t: Default::default(),
31        }
32    }
33
34    pub fn equal(self, v: impl Into<T>) -> Box<ClauseColValEqual<T>>
35    where
36        T: Param,
37    {
38        let cv = ClauseColValEqual::<T> {
39            null_clause: false,
40            not_clause: false,
41            col: self.col,
42            operator: "=",
43            val: Some(v.into()),
44        };
45        Box::new(cv)
46    }
47
48    pub fn not_equal(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
49    where
50        T: Param,
51    {
52        let cv = ClauseColVal::<T> {
53            null_clause: false,
54            not_clause: true,
55            col: self.col,
56            operator: "!=",
57            val: Some(v.into()),
58        };
59        Box::new(cv)
60    }
61
62    /// Will write SQL "IN" to check that the value is in a list
63    pub fn in_list<P>(self, slice: &[P]) -> Box<ClauseColValIn<T>>
64    where
65        P: Into<T> + Clone,
66        T: Param,
67    {
68        let mut list = Vec::default();
69        for param in slice {
70            list.push(param.clone().into());
71        }
72        let c = ClauseColValIn::<T> {
73            col: self.col,
74            operator: "IN",
75            list,
76        };
77        Box::new(c)
78    }
79
80    /// Will write SQL "NOT IN ()" to check that the value is not in a list
81    pub fn not_in_list<P>(self, slice: &[P]) -> Box<ClauseColValIn<T>>
82    where
83        P: Into<T> + Clone,
84        T: Param,
85    {
86        let mut list = Vec::default();
87        for param in slice {
88            list.push(param.clone().into());
89        }
90        let c = ClauseColValIn::<T> {
91            col: self.col,
92            operator: "NOT IN",
93            list,
94        };
95        Box::new(c)
96    }
97}