use crate::ast::{ConditionTree, Table};
#[derive(Debug, PartialEq, Clone)]
pub struct JoinData<'a> {
pub(crate) table: Table<'a>,
pub(crate) conditions: ConditionTree<'a>,
#[cfg(feature = "postgresql")]
pub(crate) lateral: bool,
}
impl<'a> JoinData<'a> {
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.lateral = true;
}
}
impl<'a, T> From<T> for JoinData<'a>
where
T: Into<Table<'a>>,
{
fn from(table: T) -> Self {
Self::all_from(table)
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum Join<'a> {
Inner(JoinData<'a>),
Left(JoinData<'a>),
Right(JoinData<'a>),
Full(JoinData<'a>),
}
pub trait Joinable<'a> {
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,
}
}
}