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
use super::{DynCol, ToQuery};

/// A struct representing SQL joins.
pub struct Join {
    column: &'static DynCol,
    on_column: &'static DynCol,
    join_type: JoinType,
}

/// The different types of SQL joins.
pub enum JoinType {
    Inner,
    Outer,
    Left,
    Right,
}

impl Join {
    pub const fn new(c1: &'static DynCol, c2: &'static DynCol, ty: JoinType) -> Join {
        Self {
            column: c1,
            on_column: c2,
            join_type: ty,
        }
    }
}

impl ToQuery for Join {
    fn to_sql(&self) -> String {
        let join_type: &'static str = match self.join_type {
            JoinType::Inner => "INNER",
            JoinType::Outer => "OUTER",
            JoinType::Left => "LEFT",
            JoinType::Right => "RIGHT",
        };

        format!(
            "{join_type} JOIN {0} ON {1}.{2} = {0}.{3}",
            self.on_column.table_name(),
            self.column.table_name(),
            self.column.column_name(),
            self.on_column.column_name()
        )
    }
}

impl PartialEq for Join {
    fn eq(&self, other: &Self) -> bool {
        self.to_sql().eq(&other.to_sql())
    }
}