1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use super::{Node, Query, Returning, Source, Statement, Visit, VisitMut};
use crate::stmt::{self, Filter};
/// A `DELETE` statement that removes existing records.
///
/// Specifies a source to delete from, a filter selecting which records to
/// delete, and an optional returning clause.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::stmt::{Delete, Source, Filter};
/// use toasty_core::schema::app::ModelId;
///
/// let delete = Delete {
/// from: Source::from(ModelId(0)),
/// filter: Filter::default(),
/// returning: None,
/// };
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Delete {
/// The source to delete from (`FROM` clause).
pub from: Source,
/// Filter selecting which records to delete (`WHERE` clause).
pub filter: Filter,
/// Optional `RETURNING` clause.
pub returning: Option<Returning>,
}
impl Delete {
/// Returns a [`Query`] that selects the records this delete would remove.
pub fn selection(&self) -> Query {
stmt::Query::new_select(self.from.model_id_unwrap(), self.filter.clone())
}
}
impl Statement {
/// Returns `true` if this statement is a [`Delete`].
pub fn is_delete(&self) -> bool {
matches!(self, Statement::Delete(..))
}
/// Attempts to return a reference to an inner [`Delete`].
///
/// * If `self` is a [`Statement::Delete`], a reference to the inner [`Delete`] is
/// returned wrapped in [`Some`].
/// * Else, [`None`] is returned.
pub fn as_delete(&self) -> Option<&Delete> {
match self {
Self::Delete(delete) => Some(delete),
_ => None,
}
}
/// Consumes `self` and attempts to return the inner [`Delete`].
///
/// * If `self` is a [`Statement::Delete`], inner [`Delete`] is returned wrapped in
/// [`Some`].
/// * Else, [`None`] is returned.
pub fn into_delete(self) -> Option<Delete> {
match self {
Self::Delete(delete) => Some(delete),
_ => None,
}
}
/// Consumes `self` and returns the inner [`Delete`].
///
/// # Panics
///
/// If `self` is not a [`Statement::Delete`].
pub fn into_delete_unwrap(self) -> Delete {
match self {
Self::Delete(delete) => delete,
v => panic!("expected `Delete`, found {v:#?}"),
}
}
}
impl From<Delete> for Statement {
fn from(src: Delete) -> Self {
Self::Delete(src)
}
}
impl Node for Delete {
fn visit<V: Visit>(&self, mut visit: V) {
visit.visit_stmt_delete(self);
}
fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
visit.visit_stmt_delete_mut(self);
}
}