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
use crate::ast::{ConditionTree, Table};

/// The `JOIN` table and conditions.
#[derive(Debug, PartialEq, Clone)]
pub struct JoinData<'a> {
    pub(crate) table: Table<'a>,
    pub(crate) conditions: ConditionTree<'a>,
    pub(crate) lateral: bool,
}

impl<'a> JoinData<'a> {
    /// Implement a join with no conditions.
    pub fn all_from(table: impl Into<Table<'a>>) -> Self {
        Self {
            table: table.into(),
            conditions: ConditionTree::NoCondition,
            lateral: false,
        }
    }

    pub fn lateral(mut self) -> Self {
        self.lateral = true;
        self
    }
}

impl<'a, T> From<T> for JoinData<'a>
where
    T: Into<Table<'a>>,
{
    fn from(table: T) -> Self {
        Self::all_from(table)
    }
}

/// A representation of a `JOIN` statement.
#[derive(Debug, PartialEq, Clone)]
pub enum Join<'a> {
    /// Implements an `INNER JOIN` with given `JoinData`.
    Inner(JoinData<'a>),
    /// Implements an `LEFT JOIN` with given `JoinData`.
    Left(JoinData<'a>),
    /// Implements an `RIGHT JOIN` with given `JoinData`.
    Right(JoinData<'a>),
    /// Implements an `FULL JOIN` with given `JoinData`.
    Full(JoinData<'a>),
}

/// An item that can be joined.
pub trait Joinable<'a> {
    /// Add the `JOIN` conditions.
    ///
    /// ```rust
    /// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
    /// # fn main() -> Result<(), quaint::error::Error> {
    /// let join_data = "b".on(("b", "id").equals(Column::from(("a", "id"))));
    /// let query = Select::from_table("a").inner_join(join_data);
    /// let (sql, _) = Sqlite::build(query)?;
    ///
    /// assert_eq!(
    ///     "SELECT `a`.* FROM `a` INNER JOIN `b` ON `b`.`id` = `a`.`id`",
    ///     sql,
    /// );
    /// # Ok(())
    /// # }
    /// ```
    fn on<T>(self, conditions: T) -> JoinData<'a>
    where
        T: Into<ConditionTree<'a>>;
}

impl<'a, U> Joinable<'a> for U
where
    U: Into<Table<'a>>,
{
    fn on<T>(self, conditions: T) -> JoinData<'a>
    where
        T: Into<ConditionTree<'a>>,
    {
        JoinData {
            table: self.into(),
            conditions: conditions.into(),
            lateral: false,
        }
    }
}

impl<'a> Joinable<'a> for JoinData<'a> {
    fn on<T>(self, conditions: T) -> JoinData<'a>
    where
        T: Into<ConditionTree<'a>>,
    {
        let conditions = match self.conditions {
            ConditionTree::NoCondition => conditions.into(),
            cond => cond.and(conditions.into()),
        };

        JoinData {
            table: self.table,
            conditions,
            lateral: false,
        }
    }
}