use nom::types::CompleteByteSlice;
use std::{fmt, str};
use common::{opt_multispace, statement_terminator, table_reference};
use condition::ConditionExpression;
use keywords::escape_if_keyword;
use select::where_clause;
use table::Table;
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct DeleteStatement {
pub table: Table,
pub where_clause: Option<ConditionExpression>,
}
impl fmt::Display for DeleteStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DELETE FROM ")?;
write!(f, "{}", escape_if_keyword(&self.table.name))?;
if let Some(ref where_clause) = self.where_clause {
write!(f, " WHERE ")?;
write!(f, "{}", where_clause)?;
}
Ok(())
}
}
named!(pub deletion<CompleteByteSlice, DeleteStatement>,
do_parse!(
tag_no_case!("delete") >>
delimited!(opt_multispace, tag_no_case!("from"), opt_multispace) >>
table: table_reference >>
cond: opt!(where_clause) >>
statement_terminator >>
({
DeleteStatement {
table: table,
where_clause: cond,
}
})
)
);
#[cfg(test)]
mod tests {
use super::*;
use column::Column;
use common::{Literal, Operator};
use condition::ConditionBase::*;
use condition::ConditionExpression::*;
use condition::ConditionTree;
use table::Table;
#[test]
fn simple_delete() {
let qstring = "DELETE FROM users;";
let res = deletion(CompleteByteSlice(qstring.as_bytes()));
assert_eq!(
res.unwrap().1,
DeleteStatement {
table: Table::from("users"),
..Default::default()
}
);
}
#[test]
fn delete_with_where_clause() {
let qstring = "DELETE FROM users WHERE id = 1;";
let res = deletion(CompleteByteSlice(qstring.as_bytes()));
let expected_left = Base(Field(Column::from("id")));
let expected_where_cond = Some(ComparisonOp(ConditionTree {
left: Box::new(expected_left),
right: Box::new(Base(Literal(Literal::Integer(1)))),
operator: Operator::Equal,
}));
assert_eq!(
res.unwrap().1,
DeleteStatement {
table: Table::from("users"),
where_clause: expected_where_cond,
..Default::default()
}
);
}
#[test]
fn format_delete() {
let qstring = "DELETE FROM users WHERE id = 1";
let expected = "DELETE FROM users WHERE id = 1";
let res = deletion(CompleteByteSlice(qstring.as_bytes()));
assert_eq!(format!("{}", res.unwrap().1), expected);
}
}