use crate::prelude::*;
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug)]
pub struct Select {
columns: Vec<SelectList>,
}
impl Select {
pub fn all() -> Self {
Self {
columns: vec![SelectList::All],
}
}
pub fn all_of_table(table: impl Into<Table>) -> Self {
Self {
columns: vec![SelectList::TableAll {
table: table.into(),
}],
}
}
pub fn new() -> Self {
Self { columns: vec![] }
}
pub fn column(mut self, table: impl Into<Table>, column: impl Into<Column>) -> Self {
let select_list = SelectList::TableColumn {
table: table.into(),
column: column.into(),
};
self.columns.push(select_list);
self
}
pub(crate) fn push(&mut self, select_list: impl Into<SelectList>) {
self.columns.push(select_list.into());
}
}
impl Sql for Select {
fn sql(&self, mut s: String, ctx: &Context) -> Result<String> {
s.push_str("SELECT ");
for (index, column) in self.columns.iter().enumerate() {
if index != self.columns.len() - 1 {
s = column.sql(s, ctx)?;
s.push_str(", ");
} else {
s = column.sql(s, ctx)?;
}
}
s.push(' ');
Ok(s)
}
}