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
use expression::SelectableExpression;
use query_builder::*;
use result::QueryResult;
use super::{QuerySource, Table};
use types::IntoNullable;

#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct InnerJoinSource<Left, Right> {
    left: Left,
    right: Right,
}

impl<Left, Right> InnerJoinSource<Left, Right> {
    pub fn new(left: Left, right: Right) -> Self {
        InnerJoinSource {
            left: left,
            right: right,
        }
    }
}

impl<Left, Right> QuerySource for InnerJoinSource<Left, Right> where
    Left: Table + JoinTo<Right, Inner>,
    Right: Table,
{
    type FromClause = <Left as JoinTo<Right, Inner>>::JoinClause;

    fn from_clause(&self) -> Self::FromClause {
        self.left.join_clause(Inner)
    }
}

impl<Left, Right> AsQuery for InnerJoinSource<Left, Right> where
    Left: Table + JoinTo<Right, Inner>,
    Right: Table,
    (Left::AllColumns, Right::AllColumns): SelectableExpression<
                                   InnerJoinSource<Left, Right>,
                                   (Left::SqlType, Right::SqlType),
                               >,
{
    type SqlType = (Left::SqlType, Right::SqlType);
    type Query = SelectStatement<
        (Left::SqlType, Right::SqlType),
        (Left::AllColumns, Right::AllColumns),
        Self,
    >;

    fn as_query(self) -> Self::Query {
        SelectStatement::simple((Left::all_columns(), Right::all_columns()), self)
    }
}

impl_query_id!(InnerJoinSource<Left, Right>);

#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct LeftOuterJoinSource<Left, Right> {
    left: Left,
    right: Right,
}

impl<Left, Right> LeftOuterJoinSource<Left, Right> {
    pub fn new(left: Left, right: Right) -> Self {
        LeftOuterJoinSource {
            left: left,
            right: right,
        }
    }
}

impl<Left, Right> QuerySource for LeftOuterJoinSource<Left, Right> where
    Left: Table + JoinTo<Right, LeftOuter>,
    Right: Table,
{
    type FromClause = <Left as JoinTo<Right, LeftOuter>>::JoinClause;

    fn from_clause(&self) -> Self::FromClause {
        self.left.join_clause(LeftOuter)
    }
}

impl<Left, Right> AsQuery for LeftOuterJoinSource<Left, Right> where
    Left: Table + JoinTo<Right, LeftOuter>,
    Right: Table,
    Right::SqlType: IntoNullable,
    (Left::AllColumns, Right::AllColumns): SelectableExpression<
                                   LeftOuterJoinSource<Left, Right>,
                                   (Left::SqlType, <Right::SqlType as IntoNullable>::Nullable),
                               >,
{
    type SqlType = (Left::SqlType, <Right::SqlType as IntoNullable>::Nullable);
    type Query = SelectStatement<
        Self::SqlType,
        (Left::AllColumns, Right::AllColumns),
        Self,
    >;

    fn as_query(self) -> Self::Query {
        SelectStatement::simple((Left::all_columns(), Right::all_columns()), self)
    }
}

impl_query_id!(LeftOuterJoinSource<Left, Right>);

/// Indicates that two tables can be used together in a JOIN clause.
/// Implementations of this trait will be generated for you automatically by
/// the [association annotations](FIXME: Add link) from codegen.
pub trait JoinTo<T: Table, JoinType>: Table {
    #[doc(hidden)]
    type JoinClause;
    #[doc(hidden)]
    fn join_clause(&self, join_type: JoinType) -> Self::JoinClause;
}

use backend::Backend;

#[doc(hidden)]
#[derive(Clone, Copy, Debug)]
pub struct Inner;

impl<DB: Backend> QueryFragment<DB> for Inner {
    fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
        out.push_sql(" INNER");
        Ok(())
    }

    fn collect_binds(&self, _out: &mut DB::BindCollector) -> QueryResult<()> {
        Ok(())
    }

    fn is_safe_to_cache_prepared(&self) -> bool {
        true
    }
}

#[doc(hidden)]
#[derive(Clone, Copy, Debug)]
pub struct LeftOuter;

impl<DB: Backend> QueryFragment<DB> for LeftOuter {
    fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
        out.push_sql(" LEFT OUTER");
        Ok(())
    }

    fn collect_binds(&self, _out: &mut DB::BindCollector) -> QueryResult<()> {
        Ok(())
    }

    fn is_safe_to_cache_prepared(&self) -> bool {
        true
    }
}