reinhardt-db 0.1.1

Django-style database layer for Reinhardt framework
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Type-safe field representation with transformation support

use super::comparison::{ComparisonOperator, FieldComparison, FieldRef};
use super::lookup::{Lookup, LookupType};
use super::traits::{Comparable, Date, DateTime, NumericType};
use crate::orm::Model;
use std::marker::PhantomData;

/// Represents a field with its type information
///
/// The type parameter `M` is the model type, and `T` is the field's type.
/// This allows us to enforce type safety at compile time.
///
/// # Breaking Change
///
/// The type of `path` has been changed from `Vec<&'static str>` to `Vec<String>`.
/// Table alias support has been added.
#[derive(Debug, Clone)]
pub struct Field<M, T> {
	pub(crate) path: Vec<String>,
	pub(crate) table_alias: Option<String>,
	pub(crate) _phantom: PhantomData<(M, T)>,
}

impl<M: Model, T> Field<M, T> {
	/// Create a new field with the given path
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_db::orm::query_fields::Field;
	/// use reinhardt_db::orm::Model;
	/// use serde::{Serialize, Deserialize};
	///
	/// #[derive(Debug, Clone, Serialize, Deserialize)]
	/// struct User {
	///     id: Option<i32>,
	///     name: String,
	/// }
	///
	/// #[derive(Clone)]
	/// struct UserFields;
	/// impl reinhardt_db::orm::FieldSelector for UserFields {
	///     fn with_alias(self, _alias: &str) -> Self { self }
	/// }
	///
	/// impl Model for User {
	///     type PrimaryKey = i32;
	///     type Fields = UserFields;
	///     fn table_name() -> &'static str {
	///         "users"
	///     }
	///     fn new_fields() -> Self::Fields { UserFields }
	///     fn primary_key(&self) -> Option<Self::PrimaryKey> {
	///         self.id
	///     }
	///     fn set_primary_key(&mut self, value: Self::PrimaryKey) {
	///         self.id = Some(value);
	///     }
	/// }
	///
	/// let field: Field<User, String> = Field::new(vec!["name"]);
	/// assert_eq!(field.path(), &["name".to_string()]);
	/// ```
	pub fn new<S: Into<String>>(path: Vec<S>) -> Self {
		Self {
			path: path.into_iter().map(|s| s.into()).collect(),
			table_alias: None,
			_phantom: PhantomData,
		}
	}

	/// Get the field path
	pub fn path(&self) -> &[String] {
		&self.path
	}

	/// Set table alias
	///
	/// Used when referencing the same table multiple times (e.g., self-JOINs).
	///
	/// # Examples
	///
	/// ```ignore
	/// let u1_id = Field::<User, i32>::new(vec!["id".to_string()]).with_alias("u1");
	/// let u2_id = Field::<User, i32>::new(vec!["id".to_string()]).with_alias("u2");
	/// ```
	pub fn with_alias(mut self, alias: &str) -> Self {
		self.table_alias = Some(alias.to_string());
		self
	}

	/// Convert Field to FieldRef for comparison operations
	pub(crate) fn to_field_ref(&self) -> FieldRef {
		FieldRef::Field {
			table_alias: self.table_alias.clone(),
			field_path: self.path.clone(),
		}
	}

	// =============================================================================
	// Inter-field comparison methods (for JOIN conditions)
	// =============================================================================

	/// Inter-field comparison: <
	///
	/// # Examples
	///
	/// ```ignore
	/// // u1.id < u2.id
	/// let comparison = u1_id.field_lt(u2_id);
	/// ```
	pub fn field_lt<M2: Model>(self, other: Field<M2, T>) -> FieldComparison {
		FieldComparison::new(
			self.to_field_ref(),
			other.to_field_ref(),
			ComparisonOperator::Lt,
		)
	}

	/// Inter-field comparison: >
	pub fn field_gt<M2: Model>(self, other: Field<M2, T>) -> FieldComparison {
		FieldComparison::new(
			self.to_field_ref(),
			other.to_field_ref(),
			ComparisonOperator::Gt,
		)
	}

	/// Inter-field comparison: <=
	pub fn field_lte<M2: Model>(self, other: Field<M2, T>) -> FieldComparison {
		FieldComparison::new(
			self.to_field_ref(),
			other.to_field_ref(),
			ComparisonOperator::Lte,
		)
	}

