1use nom::{
3 bytes::complete::tag_no_case, character::complete::multispace0, error::Error, Err, IResult,
4};
5
6use crate::{
7 common::parse_identifier,
8 r#where::{parse_where_clause, WhereClause},
9};
10
11#[derive(Debug, PartialEq)]
13pub struct DeleteQuery {
14 pub table: String,
16 pub conditions: Vec<WhereClause>,
18}
19
20impl<'a> TryFrom<&'a str> for DeleteQuery {
21 type Error = Err<Error<&'a str>>;
22
23 fn try_from(value: &'a str) -> Result<Self, Self::Error> {
24 Ok(parse_delete(value)?.1)
25 }
26}
27
28pub fn parse_delete(input: &str) -> IResult<&str, DeleteQuery> {
30 let (input, _) = tag_no_case("delete from ")(input)?;
31 let (input, table) = parse_identifier(input)?;
32 let (input, _) = multispace0(input)?;
33 let (input, conditions) = parse_where_clause(input)?;
34
35 Ok((
36 input,
37 DeleteQuery {
38 table: table.to_string(),
39 conditions,
40 },
41 ))
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use crate::*;
48
49 #[test]
50 fn test_parse_delete() {
51 let input = "delete from person where id = ?";
52
53 let expected = DeleteQuery {
54 table: "person".to_string(),
55 conditions: vec![WhereClause {
56 column: Column::Identifier("id".to_string()),
57 operator: r#where::ComparisonOperator::Equal,
58 value: Value::Variable(Variable::Placeholder),
59 }],
60 };
61
62 assert_eq!(parse_delete(input), Ok(("", expected)));
63 }
64}