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
use crate::backend::{Backend, DieselReserveSpecialization};
use crate::query_builder::*;
use crate::result::QueryResult;
use std::marker::PhantomData;

#[doc(hidden)] // used by the table macro
pub trait StaticQueryFragment {
    type Component: 'static;
    const STATIC_COMPONENT: &'static Self::Component;
}

#[derive(Debug, Copy, Clone)]
#[doc(hidden)] // used by the table macro
pub struct StaticQueryFragmentInstance<T>(PhantomData<T>);

impl<T> StaticQueryFragmentInstance<T> {
    #[doc(hidden)] // used by the table macro
    pub const fn new() -> Self {
        Self(PhantomData)
    }
}

impl<T, DB> QueryFragment<DB> for StaticQueryFragmentInstance<T>
where
    DB: Backend + DieselReserveSpecialization,
    T: StaticQueryFragment,
    T::Component: QueryFragment<DB>,
{
    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        T::STATIC_COMPONENT.walk_ast(pass)
    }
}

#[derive(Debug, Copy, Clone)]
#[doc(hidden)] // used by the table macro
pub struct Identifier<'a>(pub &'a str);

impl<'a, DB: Backend> QueryFragment<DB> for Identifier<'a> {
    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        out.push_identifier(self.0)
    }
}

pub trait MiddleFragment<DB: Backend> {
    fn push_sql(&self, pass: AstPass<'_, '_, DB>);
}

impl<'a, DB: Backend> MiddleFragment<DB> for &'a str {
    fn push_sql(&self, mut pass: AstPass<'_, '_, DB>) {
        pass.push_sql(self);
    }
}

#[derive(Debug, Copy, Clone)]
#[doc(hidden)] // used by the table macro
pub struct InfixNode<T, U, M> {
    lhs: T,
    rhs: U,
    middle: M,
}

impl<T, U, M> InfixNode<T, U, M> {
    #[doc(hidden)] // used by the table macro
    pub const fn new(lhs: T, rhs: U, middle: M) -> Self {
        InfixNode { lhs, rhs, middle }
    }
}

impl<T, U, DB, M> QueryFragment<DB> for InfixNode<T, U, M>
where
    DB: Backend + DieselReserveSpecialization,
    T: QueryFragment<DB>,
    U: QueryFragment<DB>,
    M: MiddleFragment<DB>,
{
    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        self.lhs.walk_ast(out.reborrow())?;
        self.middle.push_sql(out.reborrow());
        self.rhs.walk_ast(out.reborrow())?;
        Ok(())
    }
}