welds 0.4.23

An async ORM for (postgres, mssql, mysql, sqlite)
Documentation
use super::{AsFieldName, ClauseColVal, ClauseColValEqual, ClauseColValIn};
use std::marker::PhantomData;
use welds_connections::Param;

#[derive(Clone)]
pub struct Text<T> {
    col: &'static str,
    field: &'static str,
    _t: PhantomData<T>,
}

impl<T> AsFieldName<T> for Text<T> {
    fn colname(&self) -> &'static str {
        self.col
    }
    fn fieldname(&self) -> &'static str {
        self.field
    }
}
impl<T: Clone> Copy for Text<T> {}

impl<T> Text<T>
where
    T: 'static + Clone + Send + Sync,
{
    pub fn new(col: &'static str, field: &'static str) -> Self {
        Self {
            col,
            field,
            _t: Default::default(),
        }
    }

    pub fn equal(self, v: impl Into<T>) -> Box<ClauseColValEqual<T>>
    where
        T: Param,
    {
        let cv = ClauseColValEqual::<T> {
            null_clause: false,
            not_clause: false,
            col: self.col,
            operator: "=",
            val: Some(v.into()),
        };
        Box::new(cv)
    }

    pub fn not_equal(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
    where
        T: Param,
    {
        let cv = ClauseColVal::<T> {
            null_clause: false,
            not_clause: true,
            col: self.col,
            operator: "!=",
            val: Some(v.into()),
        };
        Box::new(cv)
    }

    pub fn like(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
    where
        T: Param,
    {
        let cv = ClauseColVal::<T> {
            null_clause: false,
            not_clause: false,
            col: self.col,
            operator: "like",
            val: Some(v.into()),
        };
        Box::new(cv)
    }

    pub fn not_like(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
    where
        T: Param,
    {
        let cv = ClauseColVal::<T> {
            null_clause: false,
            not_clause: true,
            col: self.col,
            operator: "not like",
            val: Some(v.into()),
        };
        Box::new(cv)
    }

    pub fn ilike(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
    where
        T: Param,
    {
        let cv = ClauseColVal::<T> {
            null_clause: false,
            not_clause: false,
            col: self.col,
            operator: "ilike",
            val: Some(v.into()),
        };
        Box::new(cv)
    }

    pub fn not_ilike(self, v: impl Into<T>) -> Box<ClauseColVal<T>>
    where
        T: Param,
    {
        let cv = ClauseColVal::<T> {
            null_clause: false,
            not_clause: true,
            col: self.col,
            operator: "not ilike",
            val: Some(v.into()),
        };
        Box::new(cv)
    }

    /// Will write SQL "IN" to check that the value is in a list
    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)
    }

    /// Will write SQL "NOT IN ()" to check that the value is not in a list
    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)
    }
}