zino_core/model/
query.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use super::QueryOrder;
use crate::{
    extension::{JsonObjectExt, JsonValueExt},
    validation::Validation,
    JsonValue, Map, SharedString,
};

/// A query type for models.
#[derive(Debug, Clone)]
pub struct Query {
    /// Projection fields.
    fields: Vec<String>,
    /// Filters.
    filters: Map,
    /// Sort order.
    sort_order: Vec<QueryOrder>,
    /// Offset.
    offset: usize,
    /// Limit.
    limit: usize,
    /// Extra flags.
    extra: Map,
}

impl Query {
    /// Creates a new instance.
    #[inline]
    pub fn new(filters: impl Into<JsonValue>) -> Self {
        let filters = filters.into().into_map_opt().unwrap_or_default();
        Self {
            fields: Vec::new(),
            filters,
            sort_order: Vec::new(),
            offset: 0,
            limit: 0,
            extra: Map::new(),
        }
    }

    /// Creates a new instance with the entry.
    #[inline]
    pub fn from_entry(key: impl Into<String>, value: impl Into<JsonValue>) -> Self {
        Self::new(Map::from_entry(key, value))
    }

    /// Updates the query using the json object and returns the validation result.
    #[must_use]
    pub fn read_map(&mut self, data: &Map) -> Validation {
        let mut validation = Validation::new();
        let mut pagination_current_page = None;
        let filters = &mut self.filters;
        let extra = &mut self.extra;
        for (key, value) in data.iter().filter(|(_, v)| !v.is_ignorable()) {
            match key.as_str() {
                "fields" | "columns" => {
                    if let Some(fields) = value.parse_str_array() {
                        self.fields.clear();
                        self.fields.extend(fields.into_iter().map(|s| s.to_owned()));
                    }
                }
                "order_by" | "sort_by" => {
                    if let Some(sort_order) = value.parse_str_array() {
                        self.sort_order.clear();
                        self.sort_order.extend(sort_order.into_iter().map(|s| {
                            if let Some(sort) = s.strip_suffix("|asc") {
                                QueryOrder::new(sort.to_owned(), false)
                            } else if let Some(sort) = s.strip_suffix("|desc") {
                                QueryOrder::new(sort.to_owned(), true)
                            } else {
                                QueryOrder::new(s.to_owned(), true)
                            }
                        }));
                    }
                }
                "offset" | "skip" => {
                    if let Some(result) = value.parse_usize() {
                        match result {
                            Ok(offset) => self.offset = offset,
                            Err(err) => validation.record_fail("offset", err),
                        }
                    }
                }
                "limit" | "page_size" => {
                    // Parses as `isize` so that it can accept `-1`
                    if let Some(result) = value.parse_isize() {
                        match result {
                            Ok(limit) => self.limit = usize::MIN.saturating_add_signed(limit),
                            Err(err) => validation.record_fail("limit", err),
                        }
                    }
                }
                "current_page" => {
                    if let Some(result) = value.parse_usize() {
                        match result {
                            Ok(current_page) => pagination_current_page = Some(current_page),
                            Err(err) => validation.record_fail("current_page", err),
                        }
                    }
                }
                "populate" | "translate" | "show_deleted" | "validate_only" | "no_check" => {
                    if let Some(result) = value.parse_bool() {
                        match result {
                            Ok(flag) => {
                                extra.upsert(key, flag);
                            }
                            Err(err) => validation.record_fail(key.to_owned(), err),
                        }
                    }
                }
                "timestamp" | "nonce" | "signature" => {
                    extra.upsert(key, value.clone());
                }
                _ => {
                    if let Some(value) = value.as_str().filter(|&s| s != "all") {
                        if key.starts_with('$') {
                            if let Some(expr) = value.strip_prefix('(') {
                                filters.upsert(key, Self::parse_logical_query(expr));
                            } else {
                                filters.upsert(key, value);
                            }
                        } else if value.starts_with('$') {
                            if let Some((operator, value)) = value.split_once('.') {
                                filters.upsert(key, Map::from_entry(operator, value));
                            } else {
                                filters.upsert(key, value);
                            }
                        } else {
                            filters.upsert(key, value);
                        }
                    } else {
                        filters.upsert(key, value.clone());
                    }
                }
            }
        }
        if let Some(current_page) = pagination_current_page {
            self.offset = self.limit * current_page.saturating_sub(1);
        }
        validation
    }

    /// Parses the query expression with logical operators.
    fn parse_logical_query(expr: &str) -> Vec<Map> {
        let mut filters = Vec::new();
        for expr in expr.trim_end_matches(')').split(',') {
            if let Some((key, expr)) = expr.split_once('.') {
                if let Some((operator, value)) = expr.split_once('.') {
                    let value = if value.starts_with('$') {
                        if let Some((operator, expr)) = value.split_once('(') {
                            Map::from_entry(operator, Self::parse_logical_query(expr)).into()
                        } else {
                            JsonValue::from(value)
                        }
                    } else {
                        JsonValue::from(value)
                    };
                    let filter = Map::from_entry(key, Map::from_entry(operator, value));
                    filters.push(filter);
                }
            }
        }
        filters
    }

    /// Sets the fields.
    #[inline]
    pub fn set_fields(&mut self, fields: Vec<String>) {
        self.fields = fields;
    }

    /// Retains the projection fields in the allow list.
    /// If the projection fields are empty, it will be set to the list.
    #[inline]
    pub fn allow_fields(&mut self, fields: &[&str]) {
        if self.fields.is_empty() {
            self.fields.extend(fields.iter().map(|&key| key.to_owned()));
        } else {
            self.fields.retain(|field| fields.contains(&field.as_str()))
        }
    }

