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
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>,
}
/// 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}};
/// 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,
/// );
/// ```
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>>,
{
#[inline]
fn on<T>(self, conditions: T) -> JoinData<'a>
where
T: Into<ConditionTree<'a>>,
{
JoinData {
table: self.into(),
conditions: conditions.into(),
}
}
}