Skip to main content

openauth_core/db/adapter/
query.rs

1use indexmap::IndexMap;
2use serde::{Deserialize, Serialize};
3
4use super::join::JoinOption;
5use super::value::{DbRecord, DbValue};
6
7/// Predicate operator.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9pub enum WhereOperator {
10    Eq,
11    Ne,
12    Lt,
13    Lte,
14    Gt,
15    Gte,
16    In,
17    NotIn,
18    Contains,
19    StartsWith,
20    EndsWith,
21}
22
23/// Connector between predicates.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
25pub enum Connector {
26    And,
27    Or,
28}
29
30/// Case sensitivity for string predicates.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
32pub enum WhereMode {
33    Sensitive,
34    Insensitive,
35}
36
37/// Adapter query predicate.
38#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
39pub struct Where {
40    pub field: String,
41    pub value: DbValue,
42    pub operator: WhereOperator,
43    pub connector: Connector,
44    pub mode: WhereMode,
45}
46
47impl Where {
48    pub fn new(field: impl Into<String>, value: DbValue) -> Self {
49        Self {
50            field: field.into(),
51            value,
52            operator: WhereOperator::Eq,
53            connector: Connector::And,
54            mode: WhereMode::Sensitive,
55        }
56    }
57
58    pub fn operator(mut self, operator: WhereOperator) -> Self {
59        self.operator = operator;
60        self
61    }
62
63    pub fn or(mut self) -> Self {
64        self.connector = Connector::Or;
65        self
66    }
67
68    pub fn insensitive(mut self) -> Self {
69        self.mode = WhereMode::Insensitive;
70        self
71    }
72}
73
74/// Sort direction.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
76pub enum SortDirection {
77    Asc,
78    Desc,
79}
80
81/// Sort clause.
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83pub struct Sort {
84    pub field: String,
85    pub direction: SortDirection,
86}
87
88impl Sort {
89    pub fn new(field: impl Into<String>, direction: SortDirection) -> Self {
90        Self {
91            field: field.into(),
92            direction,
93        }
94    }
95}
96
97/// Create query contract for adapters.
98#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
99pub struct Create {
100    pub model: String,
101    pub data: DbRecord,
102    pub select: Vec<String>,
103    pub force_allow_id: bool,
104}
105
106impl Create {
107    pub fn new(model: impl Into<String>) -> Self {
108        Self {
109            model: model.into(),
110            data: DbRecord::new(),
111            select: Vec::new(),
112            force_allow_id: false,
113        }
114    }
115
116    pub fn data(mut self, field: impl Into<String>, value: DbValue) -> Self {
117        self.data.insert(field.into(), value);
118        self
119    }
120
121    pub fn select<const N: usize>(mut self, fields: [&str; N]) -> Self {
122        self.select = fields.into_iter().map(str::to_owned).collect();
123        self
124    }
125
126    pub fn force_allow_id(mut self) -> Self {
127        self.force_allow_id = true;
128        self
129    }
130}
131
132/// Find-one query contract for adapters.
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct FindOne {
135    pub model: String,
136    pub where_clauses: Vec<Where>,
137    pub select: Vec<String>,
138    pub joins: IndexMap<String, JoinOption>,
139}
140
141impl FindOne {
142    pub fn new(model: impl Into<String>) -> Self {
143        Self {
144            model: model.into(),
145            where_clauses: Vec::new(),
146            select: Vec::new(),
147            joins: IndexMap::new(),
148        }
149    }
150
151    pub fn where_clause(mut self, where_clause: Where) -> Self {
152        self.where_clauses.push(where_clause);
153        self
154    }
155
156    pub fn select<const N: usize>(mut self, fields: [&str; N]) -> Self {
157        self.select = fields.into_iter().map(str::to_owned).collect();
158        self
159    }
160
161    pub fn join(mut self, model: impl Into<String>, option: JoinOption) -> Self {
162        self.joins.insert(model.into(), option);
163        self
164    }
165}
166
167/// Find-many query contract for adapters.
168#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
169pub struct FindMany {
170    pub model: String,
171    pub where_clauses: Vec<Where>,
172    pub limit: Option<usize>,
173    pub offset: Option<usize>,
174    pub sort_by: Option<Sort>,
175    pub select: Vec<String>,
176    pub joins: IndexMap<String, JoinOption>,
177}
178
179impl FindMany {
180    pub fn new(model: impl Into<String>) -> Self {
181        Self {
182            model: model.into(),
183            where_clauses: Vec::new(),
184            limit: None,
185            offset: None,
186            sort_by: None,
187            select: Vec::new(),
188            joins: IndexMap::new(),
189        }
190    }
191
192    pub fn where_clause(mut self, where_clause: Where) -> Self {
193        self.where_clauses.push(where_clause);
194        self
195    }
196
197    pub fn limit(mut self, limit: usize) -> Self {
198        self.limit = Some(limit);
199        self
200    }
201
202    pub fn offset(mut self, offset: usize) -> Self {
203        self.offset = Some(offset);
204        self
205    }
206
207    pub fn sort_by(mut self, sort: Sort) -> Self {
208        self.sort_by = Some(sort);
209        self
210    }
211
212    pub fn select<const N: usize>(mut self, fields: [&str; N]) -> Self {
213        self.select = fields.into_iter().map(str::to_owned).collect();
214        self
215    }
216
217    pub fn join(mut self, model: impl Into<String>, option: JoinOption) -> Self {
218        self.joins.insert(model.into(), option);
219        self
220    }
221}
222
223/// Count query contract for adapters.
224#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
225pub struct Count {
226    pub model: String,
227    pub where_clauses: Vec<Where>,
228}
229
230impl Count {
231    pub fn new(model: impl Into<String>) -> Self {
232        Self {
233            model: model.into(),
234            where_clauses: Vec::new(),
235        }
236    }
237
238    pub fn where_clause(mut self, where_clause: Where) -> Self {
239        self.where_clauses.push(where_clause);
240        self
241    }
242}
243
244/// Single-row update query contract for adapters.
245#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
246pub struct Update {
247    pub model: String,
248    pub where_clauses: Vec<Where>,
249    pub data: DbRecord,
250}
251
252impl Update {
253    pub fn new(model: impl Into<String>) -> Self {
254        Self {
255            model: model.into(),
256            where_clauses: Vec::new(),
257            data: DbRecord::new(),
258        }
259    }
260
261    pub fn where_clause(mut self, where_clause: Where) -> Self {
262        self.where_clauses.push(where_clause);
263        self
264    }
265
266    pub fn data(mut self, field: impl Into<String>, value: DbValue) -> Self {
267        self.data.insert(field.into(), value);
268        self
269    }
270}
271
272/// Multi-row update query contract for adapters.
273#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
274pub struct UpdateMany {
275    pub model: String,
276    pub where_clauses: Vec<Where>,
277    pub data: DbRecord,
278}
279
280impl UpdateMany {
281    pub fn new(model: impl Into<String>) -> Self {
282        Self {
283            model: model.into(),
284            where_clauses: Vec::new(),
285            data: DbRecord::new(),
286        }
287    }
288
289    pub fn where_clause(mut self, where_clause: Where) -> Self {
290        self.where_clauses.push(where_clause);
291        self
292    }
293
294    pub fn data(mut self, field: impl Into<String>, value: DbValue) -> Self {
295        self.data.insert(field.into(), value);
296        self
297    }
298}
299
300/// Single-row delete query contract for adapters.
301#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
302pub struct Delete {
303    pub model: String,
304    pub where_clauses: Vec<Where>,
305}
306
307impl Delete {
308    pub fn new(model: impl Into<String>) -> Self {
309        Self {
310            model: model.into(),
311            where_clauses: Vec::new(),
312        }
313    }
314
315    pub fn where_clause(mut self, where_clause: Where) -> Self {
316        self.where_clauses.push(where_clause);
317        self
318    }
319}
320
321/// Multi-row delete query contract for adapters.
322#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
323pub struct DeleteMany {
324    pub model: String,
325    pub where_clauses: Vec<Where>,
326}
327
328impl DeleteMany {
329    pub fn new(model: impl Into<String>) -> Self {
330        Self {
331            model: model.into(),
332            where_clauses: Vec::new(),
333        }
334    }
335
336    pub fn where_clause(mut self, where_clause: Where) -> Self {
337        self.where_clauses.push(where_clause);
338        self
339    }
340}