toasty_core/stmt/
delete.rs

1use super::{Node, Query, Returning, Source, Statement, Visit, VisitMut};
2use crate::stmt::{self, Filter};
3
4#[derive(Debug, Clone, PartialEq)]
5pub struct Delete {
6    /// Source of data to delete from
7    pub from: Source,
8
9    /// WHERE
10    pub filter: Filter,
11
12    /// Optionally, return something
13    pub returning: Option<Returning>,
14}
15
16impl Delete {
17    pub fn selection(&self) -> Query {
18        stmt::Query::new_select(self.from.model_id_unwrap(), self.filter.clone())
19    }
20}
21
22impl Statement {
23    pub fn is_delete(&self) -> bool {
24        matches!(self, Statement::Delete(..))
25    }
26
27    /// Attempts to return a reference to an inner [`Delete`].
28    ///
29    /// * If `self` is a [`Statement::Delete`], a reference to the inner [`Delete`] is
30    ///   returned wrapped in [`Some`].
31    /// * Else, [`None`] is returned.
32    pub fn as_delete(&self) -> Option<&Delete> {
33        match self {
34            Self::Delete(delete) => Some(delete),
35            _ => None,
36        }
37    }
38
39    /// Consumes `self` and attempts to return the inner [`Delete`].
40    ///
41    /// * If `self` is a [`Statement::Delete`], inner [`Delete`] is returned wrapped in
42    ///   [`Some`].
43    /// * Else, [`None`] is returned.
44    pub fn into_delete(self) -> Option<Delete> {
45        match self {
46            Self::Delete(delete) => Some(delete),
47            _ => None,
48        }
49    }
50
51    /// Consumes `self` and returns the inner [`Delete`].
52    ///
53    /// # Panics
54    ///
55    /// If `self` is not a [`Statement::Delete`].
56    pub fn unwrap_delete(self) -> Delete {
57        match self {
58            Self::Delete(delete) => delete,
59            v => panic!("expected `Delete`, found {v:#?}"),
60        }
61    }
62}
63
64impl From<Delete> for Statement {
65    fn from(src: Delete) -> Self {
66        Self::Delete(src)
67    }
68}
69
70impl Node for Delete {
71    fn visit<V: Visit>(&self, mut visit: V) {
72        visit.visit_stmt_delete(self);
73    }
74
75    fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
76        visit.visit_stmt_delete_mut(self);
77    }
78}