icydb_core/db/query/fluent/
delete.rs1use crate::{
7 db::{
8 DbSession, EntityResponse, PersistedRow,
9 predicate::Predicate,
10 query::{
11 explain::ExplainPlan,
12 expr::{FilterExpr, SortExpr},
13 intent::{CompiledQuery, PlannedQuery, Query, QueryError},
14 trace::QueryTracePlan,
15 },
16 response::ResponseError,
17 },
18 traits::{EntityKind, EntityValue, SingletonEntity},
19 types::Id,
20};
21
22pub struct FluentDeleteQuery<'a, E>
32where
33 E: EntityKind,
34{
35 session: &'a DbSession<E::Canister>,
36 query: Query<E>,
37}
38
39impl<'a, E> FluentDeleteQuery<'a, E>
40where
41 E: PersistedRow,
42{
43 pub(crate) const fn new(session: &'a DbSession<E::Canister>, query: Query<E>) -> Self {
44 Self { session, query }
45 }
46
47 #[must_use]
53 pub const fn query(&self) -> &Query<E> {
54 &self.query
55 }
56
57 fn map_query(mut self, map: impl FnOnce(Query<E>) -> Query<E>) -> Self {
58 self.query = map(self.query);
59 self
60 }
61
62 fn try_map_query(
63 mut self,
64 map: impl FnOnce(Query<E>) -> Result<Query<E>, QueryError>,
65 ) -> Result<Self, QueryError> {
66 self.query = map(self.query)?;
67 Ok(self)
68 }
69
70 fn map_session_query_output<T>(
74 &self,
75 map: impl FnOnce(&DbSession<E::Canister>, &Query<E>) -> Result<T, QueryError>,
76 ) -> Result<T, QueryError> {
77 map(self.session, self.query())
78 }
79
80 #[must_use]
88 pub fn by_id(self, id: Id<E>) -> Self {
89 self.map_query(|query| query.by_id(id.key()))
90 }
91
92 #[must_use]
96 pub fn by_ids<I>(self, ids: I) -> Self
97 where
98 I: IntoIterator<Item = Id<E>>,
99 {
100 self.map_query(|query| query.by_ids(ids.into_iter().map(|id| id.key())))
101 }
102
103 #[must_use]
109 pub fn filter(self, predicate: Predicate) -> Self {
110 self.map_query(|query| query.filter(predicate))
111 }
112
113 pub fn filter_expr(self, expr: FilterExpr) -> Result<Self, QueryError> {
115 self.try_map_query(|query| query.filter_expr(expr))
116 }
117
118 pub fn sort_expr(self, expr: SortExpr) -> Result<Self, QueryError> {
120 self.try_map_query(|query| query.sort_expr(expr))
121 }
122
123 #[must_use]
125 pub fn order_by(self, field: impl AsRef<str>) -> Self {
126 self.map_query(|query| query.order_by(field))
127 }
128
129 #[must_use]
131 pub fn order_by_desc(self, field: impl AsRef<str>) -> Self {
132 self.map_query(|query| query.order_by_desc(field))
133 }
134
135 #[must_use]
137 pub fn limit(self, limit: u32) -> Self {
138 self.map_query(|query| query.limit(limit))
139 }
140
141 pub fn explain(&self) -> Result<ExplainPlan, QueryError> {
147 self.map_session_query_output(DbSession::explain_query_with_visible_indexes)
148 }
149
150 pub fn plan_hash_hex(&self) -> Result<String, QueryError> {
152 self.map_session_query_output(DbSession::query_plan_hash_hex_with_visible_indexes)
153 }
154
155 pub fn trace(&self) -> Result<QueryTracePlan, QueryError> {
157 self.map_session_query_output(DbSession::trace_query)
158 }
159
160 pub fn planned(&self) -> Result<PlannedQuery<E>, QueryError> {
162 self.map_session_query_output(DbSession::planned_query_with_visible_indexes)
163 }
164
165 pub fn plan(&self) -> Result<CompiledQuery<E>, QueryError> {
167 self.map_session_query_output(DbSession::compile_query_with_visible_indexes)
168 }
169
170 pub fn execute(&self) -> Result<u32, QueryError>
176 where
177 E: EntityValue,
178 {
179 self.session.execute_delete_count(self.query())
180 }
181
182 pub fn execute_rows(&self) -> Result<EntityResponse<E>, QueryError>
185 where
186 E: EntityValue,
187 {
188 self.session.execute_query(self.query())
189 }
190
191 pub fn is_empty(&self) -> Result<bool, QueryError>
193 where
194 E: EntityValue,
195 {
196 Ok(self.execute()? == 0)
197 }
198
199 pub fn count(&self) -> Result<u32, QueryError>
201 where
202 E: EntityValue,
203 {
204 self.execute()
205 }
206
207 pub fn require_one(&self) -> Result<(), QueryError>
209 where
210 E: EntityValue,
211 {
212 match self.execute()? {
213 1 => Ok(()),
214 0 => Err(ResponseError::not_found(E::PATH).into()),
215 count => Err(ResponseError::not_unique(E::PATH, count).into()),
216 }
217 }
218
219 pub fn require_some(&self) -> Result<(), QueryError>
221 where
222 E: EntityValue,
223 {
224 if self.execute()? == 0 {
225 return Err(ResponseError::not_found(E::PATH).into());
226 }
227
228 Ok(())
229 }
230}
231
232impl<E> FluentDeleteQuery<'_, E>
233where
234 E: PersistedRow + SingletonEntity,
235 E::Key: Default,
236{
237 #[must_use]
239 pub fn only(self) -> Self {
240 self.map_query(Query::only)
241 }
242}