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
use super::build::*;
use super::{build::CompareOp, Resolvable};
use crate::entity::EntityID;
use crate::{entity::EntityColumn, model::Modelable, Entity, QueryInterface};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;

/// Any query that can have a WHERE clause attached to it
pub trait Filterable<'r, 'q>: Resolvable<'r, 'q>
where
    'q: 'r,
{
    type Table: Entity;
    type Output;

    fn by<C: EntityColumn<Entity = Self::Table>, G: Modelable + ?Sized>(
        self,
        col: C,
        given: &'r G,
    ) -> Filter<'r, 'q, Self, C, G>
    where
        Self: Sized,
    {
        Filter {
            wrap: self,
            col,
            op: CompareOp::Equals,
            given,
            _ghost: PhantomData,
        }
    }

    fn by_id<I: EntityID<Entity = Self::Table>>(
        self,
        given: &'r I,
    ) -> Filter<'r, 'q, Self, <Self::Table as Entity>::IDColumn, I>
    where
        Self: Sized,
    {
        Filter {
            wrap: self,
            col: Self::Table::id_column(),
            op: CompareOp::Equals,
            given,
            _ghost: PhantomData,
        }
    }

    fn by_op<C: EntityColumn<Entity = Self::Table>, G: Modelable + ?Sized>(
        self,
        col: C,
        op: CompareOp,
        given: &'r G,
    ) -> Filter<'r, 'q, Self, C, G>
    where
        Self: Sized,
    {
        Filter {
            wrap: self,
            col,
            op,
            given,
            _ghost: PhantomData,
        }
    }
}

/// A concrete WHERE clause
pub struct Filter<'r, 'q, F: Filterable<'r, 'q>, C: EntityColumn, G: Modelable + ?Sized>
where
    'q: 'r,
{
    wrap: F,
    col: C,
    op: CompareOp,
    given: &'r G,
    _ghost: PhantomData<&'q ()>,
}

impl<'r, 'q, F: Filterable<'r, 'q>, C: EntityColumn, G: Modelable + ?Sized> StaticVersion
    for Filter<'r, 'q, F, C, G>
where
    <F as StaticVersion>::Is: Filterable<'static, 'static>,
{
    type Is = Filter<'static, 'static, <F as StaticVersion>::Is, C, u64>;
}

impl<'r, 'q, F: Filterable<'r, 'q>, C: EntityColumn, G: Modelable + ?Sized> QueryComponent
    for Filter<'r, 'q, F, C, G>
where
    <F as StaticVersion>::Is: Filterable<'static, 'static>,
    'q: 'r,
{
    fn derive(&self) -> DerivedQuery {
        self.wrap.derive().add(
            QueryPart::Where,
            format!("`{}` {} ?", self.col.name(), self.op.ch()),
        )
    }

    fn contribute<H: Hasher>(&self, hasher: &mut H) {
        self.wrap.contribute(hasher);
        std::any::TypeId::of::<Self::Is>().hash(hasher);
        std::any::TypeId::of::<C>().hash(hasher);
    }

    fn bind(&self, stmt: &mut sqlite::Statement<'_>) -> Result<usize, crate::Error> {
        let next_index = self.wrap.bind(stmt)?;

        self.given.bind_to(stmt, next_index)?;

        Ok(next_index + 1)
    }
}

impl<'r, 'q, O, F: Filterable<'r, 'q, Output = O>, C: EntityColumn, G: Modelable + ?Sized>
    Resolvable<'r, 'q> for Filter<'r, 'q, F, C, G>
where
    <F as StaticVersion>::Is: Filterable<'static, 'static>,
    'q: 'r,
{
    type Output = O;

    fn qi(&self) -> &'r QueryInterface<'q> {
        self.wrap.qi()
    }
}

impl<'r, 'q, O, F: Filterable<'r, 'q, Output = O>, C: EntityColumn, G: Modelable + ?Sized>
    Filterable<'r, 'q> for Filter<'r, 'q, F, C, G>
where
    <F as StaticVersion>::Is: Filterable<'static, 'static>,
    'q: 'r,
{
    type Output = O;
    type Table = C::Entity;
}