use super::BuildableStatement;
use crate::errors::SqlBuilderError;
#[derive(Debug)]
pub struct Delete {
table: String,
condition: Option<String>,
}
impl Delete {
pub fn new(table: &str) -> Self {
Self {
table: table.to_string(),
condition: None,
}
}
pub fn condition(&mut self, condition: String) -> &mut Self {
self.condition = Some(condition);
self
}
pub fn build(&self) -> Result<String, SqlBuilderError> {
if self.table.is_empty() {
return Err(SqlBuilderError::EmptyTableName);
}
if let Some(condition) = &self.condition {
if condition.is_empty() {
return Err(SqlBuilderError::EmptyCondition);
}
return Ok(format!("DELETE FROM {} WHERE {};", self.table, condition));
}
Ok(format!("DELETE FROM {};", self.table))
}
}
impl BuildableStatement for Delete {
fn build(&self) -> String {
self.build().unwrap()
}
}