icydb_core/db/query/fluent/load/
builder.rs1use crate::{
7 db::{
8 DbSession,
9 predicate::{CompareOp, Predicate},
10 query::{
11 builder::aggregate::AggregateExpr,
12 explain::ExplainPlan,
13 expr::{FilterExpr, SortExpr},
14 intent::{CompiledQuery, PlannedQuery, Query, QueryError},
15 trace::QueryTracePlan,
16 },
17 },
18 traits::{EntityKind, SingletonEntity},
19 types::Id,
20 value::Value,
21};
22
23pub struct FluentLoadQuery<'a, E>
32where
33 E: EntityKind,
34{
35 pub(super) session: &'a DbSession<E::Canister>,
36 pub(super) query: Query<E>,
37 pub(super) cursor_token: Option<String>,
38}
39
40impl<'a, E> FluentLoadQuery<'a, E>
41where
42 E: EntityKind,
43{
44 pub(crate) const fn new(session: &'a DbSession<E::Canister>, query: Query<E>) -> Self {
45 Self {
46 session,
47 query,
48 cursor_token: None,
49 }
50 }
51
52 #[must_use]
58 pub const fn query(&self) -> &Query<E> {
59 &self.query
60 }
61
62 pub(super) fn map_query(mut self, map: impl FnOnce(Query<E>) -> Query<E>) -> Self {
63 self.query = map(self.query);
64 self
65 }
66
67 pub(super) fn try_map_query(
68 mut self,
69 map: impl FnOnce(Query<E>) -> Result<Query<E>, QueryError>,
70 ) -> Result<Self, QueryError> {
71 self.query = map(self.query)?;
72 Ok(self)
73 }
74
75 fn map_session_query_output<T>(
79 &self,
80 map: impl FnOnce(&DbSession<E::Canister>, &Query<E>) -> Result<T, QueryError>,
81 ) -> Result<T, QueryError> {
82 map(self.session, self.query())
83 }
84
85 fn map_cursor_ready_query_output<T>(
89 &self,
90 map: impl FnOnce(&DbSession<E::Canister>, &Query<E>) -> Result<T, QueryError>,
91 ) -> Result<T, QueryError> {
92 self.ensure_cursor_mode_ready()?;
93
94 self.map_session_query_output(map)
95 }
96
97 #[must_use]
105 pub fn by_id(self, id: Id<E>) -> Self {
106 self.map_query(|query| query.by_id(id.key()))
107 }
108
109 #[must_use]
113 pub fn by_ids<I>(self, ids: I) -> Self
114 where
115 I: IntoIterator<Item = Id<E>>,
116 {
117 self.map_query(|query| query.by_ids(ids.into_iter().map(|id| id.key())))
118 }
119
120 #[must_use]
126 pub fn filter(self, predicate: Predicate) -> Self {
127 self.map_query(|query| query.filter(predicate))
128 }
129
130 pub fn filter_expr(self, expr: FilterExpr) -> Result<Self, QueryError> {
132 self.try_map_query(|query| query.filter_expr(expr))
133 }
134
135 pub fn sort_expr(self, expr: SortExpr) -> Result<Self, QueryError> {
137 self.try_map_query(|query| query.sort_expr(expr))
138 }
139
140 #[must_use]
142 pub fn order_by(self, field: impl AsRef<str>) -> Self {
143 self.map_query(|query| query.order_by(field))
144 }
145
146 #[must_use]
148 pub fn order_by_desc(self, field: impl AsRef<str>) -> Self {
149 self.map_query(|query| query.order_by_desc(field))
150 }
151
152 pub fn group_by(self, field: impl AsRef<str>) -> Result<Self, QueryError> {
154 let field = field.as_ref().to_owned();
155 self.try_map_query(|query| query.group_by(&field))
156 }
157
158 #[must_use]
160 pub fn aggregate(self, aggregate: AggregateExpr) -> Self {
161 self.map_query(|query| query.aggregate(aggregate))
162 }
163
164 #[must_use]
166 pub fn grouped_limits(self, max_groups: u64, max_group_bytes: u64) -> Self {
167 self.map_query(|query| query.grouped_limits(max_groups, max_group_bytes))
168 }
169
170 pub fn having_group(
172 self,
173 field: impl AsRef<str>,
174 op: CompareOp,
175 value: Value,
176 ) -> Result<Self, QueryError> {
177 let field = field.as_ref().to_owned();
178 self.try_map_query(|query| query.having_group(&field, op, value))
179 }
180
181 pub fn having_aggregate(
183 self,
184 aggregate_index: usize,
185 op: CompareOp,
186 value: Value,
187 ) -> Result<Self, QueryError> {
188 self.try_map_query(|query| query.having_aggregate(aggregate_index, op, value))
189 }
190
191 #[must_use]
197 pub fn limit(self, limit: u32) -> Self {
198 self.map_query(|query| query.limit(limit))
199 }
200
201 #[must_use]
207 pub fn offset(self, offset: u32) -> Self {
208 self.map_query(|query| query.offset(offset))
209 }
210
211 #[must_use]
217 pub fn cursor(mut self, token: impl Into<String>) -> Self {
218 self.cursor_token = Some(token.into());
219 self
220 }
221
222 pub fn explain(&self) -> Result<ExplainPlan, QueryError> {
228 self.map_session_query_output(DbSession::explain_query_with_visible_indexes)
229 }
230
231 pub fn plan_hash_hex(&self) -> Result<String, QueryError> {
233 self.map_session_query_output(DbSession::query_plan_hash_hex_with_visible_indexes)
234 }
235
236 pub fn trace(&self) -> Result<QueryTracePlan, QueryError> {
238 self.map_session_query_output(DbSession::trace_query)
239 }
240
241 pub fn planned(&self) -> Result<PlannedQuery<E>, QueryError> {
243 self.map_cursor_ready_query_output(DbSession::planned_query_with_visible_indexes)
244 }
245
246 pub fn plan(&self) -> Result<CompiledQuery<E>, QueryError> {
248 self.map_cursor_ready_query_output(DbSession::compile_query_with_visible_indexes)
249 }
250}
251
252impl<E> FluentLoadQuery<'_, E>
253where
254 E: EntityKind + SingletonEntity,
255 E::Key: Default,
256{
257 #[must_use]
259 pub fn only(self) -> Self {
260 self.map_query(Query::only)
261 }
262}