welds/query/clause/
numeric.rs

1#[cfg(feature = "postgres")]
2use super::ClauseColValList;
3use super::{AsFieldName, ClauseColVal, ClauseColValEqual, ClauseColValIn};
4use std::marker::PhantomData;
5use welds_connections::Param;
6
7/// Clauses for numeric types such as int, float, etc
8pub struct Numeric<T> {
9    col: String,
10    field: String,
11    _t: PhantomData<T>,
12}
13
14impl<T> AsFieldName<T> for Numeric<T> {
15    fn colname(&self) -> &str {
16        self.col.as_str()
17    }
18    fn fieldname(&self) -> &str {
19        self.field.as_str()
20    }
21}
22
23impl<T> Numeric<T>
24where
25    T: 'static + Clone + Send + Sync,
26{
27    pub fn new(col: impl Into<String>, field: impl Into<String>) -> Self {
28        Self {
29            col: col.into(),
30            field: field.into(),
31            _t: Default::default(),
32        }
33    }
34
35    /// Will write SQL checking the value is equal to this (==)
36    pub fn equal(self, v: impl Into<T>) -> Box<ClauseColValEqual<T>>
37    where
38        T: Param,
39    {
40        let cv = ClauseColValEqual::<T> {
41            null_clause: false,
42            not_clause: false,
43            col: self.col,
44            operator: "=",
45            val: Some(v.into()),
46        };
47        Box::new(cv)
48    }
49
50    /// Will write SQL checking the value is equal to this (!=)
51    pub fn not_equal(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
52    where
53        T: Param,
54    {
55        let cv = ClauseColVal::<T> {
56            null_clause: false,
57            not_clause: true,
58            col: self.col,
59            operator: "!=",
60            val: Some(v.into()),
61        };
62        Box::new(cv)
63    }
64
65    /// Will write SQL checking the value is greater than (>)
66    pub fn gt(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
67    where
68        T: Param,
69    {
70        let cv = ClauseColVal::<T> {
71            null_clause: false,
72            not_clause: false,
73            col: self.col,
74            operator: ">",
75            val: Some(v.into()),
76        };
77        Box::new(cv)
78    }
79
80    /// Will write SQL checking the value is less than (<)
81    pub fn lt(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
82    where
83        T: Param,
84    {
85        let cv = ClauseColVal::<T> {
86            null_clause: false,
87            not_clause: false,
88            col: self.col,
89            operator: "<",
90            val: Some(v.into()),
91        };
92        Box::new(cv)
93    }
94
95    /// Will write SQL checking the value is greater than or equal to (>=)
96    pub fn gte(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
97    where
98        T: Param,
99    {
100        let cv = ClauseColVal::<T> {
101            null_clause: false,
102            not_clause: false,
103            col: self.col,
104            operator: ">=",
105            val: Some(v.into()),
106        };
107        Box::new(cv)
108    }
109
110    /// Will write SQL checking the value is less than or equal to (<=)
111    pub fn lte(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
112    where
113        T: Param,
114    {
115        let cv = ClauseColVal::<T> {
116            null_clause: false,
117            not_clause: false,
118            col: self.col,
119            operator: "<=",
120            val: Some(v.into()),
121        };
122        Box::new(cv)
123    }
124
125    /// Will write SQL checking for any matching value in from list
126    /// NOTE: the negation of this operator is not_all(&[])
127    #[cfg(feature = "postgres")]
128    pub fn any<P>(self, slice: &[P]) -> Box<ClauseColValList<T>>
129    where
130        P: Into<T> + Clone,
131        Vec<T>: Param,
132    {
133        let mut list: Vec<T> = Vec::default();
134        for p in slice {
135            list.push(p.clone().into());
136        }
137        let cv = ClauseColValList::<T> {
138            col: self.col,
139            operator: "= any",
140            list,
141        };
142        Box::new(cv)
143    }
144
145    /// Will make sure the columns values does NOT match ALL values in the list
146    #[cfg(feature = "postgres")]
147    pub fn not_all<P>(self, slice: &[P]) -> Box<ClauseColValList<T>>
148    where
149        P: Into<T> + Clone,
150        Vec<T>: Param,
151    {
152        let mut list: Vec<T> = Vec::default();
153        for p in slice {
154            list.push(p.clone().into());
155        }
156        let cv = ClauseColValList::<T> {
157            col: self.col,
158            operator: "!= all",
159            list,
160        };
161        Box::new(cv)
162    }
163
164    /// Will write SQL "IN" to check that the value is in a list
165    pub fn in_list<P>(self, slice: &[P]) -> Box<ClauseColValIn<T>>
166    where
167        P: Into<T> + Clone,
168        T: Param,
169    {
170        let mut list = Vec::default();
171        for param in slice {
172            list.push(param.clone().into());
173        }
174        let c = ClauseColValIn::<T> {
175            col: self.col,
176            operator: "IN",
177            list,
178        };
179        Box::new(c)
180    }
181}