use std::marker::PhantomData;
use crate::query::Where;
use crate::schema::ColumnDef;
use crate::value::{ColumnValue, DecodeError, Row, Value};
pub trait Fields: Clone + Send + Sync + 'static {
fn columns() -> &'static [ColumnDef];
fn decode(row: &Row) -> Result<Self, DecodeError>;
}
impl Fields for () {
fn columns() -> &'static [ColumnDef] {
&[]
}
fn decode(_: &Row) -> Result<(), DecodeError> {
Ok(())
}
}
pub trait Model: Sized + Send + Sync + 'static {
const TABLE: &'static str;
fn columns() -> &'static [ColumnDef];
fn decode(row: &Row) -> Result<Self, DecodeError>;
fn encode(&self) -> Vec<(&'static str, Value)>;
}
pub trait OnTable {
type Table: Model;
}
pub struct Col<O, T> {
name: &'static str,
_marker: PhantomData<fn() -> (O, T)>,
}
impl<O, T> Clone for Col<O, T> {
fn clone(&self) -> Self {
*self
}
}
impl<O, T> Copy for Col<O, T> {}
impl<O, T> std::fmt::Debug for Col<O, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Col({})", self.name)
}
}
impl<O: OnTable, T: ColumnValue> Col<O, T> {
pub const fn new(name: &'static str) -> Self {
Self {
name,
_marker: PhantomData,
}
}
pub const fn name(&self) -> &'static str {
self.name
}
pub fn value(self, value: impl Into<T>) -> (&'static str, Value) {
(self.name, value.into().into_value())
}
pub fn eq(self, value: impl Into<T>) -> Where<O::Table> {
Where::cmp(self.name, crate::adapter::Op::Eq, value.into().into_value())
}
pub fn ne(self, value: impl Into<T>) -> Where<O::Table> {
Where::cmp(self.name, crate::adapter::Op::Ne, value.into().into_value())
}
pub fn lt(self, value: impl Into<T>) -> Where<O::Table> {
Where::cmp(self.name, crate::adapter::Op::Lt, value.into().into_value())
}
pub fn lte(self, value: impl Into<T>) -> Where<O::Table> {
Where::cmp(
self.name,
crate::adapter::Op::Lte,
value.into().into_value(),
)
}
pub fn gt(self, value: impl Into<T>) -> Where<O::Table> {
Where::cmp(self.name, crate::adapter::Op::Gt, value.into().into_value())
}
pub fn gte(self, value: impl Into<T>) -> Where<O::Table> {
Where::cmp(
self.name,
crate::adapter::Op::Gte,
value.into().into_value(),
)
}
pub fn in_(self, values: impl IntoIterator<Item = T>) -> Where<O::Table> {
Where::in_list(
self.name,
values.into_iter().map(ColumnValue::into_value).collect(),
)
}
pub fn is_null(self) -> Where<O::Table> {
Where::null(self.name, true)
}
pub fn is_not_null(self) -> Where<O::Table> {
Where::null(self.name, false)
}
pub fn asc(self) -> (&'static str, crate::adapter::Order) {
(self.name, crate::adapter::Order::Asc)
}
pub fn desc(self) -> (&'static str, crate::adapter::Order) {
(self.name, crate::adapter::Order::Desc)
}
}
impl<O: OnTable> Col<O, String> {
pub fn starts_with(self, prefix: impl Into<String>) -> Where<O::Table> {
Where::starts_with(self.name, prefix.into())
}
pub fn contains(self, substring: impl Into<String>) -> Where<O::Table> {
Where::contains(self.name, substring.into())
}
pub fn ends_with(self, suffix: impl Into<String>) -> Where<O::Table> {
Where::ends_with(self.name, suffix.into())
}
}