use super::BuildableStatement;
use crate::errors::SqlBuilderError;
#[derive(Debug)]
pub enum JoinType {
Inner,
Left,
Right,
RightOuter,
LeftOuter,
Full,
}
impl BuildableStatement for JoinType {
fn build(&self) -> String {
String::from(match self {
Self::Inner => "INNER",
Self::Left => "LEFT",
Self::Right => "RIGHT",
Self::RightOuter => "RIGHT OUTER",
Self::LeftOuter => "LEFT OUTER",
Self::Full => "FULL",
})
}
}
#[derive(Debug)]
pub struct Join {
table: String,
join_type: JoinType,
on: String,
}
impl Join {
pub fn new(table: &str, join_type: JoinType, on: &str) -> Self {
Self {
table: table.to_string(),
join_type,
on: on.to_string(),
}
}
pub fn build(self) -> Result<String, SqlBuilderError> {
if self.table.is_empty() {
return Err(SqlBuilderError::EmptyTableName);
}
if self.on.is_empty() {
return Err(SqlBuilderError::EmptyOnClause);
}
Ok(format!(
"{} JOIN {} ON {}",
self.join_type.build(),
self.table,
self.on
))
}
}