use crate::{
behavior::{push_unique, Concat, WithQuery},
fmt,
structure::{Select, SelectClause},
};
impl Select<'_> {
pub fn and(mut self, condition: &str) -> Self {
self = self.where_clause(condition);
self
}
pub fn as_string(&self) -> String {
let fmts = fmt::one_line();
self.concat(&fmts)
}
pub fn debug(self) -> Self {
let fmts = fmt::multiline();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn from(mut self, tables: &str) -> Self {
push_unique(&mut self._from, tables.trim().to_owned());
self
}
pub fn group_by(mut self, column: &str) -> Self {
push_unique(&mut self._group_by, column.trim().to_owned());
self
}
pub fn having(mut self, condition: &str) -> Self {
push_unique(&mut self._having, condition.trim().to_owned());
self
}
pub fn cross_join(mut self, table: &str) -> Self {
let table = table.trim();
let table = format!("CROSS JOIN {table}");
push_unique(&mut self._join, table);
self
}
pub fn inner_join(mut self, table: &str) -> Self {
let table = table.trim();
let table = format!("INNER JOIN {table}");
push_unique(&mut self._join, table);
self
}
pub fn left_join(mut self, table: &str) -> Self {
let table = table.trim();
let table = format!("LEFT JOIN {table}");
push_unique(&mut self._join, table);
self
}
pub fn right_join(mut self, table: &str) -> Self {
let table = table.trim();
let table = format!("RIGHT JOIN {table}");
push_unique(&mut self._join, table);
self
}
pub fn new() -> Self {
Self::default()
}
pub fn order_by(mut self, column: &str) -> Self {
push_unique(&mut self._order_by, column.trim().to_owned());
self
}
pub fn print(self) -> Self {
let fmts = fmt::one_line();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn raw(mut self, raw_sql: &str) -> Self {
push_unique(&mut self._raw, raw_sql.trim().to_owned());
self
}
pub fn raw_after(mut self, clause: SelectClause, raw_sql: &str) -> Self {
self._raw_after.push((clause, raw_sql.trim().to_owned()));
self
}
pub fn raw_before(mut self, clause: SelectClause, raw_sql: &str) -> Self {
self._raw_before.push((clause, raw_sql.trim().to_owned()));
self
}
pub fn select(mut self, column: &str) -> Self {
push_unique(&mut self._select, column.trim().to_owned());
self
}
pub fn where_clause(mut self, condition: &str) -> Self {
push_unique(&mut self._where, condition.trim().to_owned());
self
}
}
#[cfg(any(doc, feature = "postgresql"))]
impl<'a> Select<'a> {
pub fn except(mut self, select: Self) -> Self {
self._except.push(select);
self
}
pub fn intersect(mut self, select: Self) -> Self {
self._intersect.push(select);
self
}
pub fn limit(mut self, num: &'a str) -> Self {
self._limit = num.trim();
self
}
pub fn offset(mut self, num: &'a str) -> Self {
self._offset = num.trim();
self
}
pub fn union(mut self, select: Self) -> Self {
self._union.push(select);
self
}
pub fn with(mut self, name: &'a str, query: impl WithQuery + 'static) -> Self {
self._with.push((name.trim(), std::sync::Arc::new(query)));
self
}
}
impl WithQuery for Select<'_> {}
impl std::fmt::Display for Select<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}
impl std::fmt::Debug for Select<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let fmts = fmt::multiline();
write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
}
}