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
use crate::query::predicate::{Eq, Gt, Lt, Neq, Op};
use crate::query::select::order::{Ascending, Descending, Ordered};
use crate::Table;

use std::marker::PhantomData;

pub struct Field<T, A> {
    name: &'static str,
    _table: PhantomData<T>,
    _type: PhantomData<A>,
}

impl<T, A> Field<T, A>
where
    T: Table,
{
    pub fn new(name: &'static str) -> Self {
        Self {
            name,
            _table: PhantomData,
            _type: PhantomData,
        }
    }

    pub fn eq<U>(self, rhs: U) -> Op<T, A, U, Eq> {
        Op::new(self, rhs)
    }

    pub fn neq<U>(self, rhs: U) -> Op<T, A, U, Neq> {
        Op::new(self, rhs)
    }

    pub fn gt<U>(self, rhs: U) -> Op<T, A, U, Gt> {
        Op::new(self, rhs)
    }

    pub fn lt<U>(self, rhs: U) -> Op<T, A, U, Lt> {
        Op::new(self, rhs)
    }

    pub fn then<T2>(self, next: T2) -> Then<Self, T2> {
        Then {
            head: self,
            tail: next,
        }
    }

    pub fn ascending(self) -> Ordered<T, A, Ascending> {
        Ordered::new(self)
    }

    pub fn descending(self) -> Ordered<T, A, Descending> {
        Ordered::new(self)
    }

    pub(crate) fn write_field(&self, sql: &mut String) {
        sql.push_str(T::NAME);
        sql.push('.');
        sql.push_str(self.name);
    }
}

impl<T, A> Copy for Field<T, A> {}

impl<T, A> Clone for Field<T, A> {
    fn clone(&self) -> Self {
        *self
    }
}

pub struct Then<H, T> {
    pub(crate) head: H,
    pub(crate) tail: T,
}

impl<H, T> Then<H, T> {
    pub fn then<T2>(self, next: T2) -> Then<Self, T2> {
        Then {
            head: self,
            tail: next,
        }
    }
}