use crate::query::QueryBuilder;
#[derive(Clone)]
pub enum Column<'a> {
Text(String),
SubQuery(Box<dyn QueryBuilder<'a> + 'a>, String),
}
impl<'a> Column<'a> {
pub fn build(&self) -> String {
match self {
Column::Text(text) => text.clone(),
Column::SubQuery(sub_query, alias) => {
"(".to_string() + &sub_query.to_sql() + ") AS " + alias
}
}
}
}
impl<'a> std::fmt::Display for Column<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.build())
}
}
impl<'a> PartialEq for Column<'a> {
fn eq(&self, other: &Self) -> bool {
self.build() == other.build()
}
}
impl<'a> PartialEq<String> for Column<'a> {
fn eq(&self, other: &String) -> bool {
match self {
Column::Text(text) => text == other,
Column::SubQuery(sub_query, _) => sub_query.to_sql() == *other,
}
}
}
impl<'a> PartialEq<&str> for Column<'a> {
fn eq(&self, other: &&str) -> bool {
match self {
Column::Text(text) => text == other,
Column::SubQuery(sub_query, _) => sub_query.to_sql() == *other,
}
}
}