Debian Codegraph @

src/query_builder/delete_statement/mod.rs

raw
1 use crate::backend::DieselReserveSpecialization;
2 use crate::dsl::{Filter, IntoBoxed, OrFilter};
3 use crate::expression::{AppearsOnTable, SelectableExpression};
4 use crate::query_builder::returning_clause::*;
5 use crate::query_builder::where_clause::*;
6 use crate::query_builder::*;
7 use crate::query_dsl::methods::{BoxedDsl, FilterDsl, OrFilterDsl};
8 use crate::query_dsl::RunQueryDsl;
9 use crate::query_source::{QuerySource, Table};
10
11 #[must_use = "Queries are only executed when calling `load`, `get_result` or similar."]
12 /// Represents a SQL `DELETE` statement.
13 ///
14 /// The type parameters on this struct represent:
15 ///
16 /// - `T`: The table we are deleting from.
17 /// - `U`: The `WHERE` clause of this query. The exact types used to represent
18 /// this are private, and you should not make any assumptions about them.
19 /// - `Ret`: The `RETURNING` clause of this query. The exact types used to
20 /// represent this are private. You can safely rely on the default type
21 /// representing the lack of a `RETURNING` clause.
22 pub struct DeleteStatement<T: QuerySource, U, Ret = NoReturningClause> {
23 from_clause: FromClause<T>,
24 where_clause: U,
25 returning: Ret,
26 }
27
28 impl<T, U, Ret> Clone for DeleteStatement<T, U, Ret>
29 where
30 T: QuerySource,
31 FromClause<T>: Clone,
32 U: Clone,
33 Ret: Clone,
34 {
35 fn clone(&self) -> Self {
36 Self {
37 from_clause: self.from_clause.clone(),
38 where_clause: self.where_clause.clone(),
39 returning: self.returning.clone(),
40 }
41 }
42 }
43
44 impl<T, U, Ret> std::fmt::Debug for DeleteStatement<T, U, Ret>
45 where
46 T: QuerySource,
47 FromClause<T>: std::fmt::Debug,
48 U: std::fmt::Debug,
49 Ret: std::fmt::Debug,
50 {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.debug_struct("DeleteStatement")
53 .field("from_clause", &self.from_clause)
54 .field("where_clause", &self.where_clause)
55 .field("returning", &self.returning)
56 .finish()
57 }
58 }
59
60 impl<T, U, Ret> QueryId for DeleteStatement<T, U, Ret>
61 where
62 T: QuerySource + QueryId + 'static,
63 U: QueryId,
64 Ret: QueryId,
65 {
66 type QueryId = DeleteStatement<T, U::QueryId, Ret::QueryId>;
67
68 const HAS_STATIC_QUERY_ID: bool =
69 T::HAS_STATIC_QUERY_ID && U::HAS_STATIC_QUERY_ID && Ret::HAS_STATIC_QUERY_ID;
70 }
71
72 /// A `DELETE` statement with a boxed `WHERE` clause
73 pub type BoxedDeleteStatement<'a, DB, T, Ret = NoReturningClause> =
74 DeleteStatement<T, BoxedWhereClause<'a, DB>, Ret>;
75
76 impl<T: QuerySource, U> DeleteStatement<T, U, NoReturningClause> {
77 pub(crate) fn new(table: T, where_clause: U) -> Self {
78 DeleteStatement {
79 from_clause: FromClause::new(table),
80 where_clause,
81 returning: NoReturningClause,
82 }
83 }
84
85 /// Adds the given predicate to the `WHERE` clause of the statement being
86 /// constructed.
87 ///
88 /// If there is already a `WHERE` clause, the predicate will be appended
89 /// with `AND`. There is no difference in behavior between
90 /// `delete(table.filter(x))` and `delete(table).filter(x)`.
91 ///
92 /// # Example
93 ///
94 /// ```rust
95 /// # include!("../../doctest_setup.rs");
96 /// #
97 /// # fn main() {
98 /// # use schema::users::dsl::*;
99 /// # let connection = &mut establish_connection();
100 /// let deleted_rows = diesel::delete(users)
101 /// .filter(name.eq("Sean"))
102 /// .execute(connection);
103 /// assert_eq!(Ok(1), deleted_rows);
104 ///
105 /// let expected_names = vec!["Tess".to_string()];
106 /// let names = users.select(name).load(connection);
107 ///
108 /// assert_eq!(Ok(expected_names), names);
109 /// # }
110 /// ```
111 pub fn filter<Predicate>(self, predicate: Predicate) -> Filter<Self, Predicate>
112 where
113 Self: FilterDsl<Predicate>,
114 {
115 FilterDsl::filter(self, predicate)
116 }
117
118 /// Adds to the `WHERE` clause of a query using `OR`
119 ///
120 /// If there is already a `WHERE` clause, the result will be `(old OR new)`.
121 /// Calling `foo.filter(bar).or_filter(baz)`
122 /// is identical to `foo.filter(bar.or(baz))`.
123 /// However, the second form is much harder to do dynamically.
124 ///
125 /// # Example
126 ///
127 /// ```rust
128 /// # include!("../../doctest_setup.rs");
129 /// #
130 /// # fn main() {
131 /// # use schema::users::dsl::*;
132 /// # let connection = &mut establish_connection();
133 /// let deleted_rows = diesel::delete(users)
134 /// .filter(name.eq("Sean"))
135 /// .or_filter(name.eq("Tess"))
136 /// .execute(connection);
137 /// assert_eq!(Ok(2), deleted_rows);
138 ///
139 /// let num_users = users.count().first(connection);
140 ///
141 /// assert_eq!(Ok(0), num_users);
142 /// # }
143 /// ```
144 pub fn or_filter<Predicate>(self, predicate: Predicate) -> OrFilter<Self, Predicate>
145 where
146 Self: OrFilterDsl<Predicate>,
147 {
148 OrFilterDsl::or_filter(self, predicate)
149 }
150
151 /// Boxes the `WHERE` clause of this delete statement.
152 ///
153 /// This is useful for cases where you want to conditionally modify a query,
154 /// but need the type to remain the same. The backend must be specified as
155 /// part of this. It is not possible to box a query and have it be useable
156 /// on multiple backends.
157 ///
158 /// A boxed query will incur a minor performance penalty, as the query builder
159 /// can no longer be inlined by the compiler. For most applications this cost
160 /// will be minimal.
161 ///
162 /// ### Example
163 ///
164 /// ```rust
165 /// # include!("../../doctest_setup.rs");
166 /// #
167 /// # fn main() {
168 /// # run_test().unwrap();
169 /// # }
170 /// #
171 /// # fn run_test() -> QueryResult<()> {
172 /// # use std::collections::HashMap;
173 /// # use schema::users::dsl::*;
174 /// # let connection = &mut establish_connection();
175 /// # let mut params = HashMap::new();
176 /// # params.insert("sean_has_been_a_jerk", true);
177 /// let mut query = diesel::delete(users).into_boxed();
178 ///
179 /// if params["sean_has_been_a_jerk"] {
180 /// query = query.filter(name.eq("Sean"));
181 /// }
182 ///
183 /// let deleted_rows = query.execute(connection)?;
184 /// assert_eq!(1, deleted_rows);
185 ///
186 /// let expected_names = vec!["Tess"];
187 /// let names = users.select(name).load::<String>(connection)?;
188 ///
189 /// assert_eq!(expected_names, names);
190 /// # Ok(())
191 /// # }
192 /// ```
193 pub fn into_boxed<'a, DB>(self) -> IntoBoxed<'a, Self, DB>
194 where
195 DB: Backend,
196 Self: BoxedDsl<'a, DB>,
197 {
198 BoxedDsl::internal_into_boxed(self)
199 }
200 }
201
202 impl<T, U, Ret, Predicate> FilterDsl<Predicate> for DeleteStatement<T, U, Ret>
203 where
204 U: WhereAnd<Predicate>,
205 Predicate: AppearsOnTable<T>,
206 T: QuerySource,
207 {
208 type Output = DeleteStatement<T, U::Output, Ret>;
209
210 fn filter(self, predicate: Predicate) -> Self::Output {
211 DeleteStatement {
212 from_clause: self.from_clause,
213 where_clause: self.where_clause.and(predicate),
214 returning: self.returning,
215 }
216 }
217 }
218
219 impl<T, U, Ret, Predicate> OrFilterDsl<Predicate> for DeleteStatement<T, U, Ret>
220 where
221 T: QuerySource,
222 U: WhereOr<Predicate>,
223 Predicate: AppearsOnTable<T>,
224 {
225 type Output = DeleteStatement<T, U::Output, Ret>;
226
227 fn or_filter(self, predicate: Predicate) -> Self::Output {
228 DeleteStatement {
229 from_clause: self.from_clause,
230 where_clause: self.where_clause.or(predicate),
231 returning: self.returning,
232 }
233 }
234 }
235
236 impl<'a, T, U, Ret, DB> BoxedDsl<'a, DB> for DeleteStatement<T, U, Ret>
237 where
238 U: Into<BoxedWhereClause<'a, DB>>,
239 T: QuerySource,
240 {
241 type Output = BoxedDeleteStatement<'a, DB, T, Ret>;
242
243 fn internal_into_boxed(self) -> Self::Output {
244 DeleteStatement {
245 where_clause: self.where_clause.into(),
246 returning: self.returning,
247 from_clause: self.from_clause,
248 }
249 }
250 }
251
252 impl<T, U, Ret, DB> QueryFragment<DB> for DeleteStatement<T, U, Ret>
253 where
254 DB: Backend + DieselReserveSpecialization,
255 T: Table,
256 FromClause<T>: QueryFragment<DB>,
257 U: QueryFragment<DB>,
258 Ret: QueryFragment<DB>,
259 {
260 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
261 out.push_sql("DELETE");
262 self.from_clause.walk_ast(out.reborrow())?;
263 self.where_clause.walk_ast(out.reborrow())?;
264 self.returning.walk_ast(out.reborrow())?;
265 Ok(())
266 }
267 }
268
269 impl<T, U> AsQuery for DeleteStatement<T, U, NoReturningClause>
270 where
271 T: Table,
272 T::AllColumns: SelectableExpression<T>,
273 DeleteStatement<T, U, ReturningClause<T::AllColumns>>: Query,
274 {
275 type SqlType = <Self::Query as Query>::SqlType;
276 type Query = DeleteStatement<T, U, ReturningClause<T::AllColumns>>;
277
278 fn as_query(self) -> Self::Query {
279 self.returning(T::all_columns())
280 }
281 }
282
283 impl<T, U, Ret> Query for DeleteStatement<T, U, ReturningClause<Ret>>
284 where
285 T: Table,
286 Ret: SelectableExpression<T>,
287 {
288 type SqlType = Ret::SqlType;
289 }
290
291 impl<T, U, Ret, Conn> RunQueryDsl<Conn> for DeleteStatement<T, U, Ret> where T: QuerySource {}
292
293 impl<T: QuerySource, U> DeleteStatement<T, U, NoReturningClause> {
294 /// Specify what expression is returned after execution of the `delete`.
295 ///
296 /// # Examples
297 ///
298 /// ### Deleting a record:
299 ///
300 /// ```rust
301 /// # include!("../../doctest_setup.rs");
302 /// #
303 /// # #[cfg(feature = "postgres")]
304 /// # fn main() {
305 /// # use schema::users::dsl::*;
306 /// # let connection = &mut establish_connection();
307 /// let deleted_name = diesel::delete(users.filter(name.eq("Sean")))
308 /// .returning(name)
309 /// .get_result(connection);
310 /// assert_eq!(Ok("Sean".to_string()), deleted_name);
311 /// # }
312 /// # #[cfg(not(feature = "postgres"))]
313 /// # fn main() {}
314 /// ```
315 pub fn returning<E>(self, returns: E) -> DeleteStatement<T, U, ReturningClause<E>>
316 where
317 E: SelectableExpression<T>,
318 DeleteStatement<T, U, ReturningClause<E>>: Query,
319 {
320 DeleteStatement {
321 where_clause: self.where_clause,
322 from_clause: self.from_clause,
323 returning: ReturningClause(returns),
324 }
325 }
326 }