1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::{
    cell::OnceCell,
    ops::Deref,
    rc::Rc,
    sync::atomic::{AtomicU64, Ordering},
};

use elsa::FrozenVec;
use sea_query::{Expr, Iden, IntoColumnRef, Nullable, SimpleExpr};

use crate::{
    ast::{Joins, MyTable},
    Builder, HasId,
};

/// Trait for all values that can be used in queries.
/// This includes dummies from queries and rust values.
pub trait Value<'t>: Sized {
    type Typ: MyIdenT;
    #[doc(hidden)]
    fn build_expr(&self) -> SimpleExpr;

    fn add<T: Value<'t>>(self, rhs: T) -> MyAdd<Self, T> {
        MyAdd(self, rhs)
    }

    fn lt(self, rhs: i32) -> MyLt<Self> {
        MyLt(self, rhs)
    }

    fn eq<T: Value<'t, Typ = Self::Typ>>(self, rhs: T) -> MyEq<Self, T> {
        MyEq(self, rhs)
    }

    fn not(self) -> MyNot<Self>
    where
        Self: Value<'t, Typ = bool>,
    {
        MyNot(self)
    }

    fn and<T: Value<'t, Typ = bool>>(self, rhs: T) -> MyAnd<Self, T>
    where
        Self: Value<'t, Typ = bool>,
    {
        MyAnd(self, rhs)
    }

    fn unwrap_or<T: Value<'t>>(self, rhs: T) -> UnwrapOr<Self, T>
    where
        Self: Value<'t, Typ = Option<T::Typ>>,
    {
        UnwrapOr(self, rhs)
    }

    fn is_not_null(self) -> IsNotNull<Self> {
        IsNotNull(self)
    }
}

impl<'t, T: Value<'t>> Value<'t> for &'_ T {
    type Typ = T::Typ;

    fn build_expr(&self) -> SimpleExpr {
        T::build_expr(self)
    }
}

impl<'t, T: Value<'t> + Nullable> Value<'t> for Option<T> {
    type Typ = Option<T::Typ>;

    fn build_expr(&self) -> SimpleExpr {
        self.as_ref().map(T::build_expr).unwrap_or(T::null().into())
    }
}

impl<'t> Value<'t> for &str {
    type Typ = String;

    fn build_expr(&self) -> SimpleExpr {
        SimpleExpr::from(*self)
    }
}

impl<'t> Value<'t> for bool {
    type Typ = bool;

    fn build_expr(&self) -> SimpleExpr {
        SimpleExpr::from(*self)
    }
}

impl<'t> Value<'t> for i64 {
    type Typ = i64;

    fn build_expr(&self) -> SimpleExpr {
        SimpleExpr::from(*self)
    }
}

impl<'t> Value<'t> for f64 {
    type Typ = f64;

    fn build_expr(&self) -> SimpleExpr {
        SimpleExpr::from(*self)
    }
}

impl<'t, T: MyIdenT> Value<'t> for Db<'t, T> {
    type Typ = T;
    fn build_expr(&self) -> SimpleExpr {
        Expr::col(self.field).into()
    }
}

#[derive(Clone, Copy)]
pub struct MyAdd<A, B>(A, B);

impl<'t, A: Value<'t>, B: Value<'t>> Value<'t> for MyAdd<A, B> {
    type Typ = A::Typ;
    fn build_expr(&self) -> SimpleExpr {
        self.0.build_expr().add(self.1.build_expr())
    }
}

#[derive(Clone, Copy)]
pub struct MyNot<T>(T);

impl<'t, T: Value<'t>> Value<'t> for MyNot<T> {
    type Typ = T::Typ;
    fn build_expr(&self) -> SimpleExpr {
        self.0.build_expr().not()
    }
}

#[derive(Clone, Copy)]
pub struct MyAnd<A, B>(A, B);

