surrealdb_sql/statements/
delete.rs1use common::fmt::Fmt;
2use surrealdb_types::{SqlFormat, ToSql, write_sql};
3
4use crate::{Cond, CoverStmts, Explain, Expr, Literal, Output, With};
5
6#[derive(Clone, Debug, Eq, PartialEq)]
7#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
8pub struct DeleteStatement {
9 pub only: bool,
10 #[cfg_attr(feature = "arbitrary", arbitrary(with = crate::arbitrary::atleast_one))]
11 pub what: Vec<Expr>,
12 pub with: Option<With>,
13 pub cond: Option<Cond>,
14 pub output: Option<Output>,
15 pub timeout: Expr,
16 pub explain: Option<Explain>,
17}
18
19impl Default for DeleteStatement {
20 fn default() -> Self {
21 Self {
22 only: Default::default(),
23 what: Default::default(),
24 with: Default::default(),
25 cond: Default::default(),
26 output: Default::default(),
27 timeout: Expr::Literal(Literal::None),
28 explain: Default::default(),
29 }
30 }
31}
32impl ToSql for DeleteStatement {
33 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
34 write_sql!(f, fmt, "DELETE");
35 if self.only {
36 f.push_str(" ONLY")
37 }
38 write_sql!(f, fmt, " {}", Fmt::comma_separated(self.what.iter().map(CoverStmts)));
39 if let Some(ref v) = self.with {
40 write_sql!(f, fmt, " {v}");
41 }
42 if let Some(ref v) = self.cond {
43 write_sql!(f, fmt, " {v}");
44 }
45 if let Some(ref v) = self.output {
46 write_sql!(f, fmt, " {v}");
47 }
48
49 if !matches!(self.timeout, Expr::Literal(Literal::None)) {
50 write_sql!(f, fmt, " TIMEOUT {}", CoverStmts(&self.timeout));
51 }
52
53 if let Some(ref v) = self.explain {
54 write_sql!(f, fmt, " {v}");
55 }
56 }
57}