icydb_core/db/query/
dynamic.rs1mod cleanup;
7
8use crate::db::query::{
9 builder::AggregateExpr,
10 expr::{FilterExpr, JunctionOperator, OrderTerm},
11};
12
13#[derive(Clone, Debug)]
26pub struct DynamicQuery {
27 entity: String,
28 filter: Option<FilterExpr>,
29 order: Vec<OrderTerm>,
30 fields: Vec<String>,
31 #[cfg(test)]
32 distinct: bool,
33 limit: Option<u32>,
34 group_fields: Vec<String>,
35 aggregates: Vec<AggregateExpr>,
36 grouped_limits: Option<(u32, u32)>,
37 cursor: Option<String>,
38}
39
40impl DynamicQuery {
41 #[must_use]
43 pub fn new(entity: impl Into<String>) -> Self {
44 Self {
45 entity: entity.into(),
46 filter: None,
47 order: Vec::new(),
48 fields: Vec::new(),
49 #[cfg(test)]
50 distinct: false,
51 limit: None,
52 group_fields: Vec::new(),
53 aggregates: Vec::new(),
54 grouped_limits: None,
55 cursor: None,
56 }
57 }
58
59 #[must_use]
61 pub fn filter(mut self, filter: impl Into<FilterExpr>) -> Self {
62 let appended = filter.into();
63 self.filter = Some(match self.filter.take() {
64 Some(FilterExpr::Junction {
65 operator: JunctionOperator::And,
66 mut filters,
67 }) => {
68 filters.push(appended);
69 FilterExpr::and(filters)
70 }
71 Some(existing) => FilterExpr::and(vec![existing, appended]),
72 None => appended,
73 });
74 self
75 }
76
77 #[must_use]
79 pub fn order_by(mut self, order: OrderTerm) -> Self {
80 self.order.push(order);
81 self
82 }
83
84 #[must_use]
89 pub fn select<I, S>(mut self, fields: I) -> Self
90 where
91 I: IntoIterator<Item = S>,
92 S: Into<String>,
93 {
94 self.fields = fields.into_iter().map(Into::into).collect();
95 self
96 }
97
98 #[must_use]
106 pub const fn limit(mut self, limit: u32) -> Self {
107 self.limit = Some(limit);
108 self
109 }
110
111 #[cfg(test)]
117 #[must_use]
118 pub(in crate::db) const fn distinct_for_internal_execution(mut self) -> Self {
119 self.distinct = true;
120 self
121 }
122
123 #[must_use]
125 pub fn group_by(mut self, field: impl Into<String>) -> Self {
126 self.group_fields.push(field.into());
127 self
128 }
129
130 #[must_use]
132 pub fn aggregate(mut self, aggregate: AggregateExpr) -> Self {
133 self.aggregates.push(aggregate);
134 self
135 }
136
137 #[must_use]
142 pub const fn grouped_limits(mut self, max_groups: u32, max_group_bytes: u32) -> Self {
143 self.grouped_limits = Some((max_groups, max_group_bytes));
144 self
145 }
146
147 #[must_use]
149 pub fn cursor(mut self, cursor: impl Into<String>) -> Self {
150 self.cursor = Some(cursor.into());
151 self
152 }
153
154 pub(in crate::db) const fn entity(&self) -> &str {
155 self.entity.as_str()
156 }
157
158 pub(in crate::db) const fn filter_expr(&self) -> Option<&FilterExpr> {
159 self.filter.as_ref()
160 }
161
162 pub(in crate::db) const fn order_terms(&self) -> &[OrderTerm] {
163 self.order.as_slice()
164 }
165
166 pub(in crate::db) const fn selected_fields(&self) -> &[String] {
167 self.fields.as_slice()
168 }
169
170 pub(in crate::db) const fn row_limit(&self) -> Option<u32> {
171 self.limit
172 }
173
174 #[cfg(test)]
175 pub(in crate::db) const fn projection_is_distinct(&self) -> bool {
176 self.distinct
177 }
178
179 pub(in crate::db) const fn has_grouping(&self) -> bool {
180 !self.group_fields.is_empty() || !self.aggregates.is_empty()
181 }
182
183 pub(in crate::db) const fn group_fields(&self) -> &[String] {
184 self.group_fields.as_slice()
185 }
186
187 pub(in crate::db) const fn aggregates(&self) -> &[AggregateExpr] {
188 self.aggregates.as_slice()
189 }
190
191 pub(in crate::db) const fn grouped_execution_limits(&self) -> Option<(u32, u32)> {
192 self.grouped_limits
193 }
194
195 pub(in crate::db) fn continuation_cursor(&self) -> Option<&str> {
196 self.cursor.as_deref()
197 }
198}
199
200impl Drop for DynamicQuery {
201 fn drop(&mut self) {
202 cleanup::clear(self);
204 }
205}
206
207#[cfg(test)]
208mod tests {
209 use super::{DynamicQuery, FilterExpr};
210
211 #[test]
212 fn repeated_filters_accumulate_as_one_conjunction() {
213 let owner = FilterExpr::eq("owner_id", 1_u64);
214 let slot = FilterExpr::eq("slot", 2_u64);
215 let query = DynamicQuery::new("InventoryStack")
216 .filter(owner.clone())
217 .filter(slot.clone());
218
219 assert_eq!(
220 query.filter_expr(),
221 Some(&FilterExpr::and(vec![owner, slot]))
222 );
223 }
224}