1use std::marker::PhantomData;
2
3use time::OffsetDateTime;
4
5use crate::{DbEnum, OrderBy, OrderDirection};
6
7#[derive(Debug, Clone, PartialEq)]
8pub enum DatabaseValue {
10 Integer(i64),
11 Real(f64),
12 Text(String),
13 Binary(Vec<u8>),
14 Boolean(bool),
15 DateTime(OffsetDateTime),
16 Uuid(uuid::Uuid),
17 Null,
18}
19
20pub trait QueryValue<T> {
21 fn into_query_value(self) -> DatabaseValue;
22}
23
24impl QueryValue<uuid::Uuid> for uuid::Uuid {
25 fn into_query_value(self) -> DatabaseValue {
26 DatabaseValue::Uuid(self)
27 }
28}
29
30pub trait PrimaryKey:
32 Copy + Eq + std::hash::Hash + Send + Sync + 'static + QueryValue<Self> + crate::FromDatabaseValue
33{
34 fn generated_value() -> Option<DatabaseValue>;
35}
36
37impl PrimaryKey for i64 {
38 fn generated_value() -> Option<DatabaseValue> {
39 None
40 }
41}
42
43impl PrimaryKey for uuid::Uuid {
44 fn generated_value() -> Option<DatabaseValue> {
45 Some(DatabaseValue::Uuid(Self::now_v7()))
46 }
47}
48
49pub trait ForeignKeyValue<Key: PrimaryKey>: Copy + Send + 'static {
51 const OPTIONAL: bool;
52 fn owner_key(self) -> Option<Key>;
53}
54
55impl<Key: PrimaryKey> ForeignKeyValue<Key> for Key {
56 const OPTIONAL: bool = false;
57 fn owner_key(self) -> Option<Key> {
58 Some(self)
59 }
60}
61
62impl<Key: PrimaryKey> ForeignKeyValue<Key> for Option<Key> {
63 const OPTIONAL: bool = true;
64 fn owner_key(self) -> Option<Key> {
65 self
66 }
67}
68
69impl QueryValue<i64> for i64 {
70 fn into_query_value(self) -> DatabaseValue {
71 DatabaseValue::Integer(self)
72 }
73}
74
75impl QueryValue<f64> for f64 {
76 fn into_query_value(self) -> DatabaseValue {
77 DatabaseValue::Real(self)
78 }
79}
80
81impl QueryValue<String> for String {
82 fn into_query_value(self) -> DatabaseValue {
83 DatabaseValue::Text(self)
84 }
85}
86
87impl QueryValue<String> for &str {
88 fn into_query_value(self) -> DatabaseValue {
89 DatabaseValue::Text(self.to_owned())
90 }
91}
92
93impl QueryValue<Vec<u8>> for Vec<u8> {
94 fn into_query_value(self) -> DatabaseValue {
95 DatabaseValue::Binary(self)
96 }
97}
98
99impl QueryValue<bool> for bool {
100 fn into_query_value(self) -> DatabaseValue {
101 DatabaseValue::Boolean(self)
102 }
103}
104
105impl QueryValue<OffsetDateTime> for OffsetDateTime {
106 fn into_query_value(self) -> DatabaseValue {
107 DatabaseValue::DateTime(self)
108 }
109}
110
111impl<T: DbEnum> QueryValue<T> for T {
112 fn into_query_value(self) -> DatabaseValue {
113 DatabaseValue::Text(self.as_str().to_owned())
114 }
115}
116
117impl<T, V> QueryValue<Option<T>> for Option<V>
118where
119 V: QueryValue<T>,
120{
121 fn into_query_value(self) -> DatabaseValue {
122 self.map_or(DatabaseValue::Null, QueryValue::into_query_value)
123 }
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub struct TableRef {
128 pub name: &'static str,
129}
130
131impl TableRef {
132 pub const fn new(name: &'static str) -> Self {
133 Self { name }
134 }
135}
136
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub struct ColumnRef {
139 pub table: &'static str,
140 pub name: &'static str,
141}
142
143impl ColumnRef {
144 pub const fn new(table: &'static str, name: &'static str) -> Self {
145 Self { table, name }
146 }
147}
148
149#[derive(Debug, Clone, Copy, PartialEq, Eq)]
150pub enum CompareOp {
151 Eq,
152 Ne,
153 Gt,
154 Gte,
155 Lt,
156 Lte,
157}
158
159#[derive(Debug, Clone)]
160pub enum Expr {
162 Column(ColumnRef),
163 Value(DatabaseValue),
164 Compare {
165 left: Box<Expr>,
166 op: CompareOp,
167 right: Box<Expr>,
168 },
169 Like {
170 left: Box<Expr>,
171 pattern: Box<Expr>,
172 },
173 In {
174 left: Box<Expr>,
175 values: Vec<DatabaseValue>,
176 },
177 And {
178 left: Box<Expr>,
179 right: Box<Expr>,
180 },
181 Or {
182 left: Box<Expr>,
183 right: Box<Expr>,
184 },
185 Not(Box<Expr>),
186 IsNull(Box<Expr>),
187 IsNotNull(Box<Expr>),
188}
189
190impl Expr {
191 pub fn and(self, other: Expr) -> Expr {
192 Expr::And {
193 left: Box::new(self),
194 right: Box::new(other),
195 }
196 }
197 pub fn or(self, other: Expr) -> Expr {
198 Expr::Or {
199 left: Box::new(self),
200 right: Box::new(other),
201 }
202 }
203 #[allow(clippy::should_implement_trait)]
204 pub fn not(self) -> Expr {
205 Expr::Not(Box::new(self))
206 }
207
208 pub fn like(self, pattern: impl Into<String>) -> Expr {
209 Expr::Like {
210 left: Box::new(self),
211 pattern: Box::new(Expr::Value(DatabaseValue::Text(pattern.into()))),
212 }
213 }
214}
215
216#[derive(Debug, Clone, Copy)]
217pub struct ModelField<M, T> {
219 table: &'static str,
220 column: &'static str,
221 _marker: PhantomData<fn() -> (M, T)>,
222}
223
224impl<M, T> ModelField<M, T> {
225 pub const fn new(table: &'static str, column: &'static str) -> Self {
226 Self {
227 table,
228 column,
229 _marker: PhantomData,
230 }
231 }
232
233 pub const fn column(&self) -> ColumnRef {
234 ColumnRef::new(self.table, self.column)
235 }
236
237 fn comparison<V>(self, op: CompareOp, value: V) -> Expr
238 where
239 V: QueryValue<T>,
240 {
241 Expr::Compare {
242 left: Box::new(Expr::Column(self.column())),
243 op,
244 right: Box::new(Expr::Value(value.into_query_value())),
245 }
246 }
247
248 pub fn eq<V>(self, value: V) -> Expr
249 where
250 V: QueryValue<T>,
251 {
252 self.comparison(CompareOp::Eq, value)
253 }
254 pub fn in_values<I, V>(self, values: I) -> Expr
255 where
256 I: IntoIterator<Item = V>,
257 V: QueryValue<T>,
258 {
259 Expr::In {
260 left: Box::new(Expr::Column(self.column())),
261 values: values
262 .into_iter()
263 .map(QueryValue::into_query_value)
264 .collect(),
265 }
266 }
267 pub fn ne<V>(self, value: V) -> Expr
268 where
269 V: QueryValue<T>,
270 {
271 self.comparison(CompareOp::Ne, value)
272 }
273 pub fn gt<V>(self, value: V) -> Expr
274 where
275 V: QueryValue<T>,
276 {
277 self.comparison(CompareOp::Gt, value)
278 }
279 pub fn gte<V>(self, value: V) -> Expr
280 where
281 V: QueryValue<T>,
282 {
283 self.comparison(CompareOp::Gte, value)
284 }
285 pub fn lt<V>(self, value: V) -> Expr
286 where
287 V: QueryValue<T>,
288 {
289 self.comparison(CompareOp::Lt, value)
290 }
291 pub fn lte<V>(self, value: V) -> Expr
292 where
293 V: QueryValue<T>,
294 {
295 self.comparison(CompareOp::Lte, value)
296 }
297 pub fn is_null(self) -> Expr {
298 Expr::IsNull(Box::new(Expr::Column(self.column())))
299 }
300 pub fn is_not_null(self) -> Expr {
301 Expr::IsNotNull(Box::new(Expr::Column(self.column())))
302 }
303
304 pub fn contains(self, value: impl Into<String>) -> Expr {
305 Expr::Column(self.column()).like(format!("%{}%", value.into()))
306 }
307 pub fn asc(self) -> OrderBy {
308 OrderBy {
309 column: self.column(),
310 direction: OrderDirection::Asc,
311 }
312 }
313 pub fn desc(self) -> OrderBy {
314 OrderBy {
315 column: self.column(),
316 direction: OrderDirection::Desc,
317 }
318 }
319}