	/// Inter-field comparison: >=
	pub fn field_gte<M2: Model>(self, other: Field<M2, T>) -> FieldComparison {
		FieldComparison::new(
			self.to_field_ref(),
			other.to_field_ref(),
			ComparisonOperator::Gte,
		)
	}

	/// Inter-field comparison: ==
	pub fn field_eq<M2: Model>(self, other: Field<M2, T>) -> FieldComparison {
		FieldComparison::new(
			self.to_field_ref(),
			other.to_field_ref(),
			ComparisonOperator::Eq,
		)
	}

	/// Inter-field comparison: !=
	pub fn field_ne<M2: Model>(self, other: Field<M2, T>) -> FieldComparison {
		FieldComparison::new(
			self.to_field_ref(),
			other.to_field_ref(),
			ComparisonOperator::Ne,
		)
	}
}

// =============================================================================
// String-specific methods
// =============================================================================

impl<M: Model> Field<M, String> {
	/// Convert field to lowercase: LOWER(field)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_db::orm::query_fields::Field;
	/// use reinhardt_db::orm::Model;
	/// use reinhardt_core::validators::TableName;
	///
	/// #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// struct User {
	///     id: i64,
	///     email: String,
	/// }
	///
	/// const USER_TABLE: TableName = TableName::new_const("users");
	///
	/// #[derive(Clone)]
	/// struct UserFields;
	/// impl reinhardt_db::orm::FieldSelector for UserFields {
	///     fn with_alias(self, _alias: &str) -> Self { self }
	/// }
	///
	/// impl Model for User {
	///     type PrimaryKey = i64;
	///     type Fields = UserFields;
	///     fn table_name() -> &'static str { USER_TABLE.as_str() }
	///     fn new_fields() -> Self::Fields { UserFields }
	///     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
	///     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
	/// }
	///
	/// // Lower case transformation for case-insensitive comparisons
	/// let email_field = Field::<User, String>::new(vec!["email"]).lower();
	/// assert_eq!(email_field.path(), &["email", "lower"]);
	/// ```
	pub fn lower(mut self) -> Self {
		self.path.push("lower".to_string());
		self
	}
	/// Convert field to uppercase: UPPER(field)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_db::orm::query_fields::Field;
	/// use reinhardt_db::orm::Model;
	/// use reinhardt_core::validators::TableName;
	///
	/// #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// struct Product {
	///     id: i64,
	///     code: String,
	/// }
	///
	/// const PRODUCT_TABLE: TableName = TableName::new_const("products");
	///
	/// #[derive(Clone)]
	/// struct ProductFields;
	/// impl reinhardt_db::orm::FieldSelector for ProductFields {
	///     fn with_alias(self, _alias: &str) -> Self { self }
	/// }
	///
	/// impl Model for Product {
	///     type PrimaryKey = i64;
	///     type Fields = ProductFields;
	///     fn table_name() -> &'static str { PRODUCT_TABLE.as_str() }
	///     fn new_fields() -> Self::Fields { ProductFields }
	///     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
	///     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
	/// }
	///
	/// // Upper case transformation for normalization
	/// let code_field = Field::<Product, String>::new(vec!["code"]).upper();
	/// assert_eq!(code_field.path(), &["code", "upper"]);
	/// ```
	pub fn upper(mut self) -> Self {
		self.path.push("upper".to_string());
		self
	}
	/// Trim whitespace: TRIM(field)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_db::orm::query_fields::Field;
	/// use reinhardt_db::orm::Model;
	/// use reinhardt_core::validators::TableName;
	///
	/// #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// struct Comment {
	///     id: i64,
	///     text: String,
	/// }
	///
	/// const COMMENT_TABLE: TableName = TableName::new_const("comments");
	///
	/// #[derive(Clone)]
	/// struct CommentFields;
	/// impl reinhardt_db::orm::FieldSelector for CommentFields {
	///     fn with_alias(self, _alias: &str) -> Self { self }
	/// }
	///
	/// impl Model for Comment {
	///     type PrimaryKey = i64;
	///     type Fields = CommentFields;
	///     fn table_name() -> &'static str { COMMENT_TABLE.as_str() }
	///     fn new_fields() -> Self::Fields { CommentFields }
	///     fn primary_key(&self) -> Option<Self::PrimaryKey> { Some(self.id) }
	///     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = value; }
	/// }
	///
	/// // Trim whitespace from both ends
	/// let text_field = Field::<Comment, String>::new(vec!["text"]).trim();
	/// assert_eq!(text_field.path(), &["text", "trim"]);
	/// ```
	pub fn trim(mut self) -> Self {
		self.path.push("trim".to_string());
		self
	}
	/// Get string length: LENGTH(field)
	/// Note: Return type changes to usize
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_db::orm::query_fields::Field;
	/// use reinhardt_db::orm::Model;
	/// use reinhardt_core::validators::TableName;
	///
	/// #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	/// struct Post {
	///     id: i64,
	///     title: String,
	/// }
	///
	/// const POST_TABLE: TableName = TableName::new_const("posts");
	///
	/// #[derive(Clone)]
	/// struct PostFields;
	/// impl reinhardt_db::orm::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 { POST_TABLE.as_str() }
	///     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; }
	/// }
	///
	/// // Get length of string field (returns Field<M, usize>)
	/// let title_length = Field::<Post, String>::new(vec!["title"]).length();
	/// assert_eq!(title_length.path(), &["title", "length"]);
	/// ```
	pub fn length(mut self) -> Field<M, usize> {
		self.path.push("length".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}

	// Lookup methods specific to strings
	/// Check if field contains a substring: LIKE '%value%'
	///
	pub fn contains(self, value: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Contains, value.into())
	}
	/// Case-insensitive contains: ILIKE '%value%'
	///
	pub fn icontains(self, value: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::IContains, value.into())
	}
	/// Check if field starts with a substring: LIKE 'value%'
	///
	pub fn startswith(self, value: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::StartsWith, value.into())
	}
	/// Case-insensitive startswith: ILIKE 'value%'
	///
	pub fn istartswith(self, value: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::IStartsWith, value.into())
	}
	/// Check if field ends with a substring: LIKE '%value'
	///
	pub fn endswith(self, value: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::EndsWith, value.into())
	}
	/// Case-insensitive endswith: ILIKE '%value'
	///
	pub fn iendswith(self, value: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::IEndsWith, value.into())
	}
	/// Regular expression match: ~
	///
	pub fn regex(self, pattern: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Regex, pattern.into())
	}
	/// Case-insensitive regex: ~*
	///
	pub fn iregex(self, pattern: &str) -> Lookup<M> {
		Lookup::new(self.path, LookupType::IRegex, pattern.into())
	}
}