    /// Removes the projection fields in the deny list.
    #[inline]
    pub fn deny_fields(&mut self, fields: &[&str]) {
        self.fields
            .retain(|field| !fields.contains(&field.as_str()))
    }

    /// Adds a projection field with the alias.
    #[inline]
    pub fn add_field_alias(&mut self, expr: impl Into<String>, alias: impl Into<String>) {
        self.fields.push([alias.into(), expr.into()].join(":"));
    }

    /// Adds a key-value pair to the query filters.
    #[inline]
    pub fn add_filter(&mut self, key: impl Into<String>, value: impl Into<JsonValue>) {
        self.filters.upsert(key, value);
    }

    /// Moves all elements from the `filters` into `self`.
    #[inline]
    pub fn append_filters(&mut self, filters: &mut Map) {
        self.filters.append(filters);
    }

    /// Removes a query filter with the key.
    #[inline]
    pub fn remove_filter(&mut self, key: &str) -> Option<JsonValue> {
        self.filters.remove(key)
    }

    /// Sets the extra flag.
    #[inline]
    pub fn set_extra_flag(&mut self, key: impl Into<String>, value: impl Into<JsonValue>) {
        self.extra.upsert(key, value);
    }

    /// Appends the extra flags.
    #[inline]
    pub fn append_extra_flags(&mut self, flags: &mut Map) {
        self.extra.append(flags);
    }

    /// Sets the sort order.
    #[inline]
    pub fn set_order(&mut self, sort_order: Vec<QueryOrder>) {
        self.sort_order = sort_order;
    }

    /// Adds a query order.
    #[inline]
    pub fn order_by(&mut self, field: impl Into<SharedString>, descending: bool) {
        let field = field.into();
        self.sort_order
            .retain(|order| order.field() != field.as_ref());
        self.sort_order.push(QueryOrder::new(field, descending));
    }

    /// Adds a query order with an extra flag to indicate whether the nulls appear first or last.
    pub fn order_by_with_nulls(
        &mut self,
        field: impl Into<SharedString>,
        descending: bool,
        nulls_first: bool,
    ) {
        let field = field.into();
        self.sort_order
            .retain(|order| order.field() != field.as_ref());

        let mut order = QueryOrder::new(field, descending);
        if nulls_first {
            order.set_nulls_first();
        } else {
            order.set_nulls_last();
        }
        self.sort_order.push(order);
    }

    /// Adds a query order with an ascending order.
    #[inline]
    pub fn order_asc(&mut self, field: impl Into<SharedString>) {
        let field = field.into();
        self.sort_order
            .retain(|order| order.field() != field.as_ref());
        self.sort_order.push(QueryOrder::new(field, false));
    }

    /// Adds a query order with an descending order.
    #[inline]
    pub fn order_desc(&mut self, field: impl Into<SharedString>) {
        let field = field.into();
        self.sort_order
            .retain(|order| order.field() != field.as_ref());
        self.sort_order.push(QueryOrder::new(field, true));
    }

    /// Sets the query offset.
    #[inline]
    pub fn set_offset(&mut self, offset: usize) {
        self.offset = offset;
    }

    /// Sets the query limit.
    #[inline]
    pub fn set_limit(&mut self, limit: usize) {
        self.limit = limit;
    }

    /// Disables the query limit.
    #[inline]
    pub fn disable_limit(&mut self) {
        self.limit = 0;
    }

    /// Returns a reference to the projection fields.
    #[inline]
    pub fn fields(&self) -> &[String] {
        self.fields.as_slice()
    }

    /// Returns a reference to the filters.
    #[inline]
    pub fn filters(&self) -> &Map {
        &self.filters
    }

    /// Returns a reference to the sort order.
    #[inline]
    pub fn sort_order(&self) -> &[QueryOrder] {
        self.sort_order.as_slice()
    }

    /// Returns the query offset.
    #[inline]
    pub fn offset(&self) -> usize {
        self.offset
    }

    /// Returns the query limit.
    #[inline]
    pub fn limit(&self) -> usize {
        self.limit
    }

    /// Returns `true` if the `flag` has been enabled.
    #[inline]
    pub fn enabled(&self, flag: &str) -> bool {
        self.extra.get_bool(flag).is_some_and(|b| b)
    }

    /// Returns `true` if the `populate` flag has been enabled.
    #[inline]
    pub fn populate_enabled(&self) -> bool {
        self.enabled("populate")
    }

    /// Returns `true` if the `translate` flag has been enabled.
    #[inline]
    pub fn translate_enabled(&self) -> bool {
        self.enabled("translate")
    }

    /// Returns `true` if the `show_deleted` flag has been enabled.
    #[inline]
    pub fn show_deleted(&self) -> bool {
        self.enabled("show_deleted")
    }

    /// Returns `true` if the `validate_only` flag has been enabled.
    #[inline]
    pub fn validate_only(&self) -> bool {
        self.enabled("validate_only")
    }

    /// Returns `true` if the `no_check` flag has been enabled.
    #[inline]
    pub fn no_check(&self) -> bool {
        self.enabled("no_check")
    }
}

impl Default for Query {
    #[inline]
    fn default() -> Self {
        Self {
            fields: Vec::new(),
            filters: Map::new(),
            sort_order: Vec::new(),
            offset: 0,
            limit: 10,
            extra: Map::new(),
        }
    }
}