Skip to main content

che_orm/
types.rs

1use std::marker::PhantomData;
2
3use time::OffsetDateTime;
4
5use crate::{DbEnum, OrderBy, OrderDirection};
6
7#[derive(Debug, Clone, PartialEq)]
8/// Values bound to SQL placeholders.
9pub enum DatabaseValue {
10    Integer(i64),
11    Text(String),
12    Boolean(bool),
13    DateTime(OffsetDateTime),
14    Null,
15}
16
17pub trait QueryValue<T> {
18    fn into_query_value(self) -> DatabaseValue;
19}
20
21impl QueryValue<i64> for i64 {
22    fn into_query_value(self) -> DatabaseValue {
23        DatabaseValue::Integer(self)
24    }
25}
26
27impl QueryValue<String> for String {
28    fn into_query_value(self) -> DatabaseValue {
29        DatabaseValue::Text(self)
30    }
31}
32
33impl QueryValue<String> for &str {
34    fn into_query_value(self) -> DatabaseValue {
35        DatabaseValue::Text(self.to_owned())
36    }
37}
38
39impl QueryValue<bool> for bool {
40    fn into_query_value(self) -> DatabaseValue {
41        DatabaseValue::Boolean(self)
42    }
43}
44
45impl QueryValue<OffsetDateTime> for OffsetDateTime {
46    fn into_query_value(self) -> DatabaseValue {
47        DatabaseValue::DateTime(self)
48    }
49}
50
51impl<T: DbEnum> QueryValue<T> for T {
52    fn into_query_value(self) -> DatabaseValue {
53        DatabaseValue::Text(self.as_str().to_owned())
54    }
55}
56
57impl<T, V> QueryValue<Option<T>> for Option<V>
58where
59    V: QueryValue<T>,
60{
61    fn into_query_value(self) -> DatabaseValue {
62        self.map_or(DatabaseValue::Null, QueryValue::into_query_value)
63    }
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub struct TableRef {
68    pub name: &'static str,
69}
70
71impl TableRef {
72    pub const fn new(name: &'static str) -> Self {
73        Self { name }
74    }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub struct ColumnRef {
79    pub table: &'static str,
80    pub name: &'static str,
81}
82
83impl ColumnRef {
84    pub const fn new(table: &'static str, name: &'static str) -> Self {
85        Self { table, name }
86    }
87}
88
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub enum CompareOp {
91    Eq,
92    Ne,
93    Gt,
94    Gte,
95    Lt,
96    Lte,
97}
98
99#[derive(Debug, Clone)]
100/// Boolean and comparison expression tree used by query builders.
101pub enum Expr {
102    Column(ColumnRef),
103    Value(DatabaseValue),
104    Compare {
105        left: Box<Expr>,
106        op: CompareOp,
107        right: Box<Expr>,
108    },
109    Like {
110        left: Box<Expr>,
111        pattern: Box<Expr>,
112    },
113    In {
114        left: Box<Expr>,
115        values: Vec<DatabaseValue>,
116    },
117    And {
118        left: Box<Expr>,
119        right: Box<Expr>,
120    },
121    Or {
122        left: Box<Expr>,
123        right: Box<Expr>,
124    },
125    Not(Box<Expr>),
126    IsNull(Box<Expr>),
127    IsNotNull(Box<Expr>),
128}
129
130impl Expr {
131    pub fn and(self, other: Expr) -> Expr {
132        Expr::And {
133            left: Box::new(self),
134            right: Box::new(other),
135        }
136    }
137    pub fn or(self, other: Expr) -> Expr {
138        Expr::Or {
139            left: Box::new(self),
140            right: Box::new(other),
141        }
142    }
143    #[allow(clippy::should_implement_trait)]
144    pub fn not(self) -> Expr {
145        Expr::Not(Box::new(self))
146    }
147
148    pub fn like(self, pattern: impl Into<String>) -> Expr {
149        Expr::Like {
150            left: Box::new(self),
151            pattern: Box::new(Expr::Value(DatabaseValue::Text(pattern.into()))),
152        }
153    }
154}
155
156#[derive(Debug, Clone, Copy)]
157/// A typed reference to one model column.
158pub struct ModelField<M, T> {
159    table: &'static str,
160    column: &'static str,
161    _marker: PhantomData<fn() -> (M, T)>,
162}
163
164impl<M, T> ModelField<M, T> {
165    pub const fn new(table: &'static str, column: &'static str) -> Self {
166        Self {
167            table,
168            column,
169            _marker: PhantomData,
170        }
171    }
172
173    pub const fn column(&self) -> ColumnRef {
174        ColumnRef::new(self.table, self.column)
175    }
176
177    fn comparison<V>(self, op: CompareOp, value: V) -> Expr
178    where
179        V: QueryValue<T>,
180    {
181        Expr::Compare {
182            left: Box::new(Expr::Column(self.column())),
183            op,
184            right: Box::new(Expr::Value(value.into_query_value())),
185        }
186    }
187
188    pub fn eq<V>(self, value: V) -> Expr
189    where
190        V: QueryValue<T>,
191    {
192        self.comparison(CompareOp::Eq, value)
193    }
194    pub fn in_values<I, V>(self, values: I) -> Expr
195    where
196        I: IntoIterator<Item = V>,
197        V: QueryValue<T>,
198    {
199        Expr::In {
200            left: Box::new(Expr::Column(self.column())),
201            values: values
202                .into_iter()
203                .map(QueryValue::into_query_value)
204                .collect(),
205        }
206    }
207    pub fn ne<V>(self, value: V) -> Expr
208    where
209        V: QueryValue<T>,
210    {
211        self.comparison(CompareOp::Ne, value)
212    }
213    pub fn gt<V>(self, value: V) -> Expr
214    where
215        V: QueryValue<T>,
216    {
217        self.comparison(CompareOp::Gt, value)
218    }
219    pub fn gte<V>(self, value: V) -> Expr
220    where
221        V: QueryValue<T>,
222    {
223        self.comparison(CompareOp::Gte, value)
224    }
225    pub fn lt<V>(self, value: V) -> Expr
226    where
227        V: QueryValue<T>,
228    {
229        self.comparison(CompareOp::Lt, value)
230    }
231    pub fn lte<V>(self, value: V) -> Expr
232    where
233        V: QueryValue<T>,
234    {
235        self.comparison(CompareOp::Lte, value)
236    }
237    pub fn is_null(self) -> Expr {
238        Expr::IsNull(Box::new(Expr::Column(self.column())))
239    }
240    pub fn is_not_null(self) -> Expr {
241        Expr::IsNotNull(Box::new(Expr::Column(self.column())))
242    }
243
244    pub fn contains(self, value: impl Into<String>) -> Expr {
245        Expr::Column(self.column()).like(format!("%{}%", value.into()))
246    }
247    pub fn asc(self) -> OrderBy {
248        OrderBy {
249            column: self.column(),
250            direction: OrderDirection::Asc,
251        }
252    }
253    pub fn desc(self) -> OrderBy {
254        OrderBy {
255            column: self.column(),
256            direction: OrderDirection::Desc,
257        }
258    }
259}