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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::marker::PhantomData;
use crate::{connection::{RawQuery, Queryable, Executable}, IntoSqlite};
use super::{Model, query::{ModelQueryFilter, ColumnQueryOrder}};
/// A query that deletes rows from a table.
pub struct ModelDeleteQuery<M: Model> {
query: RawQuery,
model: PhantomData<M>,
}
impl<M: Model> Default for ModelDeleteQuery<M> {
fn default() -> Self {
Self::new()
}
}
impl<M: Model> ModelDeleteQuery<M> {
pub fn new() -> Self {
ModelDeleteQuery {
query: RawQuery::new(format!("DELETE FROM {}", M::table_name()), Vec::new()),
model: PhantomData,
}
}
pub fn combine(self, other: RawQuery) -> Self {
let mut params_old = self.query.params;
params_old.extend(other.params);
ModelDeleteQuery {
query: RawQuery::new(format!("{} {}", self.query.sql, other.sql), params_old),
model: PhantomData,
}
}
/// Filter the rows removed by the query.
///
/// ## Arguments
/// * `filter` - The filter to apply to the query.
///
/// ## Returns
/// A new query with the filter applied.
///
/// ## Example
/// ```rs
/// User::delete().filter(User::id.eq(1)).exec(&conn).unwrap();
/// ```
pub fn filter(self, mut filter: impl ModelQueryFilter) -> Self {
let mut filter_query = filter.get_query();
filter_query.sql = format!("WHERE {}", filter_query.sql);
ModelDeleteQuery::combine(self, filter_query)
}
// Limit and offset
/// Limit the number of rows returned by the query.
/// WARNING: This requires SQLITE_ENABLE_UPDATE_DELETE_LIMIT to be enabled in the sqlite3 library.
///
/// ## Arguments
/// * `limit` - The maximum number of rows to return.
///
/// ## Returns
/// A new query with the limit applied.
///
/// ## Example
/// ```rs
/// User::delete().limit(1).exec(&conn).unwrap();
/// ```
pub fn limit(self, limit: u32) -> Self {
self.combine(RawQuery::new("LIMIT ?".to_string(), vec![Box::new(limit)]))
}
/// Offset the number of rows returned by the query.
/// WARNING: This requires SQLITE_ENABLE_UPDATE_DELETE_LIMIT to be enabled in the sqlite3 library.
///
/// ## Arguments
/// * `offset` - The number of rows to skip.
///
/// ## Returns
/// A new query with the offset applied.
///
/// ## Example
/// ```rs
/// User::delete().offset(1).exec(&conn).unwrap();
/// ```
pub fn offset(self, offset: u32) -> Self {
self.combine(RawQuery::new("OFFSET ?".to_string(), vec![Box::new(offset)]))
}
// Order
/// Order the rows returned by the query.
/// WARNING: This requires SQLITE_ENABLE_UPDATE_DELETE_LIMIT to be enabled in the sqlite3 library.
///
/// ## Arguments
/// * `order` - The order to apply to the query.
///
/// ## Returns
/// A new query with the order applied.
///
/// ## Example
/// ```rs
/// User::delete().order_by(User::id.asc()).exec(&conn).unwrap();
pub fn order_by(self, order: ColumnQueryOrder) -> Self {
self.combine(RawQuery::new(format!("ORDER BY {}", order.into_sqlite()), Vec::new()))
}
}
impl<M: Model> Queryable<()> for ModelDeleteQuery<M> {
fn get_query(&mut self) -> RawQuery {
self.query.move_clone()
}
fn parse_result(&mut self, _rows: rusqlite::Rows) {
// Nothing to parse
}
fn should_execute(&self) -> bool {
true
}
}
impl<M: Model> Executable<usize> for ModelDeleteQuery<M> {
fn exec(self, conn: &crate::prelude::Connection) -> Result<usize, rusqlite::Error> {
conn.exec(self)
}
}