surrealdb_core/sql/statements/
delete.rs1use crate::ctx::Context;
2use crate::dbs::{Iterator, Options, Statement};
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::sql::{Cond, Output, Timeout, Value, Values};
6use derive::Store;
7use reblessive::tree::Stk;
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12#[revisioned(revision = 2)]
13#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
14#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
15#[non_exhaustive]
16pub struct DeleteStatement {
17 #[revision(start = 2)]
18 pub only: bool,
19 pub what: Values,
20 pub cond: Option<Cond>,
21 pub output: Option<Output>,
22 pub timeout: Option<Timeout>,
23 pub parallel: bool,
24}
25
26impl DeleteStatement {
27 pub(crate) fn writeable(&self) -> bool {
29 true
30 }
31 pub(crate) async fn compute(
33 &self,
34 stk: &mut Stk,
35 ctx: &Context<'_>,
36 opt: &Options,
37 doc: Option<&CursorDoc<'_>>,
38 ) -> Result<Value, Error> {
39 opt.valid_for_db()?;
41 let mut i = Iterator::new();
43 let stm = Statement::from(self);
45 let opt = &opt.new_with_futures(false).with_projections(false);
47 for w in self.what.0.iter() {
49 let v = w.compute(stk, ctx, opt, doc).await?;
50 i.prepare(stk, ctx, opt, &stm, v).await.map_err(|e| match e {
51 Error::InvalidStatementTarget {
52 value: v,
53 } => Error::DeleteStatement {
54 value: v,
55 },
56 e => e,
57 })?;
58 }
59 match i.output(stk, ctx, opt, &stm).await? {
61 Value::Array(mut a) if self.only => match a.len() {
63 1 => Ok(a.remove(0)),
65 _ => Err(Error::SingleOnlyOutput),
67 },
68 v => Ok(v),
70 }
71 }
72}
73
74impl fmt::Display for DeleteStatement {
75 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76 write!(f, "DELETE")?;
77 if self.only {
78 f.write_str(" ONLY")?
79 }
80 write!(f, " {}", self.what)?;
81 if let Some(ref v) = self.cond {
82 write!(f, " {v}")?
83 }
84 if let Some(ref v) = self.output {
85 write!(f, " {v}")?
86 }
87 if let Some(ref v) = self.timeout {
88 write!(f, " {v}")?
89 }
90 if self.parallel {
91 f.write_str(" PARALLEL")?
92 }
93 Ok(())
94 }
95}