reinhardt_db/orm/
postgres_fields.rs1use chrono::{NaiveDate, NaiveDateTime};
29use serde::{Deserialize, Serialize};
30use std::marker::PhantomData;
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct ArrayField<T> {
51 base_type: String,
52 size: Option<usize>,
53 default: Option<Vec<T>>,
54 _phantom: PhantomData<T>,
55}
56
57impl<T> ArrayField<T> {
58 pub fn new(base_type: impl Into<String>) -> Self {
69 Self {
70 base_type: base_type.into(),
71 size: None,
72 default: None,
73 _phantom: PhantomData,
74 }
75 }
76
77 pub fn with_size(mut self, size: usize) -> Self {
79 self.size = Some(size);
80 self
81 }
82
83 pub fn with_default(mut self, default: Vec<T>) -> Self {
85 self.default = Some(default);
86 self
87 }
88
89 pub fn base_type(&self) -> &str {
91 &self.base_type
92 }
93
94 pub fn size(&self) -> Option<usize> {
96 self.size
97 }
98
99 pub fn sql_type(&self) -> String {
113 if let Some(size) = self.size {
114 format!("{}[{}]", self.base_type, size)
115 } else {
116 format!("{}[]", self.base_type)
117 }
118 }
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct JSONBField {
136 default: Option<serde_json::Value>,
137}
138
139impl JSONBField {
140 pub fn new() -> Self {
151 Self { default: None }
152 }
153
154 pub fn with_default(mut self, default: serde_json::Value) -> Self {
156 self.default = Some(default);
157 self
158 }
159
160 pub fn sql_type(&self) -> &'static str {
162 "JSONB"
163 }
164
165 pub fn default(&self) -> Option<&serde_json::Value> {
167 self.default.as_ref()
168 }
169}
170
171impl Default for JSONBField {
172 fn default() -> Self {
173 Self::new()
174 }
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize)]
190pub struct HStoreField {
191 default: Option<std::collections::HashMap<String, String>>,
192}
193
194impl HStoreField {
195 pub fn new() -> Self {
206 Self { default: None }
207 }
208
209 pub fn with_default(mut self, default: std::collections::HashMap<String, String>) -> Self {
211 self.default = Some(default);
212 self
213 }
214
215 pub fn sql_type(&self) -> &'static str {
217 "HSTORE"
218 }
219}
220
221impl Default for HStoreField {
222 fn default() -> Self {
223 Self::new()
224 }
225}
226
227#[derive(Debug, Clone, Serialize, Deserialize)]
240pub struct IntegerRangeField {
241 default: Option<(Option<i32>, Option<i32>)>,
242}
243
244impl IntegerRangeField {
245 pub fn new() -> Self {
247 Self { default: None }
248 }
249
250 pub fn with_default(mut self, lower: Option<i32>, upper: Option<i32>) -> Self {
252 self.default = Some((lower, upper));
253 self
254 }
255
256 pub fn sql_type(&self) -> &'static str {
258 "INT4RANGE"
259 }
260}
261
262impl Default for IntegerRangeField {
263 fn default() -> Self {
264 Self::new()
265 }
266}
267
268#[derive(Debug, Clone, Serialize, Deserialize)]
281pub struct BigIntegerRangeField {
282 default: Option<(Option<i64>, Option<i64>)>,
283}
284
285impl BigIntegerRangeField {
286 pub fn new() -> Self {
288 Self { default: None }
289 }
290
291 pub fn with_default(mut self, lower: Option<i64>, upper: Option<i64>) -> Self {
293 self.default = Some((lower, upper));
294 self
295 }
296
297 pub fn sql_type(&self) -> &'static str {
299 "INT8RANGE"
300 }
301}
302
303impl Default for BigIntegerRangeField {
304 fn default() -> Self {
305 Self::new()
306 }
307}
308
309#[derive(Debug, Clone, Serialize, Deserialize)]
322pub struct DateRangeField {
323 default: Option<(Option<NaiveDate>, Option<NaiveDate>)>,
324}
325
326impl DateRangeField {
327 pub fn new() -> Self {
329 Self { default: None }
330 }
331
332 pub fn with_default(mut self, lower: Option<NaiveDate>, upper: Option<NaiveDate>) -> Self {
334 self.default = Some((lower, upper));
335 self
336 }
337
338 pub fn sql_type(&self) -> &'static str {
340 "DATERANGE"
341 }
342}
343
344impl Default for DateRangeField {
345 fn default() -> Self {
346 Self::new()
347 }
348}
349
350#[derive(Debug, Clone, Serialize, Deserialize)]
363pub struct DateTimeRangeField {
364 default: Option<(Option<NaiveDateTime>, Option<NaiveDateTime>)>,
365}
366
367impl DateTimeRangeField {
368 pub fn new() -> Self {
370 Self { default: None }
371 }
372
373 pub fn with_default(
375 mut self,
376 lower: Option<NaiveDateTime>,
377 upper: Option<NaiveDateTime>,
378 ) -> Self {
379 self.default = Some((lower, upper));
380 self
381 }
382
383 pub fn sql_type(&self) -> &'static str {
385 "TSTZRANGE"
386 }
387}
388
389impl Default for DateTimeRangeField {
390 fn default() -> Self {
391 Self::new()
392 }
393}
394
395#[derive(Debug, Clone, Serialize, Deserialize)]
408pub struct CITextField {
409 max_length: Option<usize>,
410 default: Option<String>,
411}
412
413impl CITextField {
414 pub fn new() -> Self {
416 Self {
417 max_length: None,
418 default: None,
419 }
420 }
421
422 pub fn with_max_length(mut self, max_length: usize) -> Self {
424 self.max_length = Some(max_length);
425 self
426 }
427
428 pub fn with_default(mut self, default: impl Into<String>) -> Self {
430 self.default = Some(default.into());
431 self
432 }
433
434 pub fn sql_type(&self) -> &'static str {
436 "CITEXT"
437 }
438}
439
440impl Default for CITextField {
441 fn default() -> Self {
442 Self::new()
443 }
444}
445
446#[cfg(test)]
447mod tests {
448 use super::*;
449
450 #[test]
451 fn test_array_field_basic() {
452 let field = ArrayField::<i32>::new("INTEGER");
453 assert_eq!(field.base_type(), "INTEGER");
454 assert_eq!(field.sql_type(), "INTEGER[]");
455 }
456
457 #[test]
458 fn test_array_field_with_size() {
459 let field = ArrayField::<String>::new("VARCHAR(50)").with_size(10);
460 assert_eq!(field.size(), Some(10));
461 assert_eq!(field.sql_type(), "VARCHAR(50)[10]");
462 }
463
464 #[test]
465 fn test_jsonb_field() {
466 let field = JSONBField::new();
467 assert_eq!(field.sql_type(), "JSONB");
468 }
469
470 #[test]
471 fn test_jsonb_field_with_default() {
472 let default = serde_json::json!({"key": "value"});
473 let field = JSONBField::new().with_default(default.clone());
474 assert_eq!(field.default(), Some(&default));
475 }
476
477 #[test]
478 fn test_hstore_field() {
479 let field = HStoreField::new();
480 assert_eq!(field.sql_type(), "HSTORE");
481 }
482
483 #[test]
484 fn test_integer_range_field() {
485 let field = IntegerRangeField::new();
486 assert_eq!(field.sql_type(), "INT4RANGE");
487 }
488
489 #[test]
490 fn test_biginteger_range_field() {
491 let field = BigIntegerRangeField::new();
492 assert_eq!(field.sql_type(), "INT8RANGE");
493 }
494
495 #[test]
496 fn test_date_range_field() {
497 let field = DateRangeField::new();
498 assert_eq!(field.sql_type(), "DATERANGE");
499 }
500
501 #[test]
502 fn test_datetime_range_field() {
503 let field = DateTimeRangeField::new();
504 assert_eq!(field.sql_type(), "TSTZRANGE");
505 }
506
507 #[test]
508 fn test_citext_field() {
509 let field = CITextField::new();
510 assert_eq!(field.sql_type(), "CITEXT");
511 }
512
513 #[test]
514 fn test_citext_field_with_max_length() {
515 let field = CITextField::new().with_max_length(255);
516 assert_eq!(field.sql_type(), "CITEXT");
517 }
518}