Skip to main content

icydb_core/db/query/fluent/
delete.rs

1//! Module: query::fluent::delete
2//! Responsibility: fluent delete-query builder and execution routing.
3//! Does not own: query semantic validation or response projection.
4//! Boundary: session API facade over query intent/planning/execution.
5
6use 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
22///
23/// FluentDeleteQuery
24///
25/// Session-bound delete query wrapper.
26/// This type owns *intent construction* and *execution routing only*.
27/// Delete execution follows the same traditional mutation contract as the
28/// unified SQL write lane: bare execution returns affected-row count.
29///
30
31pub 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    // ------------------------------------------------------------------
48    // Intent inspection
49    // ------------------------------------------------------------------
50
51    /// Borrow the current immutable query intent.
52    #[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    // Run one read-only session/query projection without mutating the delete
71    // builder shell so diagnostics and planning surfaces share one handoff
72    // shape from the fluent delete boundary into the session/query layer.
73    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    // ------------------------------------------------------------------
81    // Intent builders (pure)
82    // ------------------------------------------------------------------
83
84    /// Set the access path to a single typed primary-key value.
85    ///
86    /// `Id<E>` is treated as a plain query input value here. It does not grant access.
87    #[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    /// Set the access path to multiple typed primary-key values.
93    ///
94    /// IDs are public and may come from untrusted input sources.
95    #[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    // ------------------------------------------------------------------
104    // Query Refinement
105    // ------------------------------------------------------------------
106
107    /// Add a typed predicate expression directly.
108    #[must_use]
109    pub fn filter(self, predicate: Predicate) -> Self {
110        self.map_query(|query| query.filter(predicate))
111    }
112
113    /// Add a serialized filter expression after lowering and validation.
114    pub fn filter_expr(self, expr: FilterExpr) -> Result<Self, QueryError> {
115        self.try_map_query(|query| query.filter_expr(expr))
116    }
117
118    /// Add sort clauses from a serialized sort expression.
119    pub fn sort_expr(self, expr: SortExpr) -> Result<Self, QueryError> {
120        self.try_map_query(|query| query.sort_expr(expr))
121    }
122
123    /// Append ascending order for one field.
124    #[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    /// Append descending order for one field.
130    #[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    /// Bound the number of rows affected by this delete.
136    #[must_use]
137    pub fn limit(self, limit: u32) -> Self {
138        self.map_query(|query| query.limit(limit))
139    }
140
141    // ------------------------------------------------------------------
142    // Planning / diagnostics
143    // ------------------------------------------------------------------
144
145    /// Build explain metadata for the current query.
146    pub fn explain(&self) -> Result<ExplainPlan, QueryError> {
147        self.map_session_query_output(DbSession::explain_query_with_visible_indexes)
148    }
149
150    /// Return the stable plan hash for this query.
151    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    /// Build one trace payload without executing the query.
156    pub fn trace(&self) -> Result<QueryTracePlan, QueryError> {
157        self.map_session_query_output(DbSession::trace_query)
158    }
159
160    /// Build the validated logical plan without compiling execution details.
161    pub fn planned(&self) -> Result<PlannedQuery<E>, QueryError> {
162        self.map_session_query_output(DbSession::planned_query_with_visible_indexes)
163    }
164
165    /// Build the compiled executable plan for this query.
166    pub fn plan(&self) -> Result<CompiledQuery<E>, QueryError> {
167        self.map_session_query_output(DbSession::compile_query_with_visible_indexes)
168    }
169
170    // ------------------------------------------------------------------
171    // Execution (minimal core surface)
172    // ------------------------------------------------------------------
173
174    /// Execute this delete and return the affected-row count.
175    pub fn execute(&self) -> Result<u32, QueryError>
176    where
177        E: EntityValue,
178    {
179        self.session.execute_delete_count(self.query())
180    }
181
182    /// Execute this delete and materialize deleted rows for one explicit
183    /// row-returning surface.
184    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    /// Execute and return whether any rows were affected.
192    pub fn is_empty(&self) -> Result<bool, QueryError>
193    where
194        E: EntityValue,
195    {
196        Ok(self.execute()? == 0)
197    }
198
199    /// Execute and return the number of affected rows.
200    pub fn count(&self) -> Result<u32, QueryError>
201    where
202        E: EntityValue,
203    {
204        self.execute()
205    }
206
207    /// Execute and require exactly one affected row.
208    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    /// Execute and require at least one affected row.
220    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    /// Delete the singleton entity.
238    #[must_use]
239    pub fn only(self) -> Self {
240        self.map_query(Query::only)
241    }
242}