#[cfg(feature = "postgres")]
use super::ClauseColValList;
use super::{AsFieldName, AsOptField, ClauseColVal, ClauseColValEqual, ClauseColValIn};
use crate::query::optional::HasSomeNone;
use crate::query::optional::Optional;
use std::marker::PhantomData;
use welds_connections::Param;
#[derive(Copy, Clone)]
pub struct NumericOpt<T> {
col: &'static str,
field: &'static str,
_t: PhantomData<T>,
}
impl<T> AsFieldName<T> for NumericOpt<T> {
fn colname(&self) -> &'static str {
self.col
}
fn fieldname(&self) -> &'static str {
self.field
}
}
impl<T> AsOptField for NumericOpt<T> {}
impl<T> NumericOpt<T>
where
T: 'static + Clone + Send + Sync,
{
pub fn new(col: &'static str, field: &'static str) -> Self {
Self {
col: col.into(),
field: field.into(),
_t: Default::default(),
}
}
pub fn equal(self, v: impl Into<Optional<T>>) -> Box<ClauseColValEqual<T>>
where
T: Param,
{
let opt = v.into();
let is_none = opt.is_none();
let val: Option<T> = opt.into();
let cv = ClauseColValEqual::<T> {
null_clause: is_none,
not_clause: false,
col: self.col,
operator: "=",
val,
};
Box::new(cv)
}
pub fn not_equal(self, v: impl Into<Optional<T>>) -> Box<ClauseColVal<T>>
where
T: Param,
{
let opt = v.into();
let is_none = opt.is_none();
let val: Option<T> = opt.into();
let cv = ClauseColVal::<T> {
null_clause: is_none,
not_clause: true,
col: self.col,
operator: "!=",
val,
};
Box::new(cv)
}
pub fn gt(self, v: impl Into<Optional<T>>) -> Box<ClauseColVal<T>>
where
T: Param,
{
let opt = v.into();
let is_none = opt.is_none();
let val: Option<T> = opt.into();
let cv = ClauseColVal::<T> {
null_clause: is_none,
not_clause: false,
col: self.col,
operator: ">",
val,
};
Box::new(cv)
}
pub fn lt(self, v: impl Into<Optional<T>>) -> Box<ClauseColVal<T>>
where
T: Param,
{
let opt = v.into();
let val: Option<T> = opt.into();
let cv = ClauseColVal::<T> {
null_clause: val.is_none(),
not_clause: false,
col: self.col,
operator: "<",
val,
};
Box::new(cv)
}
pub fn gte(self, v: impl Into<Optional<T>>) -> Box<ClauseColVal<T>>
where
T: Param,
{
let opt = v.into();
let is_none = opt.is_none();
let val: Option<T> = opt.into();
let cv = ClauseColVal::<T> {
null_clause: is_none,
not_clause: false,
col: self.col,
operator: ">=",
val,
};
Box::new(cv)
}
pub fn lte(self, v: impl Into<Optional<T>>) -> Box<ClauseColVal<T>>
where
T: Param,
{
let opt = v.into();
let is_none = opt.is_none();
let val: Option<T> = opt.into();
let cv = ClauseColVal::<T> {
null_clause: is_none,
not_clause: false,
col: self.col,
operator: "<=",
val,
};
Box::new(cv)
}
#[cfg(feature = "postgres")]
pub fn any<P>(self, slice: &[P]) -> Box<ClauseColValList<T>>
where
P: Into<T> + Clone,
Vec<T>: Param,
{
let mut list: Vec<T> = Vec::default();
for p in slice {
list.push(p.clone().into());
}
let cv = ClauseColValList::<T> {
col: self.col,
operator: "= any",
list,
};
Box::new(cv)
}
#[cfg(feature = "postgres")]
pub fn not_all<P>(self, slice: &[P]) -> Box<ClauseColValList<T>>
where
P: Into<T> + Clone,
Vec<T>: Param,
{
let mut list: Vec<T> = Vec::default();
for p in slice {
list.push(p.clone().into());
}
let cv = ClauseColValList::<T> {
col: self.col,
operator: "!= all",
list,
};
Box::new(cv)
}
pub fn in_list<P>(self, slice: &[P]) -> Box<ClauseColValIn<T>>
where
P: Into<T> + Clone,
T: Param,
{
let mut list = Vec::default();
for param in slice {
list.push(param.clone().into());
}
let c = ClauseColValIn::<T> {
col: self.col,
operator: "IN",
list,
};
Box::new(c)
}
pub fn not_in_list<P>(self, slice: &[P]) -> Box<ClauseColValIn<T>>
where
P: Into<T> + Clone,
T: Param,
{
let mut list = Vec::default();
for param in slice {
list.push(param.clone().into());
}
let c = ClauseColValIn::<T> {
col: self.col,
operator: "NOT IN",
list,
};
Box::new(c)
}
}