use crate::{
behavior::{push_unique, Concat, WithQuery},
fmt,
structure::{Insert, InsertClause, Select},
};
impl<'a> Insert<'a> {
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 insert_into(mut self, table_name: &'a str) -> Self {
self._insert_into = table_name.trim();
self
}
pub fn new() -> Self {
Self::default()
}
pub fn on_conflict(mut self, conflict: &'a str) -> Self {
self._on_conflict = conflict.trim();
self
}
pub fn overriding(mut self, option: &'a str) -> Self {
self._overriding = option.trim();
self
}
pub fn print(self) -> Self {
let fmts = fmt::one_line();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn select(mut self, select: Select<'a>) -> Self {
self._select = Some(select);
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: InsertClause, raw_sql: &str) -> Self {
self._raw_after.push((clause, raw_sql.trim().to_owned()));
self
}
pub fn raw_before(mut self, clause: InsertClause, raw_sql: &str) -> Self {
self._raw_before.push((clause, raw_sql.trim().to_owned()));
self
}
pub fn values(mut self, value: &str) -> Self {
push_unique(&mut self._values, value.trim().to_owned());
self
}
}
#[cfg(any(doc, feature = "postgresql"))]
impl<'a> Insert<'a> {
pub fn returning(mut self, output_name: &str) -> Self {
push_unique(&mut self._returning, output_name.trim().to_owned());
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 Insert<'_> {}
impl std::fmt::Display for Insert<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}
impl std::fmt::Debug for Insert<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let fmts = fmt::multiline();
write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
}
}