sqlparser_mysql/dms/
delete.rs

1use std::{fmt, str};
2
3use nom::bytes::complete::tag_no_case;
4use nom::character::complete::multispace1;
5use nom::combinator::opt;
6use nom::sequence::{delimited, tuple};
7use nom::IResult;
8
9use base::condition::ConditionExpression;
10use base::error::ParseSQLError;
11use base::table::Table;
12use base::{CommonParser, DisplayUtil};
13
14// FIXME TODO
15/// `DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias]
16///     [PARTITION (partition_name [, partition_name] ...)]
17///     [WHERE where_condition]
18///     [ORDER BY ...]
19///     [LIMIT row_count]`
20#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
21pub struct DeleteStatement {
22    pub table: Table,
23    pub where_clause: Option<ConditionExpression>,
24}
25
26impl DeleteStatement {
27    pub fn parse(i: &str) -> IResult<&str, DeleteStatement, ParseSQLError<&str>> {
28        let (remaining_input, (_, _, table, where_clause, _)) = tuple((
29            tag_no_case("DELETE"),
30            delimited(multispace1, tag_no_case("FROM"), multispace1),
31            Table::schema_table_reference,
32            opt(ConditionExpression::parse),
33            CommonParser::statement_terminator,
34        ))(i)?;
35
36        Ok((
37            remaining_input,
38            DeleteStatement {
39                table,
40                where_clause,
41            },
42        ))
43    }
44}
45
46impl fmt::Display for DeleteStatement {
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        write!(f, "DELETE FROM ")?;
49        write!(f, "{}", DisplayUtil::escape_if_keyword(&self.table.name))?;
50        if let Some(ref where_clause) = self.where_clause {
51            write!(f, " WHERE ")?;
52            write!(f, "{}", where_clause)?;
53        }
54        Ok(())
55    }
56}