impl<'t, A: Value<'t>, B: Value<'t>> Value<'t> for MyAnd<A, B> {
    type Typ = A::Typ;
    fn build_expr(&self) -> SimpleExpr {
        self.0.build_expr().and(self.1.build_expr())
    }
}
#[derive(Clone, Copy)]
pub struct MyLt<A>(A, i32);

impl<'t, A: Value<'t>> Value<'t> for MyLt<A> {
    type Typ = bool;
    fn build_expr(&self) -> SimpleExpr {
        Expr::expr(self.0.build_expr()).lt(self.1)
    }
}

#[derive(Clone, Copy)]
pub struct MyEq<A, B>(A, B);

impl<'t, A: Value<'t>, B: Value<'t>> Value<'t> for MyEq<A, B> {
    type Typ = bool;
    fn build_expr(&self) -> SimpleExpr {
        self.0.build_expr().eq(self.1.build_expr())
    }
}

#[derive(Clone, Copy)]
pub struct UnwrapOr<A, B>(pub(crate) A, pub(crate) B);

impl<'t, A: Value<'t>, B: Value<'t>> Value<'t> for UnwrapOr<A, B> {
    type Typ = B::Typ;
    fn build_expr(&self) -> SimpleExpr {
        Expr::expr(self.0.build_expr()).if_null(self.1.build_expr())
    }
}

#[derive(Clone, Copy)]
pub struct IsNotNull<A>(pub(crate) A);

impl<'t, A: Value<'t>> Value<'t> for IsNotNull<A> {
    type Typ = bool;
    fn build_expr(&self) -> SimpleExpr {
        Expr::expr(self.0.build_expr()).is_not_null()
    }
}

/// Use this a value in a query to get the current datetime as a number.
pub struct UnixEpoch;

impl<'t> Value<'t> for UnixEpoch {
    type Typ = i64;

    fn build_expr(&self) -> SimpleExpr {
        Expr::col(RawAlias("unixepoch('now')".to_owned())).into()
    }
}

#[derive(Debug, Clone, Copy)]
pub(super) struct FieldAlias {
    pub table: MyAlias,
    pub col: Field,
}

impl IntoColumnRef for FieldAlias {
    fn into_column_ref(self) -> sea_query::ColumnRef {
        (self.table, self.col).into_column_ref()
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) enum Field {
    U64(MyAlias),
    Str(&'static str),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct MyAlias {
    name: u64,
}

impl sea_query::Iden for Field {
    fn unquoted(&self, s: &mut dyn std::fmt::Write) {
        match self {
            Field::U64(alias) => alias.unquoted(s),
            Field::Str(name) => write!(s, "{}", name).unwrap(),
        }
    }
}

impl MyAlias {
    pub fn new() -> Self {
        static IDEN_NUM: AtomicU64 = AtomicU64::new(0);
        let next = IDEN_NUM.fetch_add(1, Ordering::Relaxed);
        Self { name: next }
    }
}

impl Field {
    pub fn new() -> Self {
        Field::U64(MyAlias::new())
    }
}

impl sea_query::Iden for MyAlias {
    fn unquoted(&self, s: &mut dyn std::fmt::Write) {
        write!(s, "_{}", self.name).unwrap()
    }
}

pub(super) trait MyTableT<'t>: Clone {
    fn unwrap(joined: &'t FrozenVec<Box<(Field, MyTable)>>) -> Self;
}

impl<'t, T: HasId> MyTableT<'t> for FkInfo<'t, T> {
    fn unwrap(joined: &'t FrozenVec<Box<(Field, MyTable)>>) -> Self {
        FkInfo {
            joined,
            inner: OnceCell::new(),
        }
    }
}

impl<'t> MyTableT<'t> for ValueInfo {
    fn unwrap(_joined: &'t FrozenVec<Box<(Field, MyTable)>>) -> Self {
        ValueInfo {}
    }
}

pub(super) struct FkInfo<'t, T: HasId> {
    pub joined: &'t FrozenVec<Box<(Field, MyTable)>>, // the table that we join onto
    pub inner: OnceCell<Rc<T::Dummy<'t>>>,
}

