use crate::column::IntoExpr;
use crate::ir::{Boolean, Join, JoinKind, Operator, TableRef, Where, WhereClause};
use crate::query::filters::{HasWheres, filters};
#[derive(Clone, Debug)]
pub struct JoinClause {
kind: JoinKind,
table: TableRef,
on: Vec<WhereClause>,
}
impl JoinClause {
pub(crate) fn new(kind: JoinKind, table: TableRef) -> Self {
JoinClause {
kind,
table,
on: Vec::new(),
}
}
pub(crate) fn into_join(self) -> Join {
Join {
kind: self.kind,
table: self.table,
on: self.on,
}
}
fn compare(
mut self,
boolean: Boolean,
first: impl IntoExpr,
op: impl Into<Operator>,
second: impl IntoExpr,
) -> Self {
self.on.push(WhereClause {
boolean,
negated: false,
filter: Where::Compare {
left: first.into_expr(),
op: op.into(),
right: second.into_expr(),
},
});
self
}
pub fn on(self, first: impl IntoExpr, op: impl Into<Operator>, second: impl IntoExpr) -> Self {
self.compare(Boolean::And, first, op, second)
}
pub fn or_on(
self,
first: impl IntoExpr,
op: impl Into<Operator>,
second: impl IntoExpr,
) -> Self {
self.compare(Boolean::Or, first, op, second)
}
filters!();
}
impl HasWheres for JoinClause {
fn wheres_mut(&mut self) -> &mut Vec<WhereClause> {
&mut self.on
}
}