// =============================================================================
// DateTime-specific methods
// =============================================================================

impl<M: Model> Field<M, DateTime> {
	/// Extract year: EXTRACT(YEAR FROM field)
	///
	pub fn year(mut self) -> Field<M, i32> {
		self.path.push("year".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract month: EXTRACT(MONTH FROM field)
	///
	pub fn month(mut self) -> Field<M, i32> {
		self.path.push("month".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract day: EXTRACT(DAY FROM field)
	///
	pub fn day(mut self) -> Field<M, i32> {
		self.path.push("day".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract week: EXTRACT(WEEK FROM field)
	///
	pub fn week(mut self) -> Field<M, i32> {
		self.path.push("week".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract day of week: EXTRACT(DOW FROM field)
	///
	pub fn weekday(mut self) -> Field<M, i32> {
		self.path.push("weekday".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract quarter: EXTRACT(QUARTER FROM field)
	///
	pub fn quarter(mut self) -> Field<M, i32> {
		self.path.push("quarter".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract hour: EXTRACT(HOUR FROM field)
	///
	pub fn hour(mut self) -> Field<M, i32> {
		self.path.push("hour".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract minute: EXTRACT(MINUTE FROM field)
	///
	pub fn minute(mut self) -> Field<M, i32> {
		self.path.push("minute".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Extract second: EXTRACT(SECOND FROM field)
	///
	pub fn second(mut self) -> Field<M, i32> {
		self.path.push("second".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
	/// Convert to date: DATE(field)
	///
	pub fn date(mut self) -> Field<M, Date> {
		self.path.push("date".to_string());
		Field {
			path: self.path,
			table_alias: self.table_alias,
			_phantom: PhantomData,
		}
	}
}

// =============================================================================
// Numeric-specific methods
// =============================================================================

impl<M: Model, T: NumericType> Field<M, T> {
	/// Absolute value: ABS(field)
	///
	pub fn abs(mut self) -> Self {
		self.path.push("abs".to_string());
		self
	}
	/// Ceiling: CEIL(field)
	///
	pub fn ceil(mut self) -> Self {
		self.path.push("ceil".to_string());
		self
	}
	/// Floor: FLOOR(field)
	///
	pub fn floor(mut self) -> Self {
		self.path.push("floor".to_string());
		self
	}
	/// Round: ROUND(field)
	///
	pub fn round(mut self) -> Self {
		self.path.push("round".to_string());
		self
	}
}

// =============================================================================
// Comparison methods (available for all Comparable types)
// =============================================================================

impl<M: Model, T: Comparable> Field<M, T> {
	/// Exact equality: =
	///
	pub fn eq(self, value: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Exact, value.into())
	}
	/// Case-insensitive equality (for strings): ILIKE
	///
	pub fn iexact(self, value: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::IExact, value.into())
	}
	/// Not equal: !=
	///
	pub fn ne(self, value: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Ne, value.into())
	}
	/// Greater than: >
	///
	pub fn gt(self, value: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Gt, value.into())
	}
	/// Greater than or equal: >=
	///
	pub fn gte(self, value: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Gte, value.into())
	}
	/// Less than: <
	///
	pub fn lt(self, value: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Lt, value.into())
	}
	/// Less than or equal: <=
	///
	pub fn lte(self, value: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Lte, value.into())
	}
	/// Range check: BETWEEN
	///
	pub fn in_range(self, start: T, end: T) -> Lookup<M> {
		Lookup::new(self.path, LookupType::Range, (start, end).into())
	}
}

// =============================================================================
// Option<T> specific methods
// =============================================================================

impl<M: Model, T> Field<M, Option<T>> {
	/// Check if NULL: IS NULL
	///
	pub fn is_null(self) -> Lookup<M> {
		Lookup::new(self.path, LookupType::IsNull, ().into())
	}
	/// Check if NOT NULL: IS NOT NULL
	///
	pub fn is_not_null(self) -> Lookup<M> {
		Lookup::new(self.path, LookupType::IsNotNull, ().into())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::orm::Model;
	use reinhardt_core::validators::TableName;

	#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	struct TestUser {
		id: i64,
		email: String,
		age: i32,
		created_at: DateTime,
	}

	const TEST_USER_TABLE: TableName = TableName::new_const("test_user");

	#[derive(Debug, Clone)]
	struct TestUserFields;

	impl crate::orm::model::FieldSelector for TestUserFields {
		fn with_alias(self, _alias: &str) -> Self {
			self
		}
	}

	impl Model for TestUser {
		type PrimaryKey = i64;
		type Fields = TestUserFields;

		fn table_name() -> &'static str {
			TEST_USER_TABLE.as_str()
		}

		fn primary_key(&self) -> Option<Self::PrimaryKey> {
			Some(self.id)
		}

		fn set_primary_key(&mut self, value: Self::PrimaryKey) {
			self.id = value;
		}

		fn primary_key_field() -> &'static str {
			"id"
		}

		fn new_fields() -> Self::Fields {
			TestUserFields
		}
	}

	// These tests verify that the type system works correctly
	#[test]
	fn test_string_methods_compile() {
		let _lookup = Field::<TestUser, String>::new(vec!["email"])
			.lower()
			.contains("test");

		let _lookup = Field::<TestUser, String>::new(vec!["email"])
			.upper()
			.startswith("TEST");
	}

	#[test]
	fn test_numeric_methods_compile() {
		let _lookup = Field::<TestUser, i32>::new(vec!["age"]).abs().gte(18);
	}

	#[test]
	fn test_datetime_methods_compile() {
		let _lookup = Field::<TestUser, DateTime>::new(vec!["created_at"])
			.year()
			.eq(2025);
	}

	#[test]
	fn test_comparison_methods_compile() {
		let _lookup =
			Field::<TestUser, String>::new(vec!["email"]).eq("test@example.com".to_string());

		let _lookup = Field::<TestUser, i32>::new(vec!["age"]).gte(18);
	}
}