reinhardt-rest 0.1.2

REST API framework aggregator for Reinhardt
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Type-safe query filter using Field and Lookup
//!
//! Provides a completely type-safe filtering system using reinhardt-orm's
//! Field<M, T> and Lookup types.

use super::ordering_field::OrderingField;
use super::{FilterBackend, FilterResult};
use async_trait::async_trait;
use reinhardt_db::orm::{
	Cond, Expr, Lookup, Model, MySqlQueryBuilder, Query, QueryFieldCompiler, QueryStatementBuilder,
};
use std::collections::HashMap;
use std::marker::PhantomData;

/// Type-safe query filter
///
/// Combines Field-based lookups and ordering into SQL WHERE and ORDER BY clauses.
/// All field access is compile-time checked for correctness.
///
/// # Examples
///
/// ```rust
/// # use reinhardt_rest::filters::QueryFilter;
/// # use reinhardt_db::orm::{Field, FieldSelector, Model};
/// # use reinhardt_rest::filters::field_extensions::FieldOrderingExt;
/// # #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
/// # struct Post {
/// #     id: i64,
/// #     title: String,
/// #     created_at: String,
/// # }
/// # #[derive(Clone)]
/// # struct PostFields;
/// # impl FieldSelector for PostFields {
/// #     fn with_alias(self, _alias: &str) -> Self { self }
/// # }
/// # impl Model for Post {
/// #     type PrimaryKey = i64;
/// #     type Fields = PostFields;
/// #     fn table_name() -> &'static str { "posts" }
/// #     fn new_fields() -> Self::Fields { PostFields }
/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
/// # }
/// let filter = QueryFilter::<Post>::new()
///     .with_lookup(Field::new(vec!["title"]).icontains("rust"))
///     .with_lookup(Field::<Post, i32>::new(vec!["created_at"]).gte(2024))
///     .order_by(Field::<Post, String>::new(vec!["title"]).asc());
/// // Verify filter was constructed successfully
/// assert_eq!(filter.lookups().len(), 2);
/// assert_eq!(filter.ordering().len(), 1);
/// ```
pub struct QueryFilter<M: Model> {
	lookups: Vec<Lookup<M>>,
	or_groups: Vec<Vec<Lookup<M>>>, // Each inner Vec is OR'd together, outer Vec is AND'd
	ordering: Vec<OrderingField<M>>,
	_phantom: PhantomData<M>,
}

impl<M: Model> QueryFilter<M> {
	/// Create a new empty filter
	pub fn new() -> Self {
		Self {
			lookups: Vec::new(),
			or_groups: Vec::new(),
			ordering: Vec::new(),
			_phantom: PhantomData,
		}
	}

	/// Add a lookup condition
	///
	/// All conditions are combined with AND.
	///
	/// # Examples
	///
	/// ```rust
	/// # use reinhardt_rest::filters::QueryFilter;
	/// # use reinhardt_db::orm::{Field, FieldSelector, Model};
	/// # #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// # struct Post {
	/// #     id: i64,
	/// #     title: String,
	/// #     age: i32,
	/// # }
	/// # #[derive(Clone)]
	/// # struct PostFields;
	/// # impl FieldSelector for PostFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// # impl Model for Post {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = PostFields;
	/// #     fn table_name() -> &'static str { "posts" }
	/// #     fn new_fields() -> Self::Fields { PostFields }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
	/// # }
	/// let filter = QueryFilter::<Post>::new()
	///     .with_lookup(Field::new(vec!["title"]).icontains("rust"))
	///     .with_lookup(Field::new(vec!["age"]).gte(18));
	/// assert_eq!(filter.lookups().len(), 2);
	/// ```
	pub fn with_lookup(mut self, lookup: Lookup<M>) -> Self {
		self.lookups.push(lookup);
		self
	}

	/// Add multiple lookups at once
	pub fn add_all(mut self, lookups: Vec<Lookup<M>>) -> Self {
		self.lookups.extend(lookups);
		self
	}