impl<'t, T: HasId> Clone for FkInfo<'t, T> {
    fn clone(&self) -> Self {
        Self {
            joined: self.joined,
            inner: self.inner.clone(),
        }
    }
}

impl<'t, T: HasId> FkInfo<'t, T> {
    pub(crate) fn joined(
        joined: &'t FrozenVec<Box<(Field, MyTable)>>,
        field: FieldAlias,
    ) -> Db<'t, T> {
        Db {
            info: FkInfo {
                joined,
                // prevent unnecessary join
                inner: OnceCell::from(Rc::new(T::build(Builder::new_full(joined, field.table)))),
            },
            field,
        }
    }
}

#[derive(Clone, Copy)]
pub(super) struct ValueInfo {}

pub(super) trait MyIdenT: Sized {
    type Info<'t>: MyTableT<'t>;
    fn iden_any(joins: &Joins, col: Field) -> Db<'_, Self> {
        let field = FieldAlias {
            table: joins.table,
            col,
        };
        Self::iden_full(&joins.joined, field)
    }
    fn iden_full(joined: &FrozenVec<Box<(Field, MyTable)>>, field: FieldAlias) -> Db<'_, Self> {
        Db {
            info: Self::Info::unwrap(joined),
            field,
        }
    }
}

impl<T: HasId> MyIdenT for T {
    type Info<'t> = FkInfo<'t, T>;
}

impl MyIdenT for i64 {
    type Info<'t> = ValueInfo;
}

impl MyIdenT for f64 {
    type Info<'t> = ValueInfo;
}

impl MyIdenT for bool {
    type Info<'t> = ValueInfo;
}

impl MyIdenT for String {
    type Info<'t> = ValueInfo;
}

impl<T: MyIdenT> MyIdenT for Option<T> {
    type Info<'t> = T::Info<'t>;
}

/// This is a dummy database column reference.
/// If the column has a foreign key constraint,
/// it can be dereferenced to join the table.
pub struct Db<'t, T: MyIdenT> {
    // invariant in `'t` because of the associated type
    pub(crate) info: T::Info<'t>,
    pub(crate) field: FieldAlias,
}

impl<'t, T: MyIdenT> Clone for Db<'t, T> {
    fn clone(&self) -> Self {
        Db {
            info: self.info.clone(),
            field: self.field,
        }
    }
}
impl<'t, T: MyIdenT> Copy for Db<'t, T> where T::Info<'t>: Copy {}

impl<'a, T: HasId> Db<'a, T> {
    pub fn id(&self) -> Db<'a, i64> {
        Db {
            info: ValueInfo {},
            field: self.field,
        }
    }
}

impl<'a, T: HasId> Deref for Db<'a, T> {
    type Target = T::Dummy<'a>;

    fn deref(&self) -> &Self::Target {
        self.info.inner.get_or_init(|| {
            let joined = self.info.joined;
            let name = self.field.col;
            let table = if let Some(item) = joined.iter().find(|item| item.0 == name) {
                &item.1
            } else {
                let table = MyTable {
                    name: T::NAME,
                    id: T::ID,
                    joins: Joins {
                        table: MyAlias::new(),
                        joined: FrozenVec::new(),
                    },
                };
                &joined.push_get(Box::new((name, table))).1
            };

            Rc::new(T::build(Builder::new(&table.joins)))
        })
    }
}

pub(crate) struct RawAlias(pub(crate) String);

impl Iden for RawAlias {
    fn unquoted(&self, s: &mut dyn std::fmt::Write) {
        write!(s, "{}", self.0).unwrap()
    }
    fn prepare(&self, s: &mut dyn std::fmt::Write, _q: sea_query::Quote) {
        self.unquoted(s)
    }
}