Skip to main content

icydb_core/db/query/session/
load.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    traits::{CanisterKind, EntityKind, EntityValue, SingletonEntity},
13    types::Id,
14};
15
16///
17/// SessionLoadQuery
18///
19/// Session-bound load query wrapper.
20/// Owns intent construction and execution routing only.
21/// All result inspection and projection is performed on `Response<E>`.
22///
23
24pub struct SessionLoadQuery<'a, C, E>
25where
26    C: CanisterKind,
27    E: EntityKind<Canister = C>,
28{
29    session: &'a DbSession<C>,
30    query: Query<E>,
31}
32
33impl<'a, C, E> SessionLoadQuery<'a, C, E>
34where
35    C: CanisterKind,
36    E: EntityKind<Canister = C>,
37{
38    pub(crate) const fn new(session: &'a DbSession<C>, query: Query<E>) -> Self {
39        Self { session, query }
40    }
41
42    // ------------------------------------------------------------------
43    // Intent inspection
44    // ------------------------------------------------------------------
45
46    #[must_use]
47    pub const fn query(&self) -> &Query<E> {
48        &self.query
49    }
50
51    // ------------------------------------------------------------------
52    // Intent builders (pure)
53    // ------------------------------------------------------------------
54
55    /// Set the access path to a single entity identity.
56    #[must_use]
57    pub fn by_id(mut self, id: Id<E>) -> Self {
58        self.query = self.query.by_id(id.into_key());
59        self
60    }
61
62    /// Set the access path to multiple entity identities.
63    #[must_use]
64    pub fn by_ids<I>(mut self, ids: I) -> Self
65    where
66        I: IntoIterator<Item = Id<E>>,
67    {
68        self.query = self.query.by_ids(ids.into_iter().map(Id::into_key));
69        self
70    }
71
72    // ------------------------------------------------------------------
73    // Query Refinement
74    // ------------------------------------------------------------------
75
76    #[must_use]
77    pub fn filter(mut self, predicate: Predicate) -> Self {
78        self.query = self.query.filter(predicate);
79        self
80    }
81
82    pub fn filter_expr(mut self, expr: FilterExpr) -> Result<Self, QueryError> {
83        self.query = self.query.filter_expr(expr)?;
84        Ok(self)
85    }
86
87    pub fn sort_expr(mut self, expr: SortExpr) -> Result<Self, QueryError> {
88        self.query = self.query.sort_expr(expr)?;
89        Ok(self)
90    }
91
92    #[must_use]
93    pub fn order_by(mut self, field: impl AsRef<str>) -> Self {
94        self.query = self.query.order_by(field);
95        self
96    }
97
98    #[must_use]
99    pub fn order_by_desc(mut self, field: impl AsRef<str>) -> Self {
100        self.query = self.query.order_by_desc(field);
101        self
102    }
103
104    #[must_use]
105    pub fn limit(mut self, limit: u32) -> Self {
106        self.query = self.query.limit(limit);
107        self
108    }
109
110    #[must_use]
111    pub fn offset(mut self, offset: u32) -> Self {
112        self.query = self.query.offset(offset);
113        self
114    }
115
116    // ------------------------------------------------------------------
117    // Planning / diagnostics
118    // ------------------------------------------------------------------
119
120    pub fn explain(&self) -> Result<ExplainPlan, QueryError> {
121        self.query.explain()
122    }
123
124    pub fn plan(&self) -> Result<ExecutablePlan<E>, QueryError> {
125        self.query.plan()
126    }
127
128    // ------------------------------------------------------------------
129    // Execution (single semantic boundary)
130    // ------------------------------------------------------------------
131
132    /// Execute this query using the session's policy settings.
133    pub fn execute(&self) -> Result<Response<E>, QueryError>
134    where
135        E: EntityValue,
136    {
137        self.session.execute_query(self.query())
138    }
139
140    // ------------------------------------------------------------------
141    // Execution terminals — semantic only
142    // ------------------------------------------------------------------
143
144    /// Execute and return whether the result set is empty.
145    pub fn is_empty(&self) -> Result<bool, QueryError>
146    where
147        E: EntityValue,
148    {
149        Ok(self.execute()?.is_empty())
150    }
151
152    /// Execute and return the number of matching rows.
153    pub fn count(&self) -> Result<u32, QueryError>
154    where
155        E: EntityValue,
156    {
157        Ok(self.execute()?.count())
158    }
159
160    /// Execute and require exactly one matching row.
161    pub fn require_one(&self) -> Result<(), QueryError>
162    where
163        E: EntityValue,
164    {
165        self.execute()?.require_one().map_err(QueryError::Response)
166    }
167
168    /// Execute and require at least one matching row.
169    pub fn require_some(&self) -> Result<(), QueryError>
170    where
171        E: EntityValue,
172    {
173        self.execute()?.require_some().map_err(QueryError::Response)
174    }
175}
176
177impl<C, E> SessionLoadQuery<'_, C, E>
178where
179    C: CanisterKind,
180    E: EntityKind<Canister = C> + SingletonEntity,
181    E::Key: Default,
182{
183    #[must_use]
184    pub fn only(mut self) -> Self {
185        self.query = self.query.only();
186        self
187    }
188}