	/// Add an ordering field
	///
	/// Multiple ordering fields are applied in the order they are added.
	///
	/// # Examples
	///
	/// ```rust
	/// # use reinhardt_rest::filters::QueryFilter;
	/// # use reinhardt_db::orm::{Field, FieldSelector, Model};
	/// # use reinhardt_rest::filters::field_extensions::FieldOrderingExt;
	/// # #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// # struct Post {
	/// #     id: i64,
	/// #     title: String,
	/// #     created_at: String,
	/// # }
	/// # #[derive(Clone)]
	/// # struct PostFields;
	/// # impl FieldSelector for PostFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// # impl Model for Post {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = PostFields;
	/// #     fn table_name() -> &'static str { "posts" }
	/// #     fn new_fields() -> Self::Fields { PostFields }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
	/// # }
	/// let filter = QueryFilter::<Post>::new()
	///     .order_by(Field::<Post, String>::new(vec!["created_at"]).desc())
	///     .order_by(Field::<Post, String>::new(vec!["title"]).asc());
	/// assert_eq!(filter.ordering().len(), 2);
	/// ```
	pub fn order_by(mut self, field: OrderingField<M>) -> Self {
		self.ordering.push(field);
		self
	}

	/// Add multiple ordering fields at once
	pub fn order_by_all(mut self, fields: Vec<OrderingField<M>>) -> Self {
		self.ordering.extend(fields);
		self
	}

	/// Add an OR group (lookups within the group are OR'd together)
	///
	/// # Examples
	///
	/// ```rust
	/// # use reinhardt_rest::filters::QueryFilter;
	/// # use reinhardt_db::orm::{Field, FieldSelector, Model};
	/// # #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// # struct Post {
	/// #     id: i64,
	/// #     title: String,
	/// #     content: String,
	/// # }
	/// # #[derive(Clone)]
	/// # struct PostFields;
	/// # impl FieldSelector for PostFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// # impl Model for Post {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = PostFields;
	/// #     fn table_name() -> &'static str { "posts" }
	/// #     fn new_fields() -> Self::Fields { PostFields }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
	/// # }
	/// // (title ICONTAINS 'rust' OR content ICONTAINS 'rust')
	/// let filter = QueryFilter::<Post>::new()
	///     .add_or_group(vec![
	///         Field::new(vec!["title"]).icontains("rust"),
	///         Field::new(vec!["content"]).icontains("rust"),
	///     ]);
	/// assert_eq!(filter.or_groups().len(), 1);
	/// assert_eq!(filter.or_groups()[0].len(), 2);
	/// ```
	pub fn add_or_group(mut self, lookups: Vec<Lookup<M>>) -> Self {
		if !lookups.is_empty() {
			self.or_groups.push(lookups);
		}
		self
	}

	/// Add multiple OR groups from multi-term search
	///
	/// Each term becomes an OR group, and all groups are AND'd together.
	///
	/// # Examples
	///
	/// ```rust
	/// # use reinhardt_rest::filters::QueryFilter;
	/// # use reinhardt_db::orm::{Field, FieldSelector, Model};
	/// # #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// # struct Post {
	/// #     id: i64,
	/// #     title: String,
	/// # }
	/// # #[derive(Clone)]
	/// # struct PostFields;
	/// # impl FieldSelector for PostFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// # impl Model for Post {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = PostFields;
	/// #     fn table_name() -> &'static str { "posts" }
	/// #     fn new_fields() -> Self::Fields { PostFields }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
	/// # }
	/// // Simulate multi-term search
	/// let term_lookups = vec![
	///     vec![Field::new(vec!["title"]).icontains("rust")],
	///     vec![Field::new(vec!["title"]).icontains("web")],
	/// ];
	/// let filter = QueryFilter::<Post>::new()
	///     .add_multi_term(term_lookups);
	/// assert_eq!(filter.or_groups().len(), 2);
	/// ```
	pub fn add_multi_term(mut self, term_lookups: Vec<Vec<Lookup<M>>>) -> Self {
		for lookups in term_lookups {
			if !lookups.is_empty() {
				self.or_groups.push(lookups);
			}
		}
		self
	}

	/// Get the list of lookups
	pub fn lookups(&self) -> &[Lookup<M>] {
		&self.lookups
	}

	/// Get the list of OR groups
	pub fn or_groups(&self) -> &[Vec<Lookup<M>>] {
		&self.or_groups
	}

	/// Get the list of ordering fields
	pub fn ordering(&self) -> &[OrderingField<M>] {
		&self.ordering
	}

