Skip to main content

icydb_core/db/query/session/
delete.rs

1use crate::{
2    db::{
3        DbSession,
4        query::{
5            Query, QueryError,
6            expr::{FilterExpr, SortExpr},
7            plan::{ExecutablePlan, ExplainPlan},
8            predicate::Predicate,
9        },
10        response::Response,
11    },
12    key::Key,
13    traits::{CanisterKind, EntityKind, UnitKey},
14};
15
16///
17/// SessionDeleteQuery
18///
19/// Session-bound delete query wrapper.
20/// This type owns *intent construction* and *execution routing only*.
21/// All result projection and cardinality handling lives on `Response<E>`.
22///
23pub struct SessionDeleteQuery<'a, C: CanisterKind, E: EntityKind<Canister = C>> {
24    session: &'a DbSession<C>,
25    query: Query<E>,
26}
27
28impl<'a, C: CanisterKind, E: EntityKind<Canister = C>> SessionDeleteQuery<'a, C, E> {
29    pub(crate) const fn new(session: &'a DbSession<C>, query: Query<E>) -> Self {
30        Self { session, query }
31    }
32
33    // ------------------------------------------------------------------
34    // Intent inspection
35    // ------------------------------------------------------------------
36
37    #[must_use]
38    pub const fn query(&self) -> &Query<E> {
39        &self.query
40    }
41
42    // ------------------------------------------------------------------
43    // Intent builders
44    // ------------------------------------------------------------------
45
46    #[must_use]
47    pub fn by_key(mut self, key: impl Into<Key>) -> Self {
48        self.query = self.query.by_key(key.into());
49        self
50    }
51
52    #[must_use]
53    pub fn many<I>(mut self, keys: I) -> Self
54    where
55        I: IntoIterator<Item = E::PrimaryKey>,
56    {
57        self.query = self.query.by_keys(keys.into_iter().map(Into::into));
58        self
59    }
60
61    #[must_use]
62    pub fn filter(mut self, predicate: Predicate) -> Self {
63        self.query = self.query.filter(predicate);
64        self
65    }
66
67    pub fn filter_expr(mut self, expr: FilterExpr) -> Result<Self, QueryError> {
68        self.query = self.query.filter_expr(expr)?;
69        Ok(self)
70    }
71
72    pub fn sort_expr(mut self, expr: SortExpr) -> Result<Self, QueryError> {
73        self.query = self.query.sort_expr(expr)?;
74        Ok(self)
75    }
76
77    #[must_use]
78    pub fn order_by(mut self, field: impl AsRef<str>) -> Self {
79        self.query = self.query.order_by(field);
80        self
81    }
82
83    #[must_use]
84    pub fn order_by_desc(mut self, field: impl AsRef<str>) -> Self {
85        self.query = self.query.order_by_desc(field);
86        self
87    }
88
89    #[must_use]
90    pub fn limit(mut self, limit: u32) -> Self {
91        self.query = self.query.limit(limit);
92        self
93    }
94
95    // ------------------------------------------------------------------
96    // Planning / diagnostics
97    // ------------------------------------------------------------------
98
99    pub fn explain(&self) -> Result<ExplainPlan, QueryError> {
100        self.query.explain()
101    }
102
103    pub fn plan(&self) -> Result<ExecutablePlan<E>, QueryError> {
104        self.query.plan()
105    }
106
107    // ------------------------------------------------------------------
108    // Execution (minimal core surface)
109    // ------------------------------------------------------------------
110
111    /// Execute this delete using the session's policy settings.
112    ///
113    /// All result inspection and projection is performed on `Response<E>`.
114    pub fn execute(&self) -> Result<Response<E>, QueryError> {
115        self.session.execute_query(self.query())
116    }
117
118    /// Execute and return whether any rows were affected.
119    pub fn is_empty(&self) -> Result<bool, QueryError> {
120        Ok(self.execute()?.is_empty())
121    }
122
123    /// Execute and return the number of affected rows.
124    pub fn count(&self) -> Result<u32, QueryError> {
125        Ok(self.execute()?.count())
126    }
127
128    /// Execute and require exactly one affected row.
129    pub fn require_one(&self) -> Result<(), QueryError> {
130        self.execute()?.require_one().map_err(QueryError::Response)
131    }
132
133    /// Execute and require at least one affected row.
134    pub fn require_some(&self) -> Result<(), QueryError> {
135        self.execute()?.require_some().map_err(QueryError::Response)
136    }
137}
138
139impl<C: CanisterKind, E: EntityKind<Canister = C>> SessionDeleteQuery<'_, C, E>
140where
141    E::PrimaryKey: UnitKey,
142{
143    /// Delete the singleton entity identified by the unit primary key `()`.
144    #[must_use]
145    pub fn only(mut self) -> Self {
146        self.query = self.query.only();
147        self
148    }
149}