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
use super::{AstPass, QueryFragment, QueryId};
use crate::backend::Backend;
use crate::query_source::AppearsInFromClause;
use crate::{QueryResult, QuerySource};

/// This type represents a not existing from clause
///
/// Custom backends can provide a custom [`QueryFragment`]
/// impl by specializing the implementtion via
/// [`SqlDialect::EmptyFromClauseSyntax`](crate::backend::SqlDialect::EmptyFromClauseSyntax)
#[cfg_attr(
    feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
    cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")
)]
#[derive(Debug, Clone, Copy, QueryId)]
pub struct NoFromClause;

impl<DB> QueryFragment<DB> for NoFromClause
where
    Self: QueryFragment<DB, DB::EmptyFromClauseSyntax>,
    DB: Backend,
{
    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        <Self as QueryFragment<DB, DB::EmptyFromClauseSyntax>>::walk_ast(self, pass)
    }
}

impl<DB> QueryFragment<DB, crate::backend::sql_dialect::from_clause_syntax::AnsiSqlFromClauseSyntax>
    for NoFromClause
where
    DB: Backend<EmptyFromClauseSyntax = crate::backend::sql_dialect::from_clause_syntax::AnsiSqlFromClauseSyntax>,
{
    fn walk_ast<'b>(&'b self, _pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        Ok(())
    }
}

pub trait AsQuerySource {
    type QuerySource: QuerySource;

    fn as_query_source(&self) -> &Self::QuerySource;
}

impl<QS> AsQuerySource for QS
where
    QS: QuerySource,
{
    type QuerySource = Self;

    fn as_query_source(&self) -> &Self::QuerySource {
        self
    }
}

#[doc(hidden)]
pub struct FromClause<F: QuerySource> {
    pub(crate) source: F,
    pub(crate) from_clause: F::FromClause,
}

impl<F> AsQuerySource for FromClause<F>
where
    F: QuerySource,
{
    type QuerySource = F;

    fn as_query_source(&self) -> &Self::QuerySource {
        &self.source
    }
}

impl<F> QueryId for FromClause<F>
where
    F: QuerySource + QueryId,
{
    type QueryId = F::QueryId;

    const HAS_STATIC_QUERY_ID: bool = F::HAS_STATIC_QUERY_ID;
}

impl<F> Copy for FromClause<F>
where
    F: QuerySource + Copy,
    F::FromClause: Copy,
{
}

impl<F> Clone for FromClause<F>
where
    F: QuerySource + Clone,
    F::FromClause: Clone,
{
    fn clone(&self) -> Self {
        Self {
            source: self.source.clone(),
            from_clause: self.from_clause.clone(),
        }
    }
}

impl<F> std::fmt::Debug for FromClause<F>
where
    F: QuerySource,
    F::FromClause: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FromClause")
            .field("from_clause", &self.from_clause)
            .finish()
    }
}

impl<F: QuerySource> FromClause<F> {
    pub(crate) fn new(qs: F) -> Self {
        Self {
            from_clause: qs.from_clause(),
            source: qs,
        }
    }
}

impl<DB, F> QueryFragment<DB> for FromClause<F>
where
    F: QuerySource,
    DB: Backend,
    F::FromClause: QueryFragment<DB>,
{
    fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        pass.push_sql(" FROM ");
        self.from_clause.walk_ast(pass)
    }
}

impl<QS1, QS2> AppearsInFromClause<QS1> for FromClause<QS2>
where
    QS1: QuerySource,
    QS2: QuerySource,
    QS2: AppearsInFromClause<QS1>,
{
    type Count = <QS2 as AppearsInFromClause<QS1>>::Count;
}