use super::expr::Expr;
use crate::core::condition::SqlValue;
#[derive(Debug, Clone, Copy)]
pub struct Column<T, V> {
pub(crate) table: &'static str,
pub(crate) name: &'static str,
_table: std::marker::PhantomData<T>,
_value: std::marker::PhantomData<V>,
}
impl<T, V> Column<T, V> {
pub const fn new(table: &'static str, name: &'static str) -> Self {
Self {
table,
name,
_table: std::marker::PhantomData,
_value: std::marker::PhantomData,
}
}
pub fn qualified(&self) -> String {
format!("\"{}\".\"{}\"", self.table, self.name)
}
pub fn name(&self) -> &'static str {
self.name
}
}
impl<T, V: Into<SqlValue>> Column<T, V> {
pub fn eq(self, val: impl Into<SqlValue>) -> Expr {
Expr::Eq(self.qualified(), val.into())
}
pub fn ne(self, val: impl Into<SqlValue>) -> Expr {
Expr::Ne(self.qualified(), val.into())
}
pub fn gt(self, val: impl Into<SqlValue>) -> Expr {
Expr::Gt(self.qualified(), val.into())
}
pub fn gte(self, val: impl Into<SqlValue>) -> Expr {
Expr::Gte(self.qualified(), val.into())
}
pub fn lt(self, val: impl Into<SqlValue>) -> Expr {
Expr::Lt(self.qualified(), val.into())
}
pub fn lte(self, val: impl Into<SqlValue>) -> Expr {
Expr::Lte(self.qualified(), val.into())
}
pub fn like(self, pattern: impl Into<SqlValue>) -> Expr {
Expr::Like(self.qualified(), pattern.into())
}
pub fn not_like(self, pattern: impl Into<SqlValue>) -> Expr {
Expr::NotLike(self.qualified(), pattern.into())
}
pub fn in_(self, vals: impl IntoIterator<Item = impl Into<SqlValue>>) -> Expr {
Expr::In(self.qualified(), vals.into_iter().map(Into::into).collect())
}
pub fn not_in(self, vals: impl IntoIterator<Item = impl Into<SqlValue>>) -> Expr {
Expr::NotIn(self.qualified(), vals.into_iter().map(Into::into).collect())
}
}
impl<T, V> Column<T, V> {
pub fn is_null(self) -> Expr {
Expr::IsNull(self.qualified())
}
pub fn is_not_null(self) -> Expr {
Expr::IsNotNull(self.qualified())
}
pub fn asc(self) -> OrderExpr {
OrderExpr {
col: self.qualified(),
dir: OrderDir::Asc,
}
}
pub fn desc(self) -> OrderExpr {
OrderExpr {
col: self.qualified(),
dir: OrderDir::Desc,
}
}
}
#[derive(Debug, Clone)]
pub struct OrderExpr {
pub(crate) col: String,
pub(crate) dir: OrderDir,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderDir {
Asc,
Desc,
}
impl OrderExpr {
pub fn to_sql(&self) -> String {
let dir = match self.dir {
OrderDir::Asc => "ASC",
OrderDir::Desc => "DESC",
};
format!("{} {}", self.col, dir)
}
}