	/// Compile lookups to SQL WHERE clause using reinhardt-query
	fn compile_where_clause(&self) -> Option<String> {
		if self.lookups.is_empty() && self.or_groups.is_empty() {
			return None;
		}

		// Build the main AND condition using reinhardt-query
		let mut main_cond = Cond::all();

		// Add regular AND conditions
		for lookup in &self.lookups {
			main_cond = main_cond.add(QueryFieldCompiler::compile_to_expr(lookup));
		}

		// Add OR groups (each group is AND'd with others)
		for or_group in &self.or_groups {
			if or_group.is_empty() {
				continue;
			}

			if or_group.len() == 1 {
				// Single condition, add directly
				main_cond = main_cond.add(QueryFieldCompiler::compile_to_expr(&or_group[0]));
			} else {
				// Multiple conditions, create OR group
				let mut or_cond = Cond::any();
				for lookup in or_group {
					or_cond = or_cond.add(QueryFieldCompiler::compile_to_expr(lookup));
				}
				main_cond = main_cond.add(or_cond);
			}
		}

		// Build a dummy query to extract just the WHERE clause
		let query = Query::select()
			.expr(Expr::val(1))
			.cond_where(main_cond)
			.to_string(MySqlQueryBuilder);

		// Extract just the WHERE portion (after "WHERE ")
		query.find("WHERE ").map(|idx| query[idx + 6..].to_string())
	}

	/// Compile ordering to SQL ORDER BY clause
	fn compile_order_clause(&self) -> Option<String> {
		if self.ordering.is_empty() {
			return None;
		}

		let order_parts: Vec<String> = self.ordering.iter().map(|field| field.to_sql()).collect();

		Some(order_parts.join(", "))
	}

	/// Append order clause to existing ORDER BY
	///
	/// Supports:
	/// - Appending to existing ORDER BY clause
	/// - Merging multiple ordering fields
	///
	/// # Examples
	///
	/// ```ignore
	/// // Input: "SELECT * FROM posts ORDER BY created_at DESC"
	/// // New ordering: "title ASC"
	/// // Output: "SELECT * FROM posts ORDER BY created_at DESC, title ASC"
	/// ```
	fn append_order_clause(&self, sql: &str, new_order: &str) -> String {
		// Find ORDER BY position
		if let Some(order_by_pos) = sql.find("ORDER BY") {
			let before_order = &sql[..order_by_pos + 8]; // Include "ORDER BY"
			let after_order = &sql[order_by_pos + 8..];

			// Find end of ORDER BY clause (before LIMIT, OFFSET, or end of string)
			let end_markers = ["LIMIT", "OFFSET", ";"];
			let mut end_pos = after_order.len();

			for marker in &end_markers {
				if let Some(pos) = after_order.find(marker) {
					end_pos = end_pos.min(pos);
				}
			}

			let existing_order = after_order[..end_pos].trim();
			let remaining = &after_order[end_pos..];

			// Merge existing and new order clauses
			// Ensure space before remaining clause if it exists
			if remaining.is_empty() {
				format!("{} {}, {}", before_order, existing_order, new_order)
			} else {
				format!(
					"{} {}, {} {}",
					before_order,
					existing_order,
					new_order,
					remaining.trim()
				)
			}
		} else {
			// No ORDER BY found, append it
			format!("{} ORDER BY {}", sql, new_order)
		}
	}
}

impl<M: Model> Default for QueryFilter<M> {
	fn default() -> Self {
		Self::new()
	}
}

#[async_trait]
impl<M: Model> FilterBackend for QueryFilter<M> {
	async fn filter_queryset(
		&self,
		_params: &HashMap<String, String>,
		mut sql: String,
	) -> FilterResult<String> {
		// Add WHERE clause if we have lookups
		if let Some(where_clause) = self.compile_where_clause() {
			sql = if sql.contains("WHERE") {
				// Already has WHERE, add with AND
				sql.replace("WHERE", &format!("WHERE {} AND", where_clause))
			} else {
				// No WHERE yet, add it
				format!("{} WHERE {}", sql, where_clause)
			};
		}

		// Add ORDER BY clause if we have ordering
		if let Some(order_clause) = self.compile_order_clause() {
			if sql.contains("ORDER BY") {
				// Already has ORDER BY, append to it
				sql = self.append_order_clause(&sql, &order_clause);
			} else {
				sql = format!("{} ORDER BY {}", sql, order_clause);
			}
		}

		Ok(sql)
	}
}