lumus_sql_builder/sqlite/
delete.rs1use super::BuildableStatement;
2use crate::errors::SqlBuilderError;
3
4#[derive(Debug)]
6pub struct Delete {
7 table: String,
8 condition: Option<String>,
9}
10
11impl Delete {
12 pub fn new(table: &str) -> Self {
22 Self {
23 table: table.to_string(),
24 condition: None,
25 }
26 }
27
28 pub fn condition(&mut self, condition: String) -> &mut Self {
30 self.condition = Some(condition);
31 self
32 }
33
34 pub fn build(&self) -> Result<String, SqlBuilderError> {
35 if self.table.is_empty() {
36 return Err(SqlBuilderError::EmptyTableName);
37 }
38
39 if let Some(condition) = &self.condition {
40 if condition.is_empty() {
41 return Err(SqlBuilderError::EmptyCondition);
42 }
43 return Ok(format!("DELETE FROM {} WHERE {};", self.table, condition));
44 }
45
46 Ok(format!("DELETE FROM {};", self.table))
47 }
48}
49
50impl BuildableStatement for Delete {
52 fn build(&self) -> String {
53 self.build().unwrap()
54 }
55}