drizzle-types 0.1.6

A type-safe SQL query builder for Rust
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! `PostgreSQL` View DDL types
//!
//! This module provides two complementary types:
//! - [`ViewDef`] - A const-friendly definition type for compile-time schema definitions
//! - [`View`] - A runtime type for serde serialization/deserialization

use crate::alloc_prelude::*;

#[cfg(feature = "serde")]
use crate::serde_helpers::{cow_from_string, cow_option_from_string};

// =============================================================================
// ViewWithOption Types
// =============================================================================

/// Const-friendly view WITH options definition
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ViewWithOptionDef {
    /// CHECK OPTION ('local' | 'cascaded')
    pub check_option: Option<&'static str>,
    /// Security barrier flag
    pub security_barrier: bool,
    /// Security invoker flag
    pub security_invoker: bool,
    /// Fillfactor (for materialized views)
    pub fillfactor: Option<i32>,
    /// Toast tuple target (for materialized views)
    pub toast_tuple_target: Option<i32>,
    /// Parallel workers (for materialized views)
    pub parallel_workers: Option<i32>,
    /// Autovacuum enabled (for materialized views)
    pub autovacuum_enabled: Option<bool>,
    /// Vacuum index cleanup (for materialized views): 'auto' | 'on' | 'off'
    pub vacuum_index_cleanup: Option<&'static str>,
    /// Vacuum truncate (for materialized views)
    pub vacuum_truncate: Option<bool>,
    /// Autovacuum vacuum threshold (for materialized views)
    pub autovacuum_vacuum_threshold: Option<i32>,
    /// Autovacuum vacuum scale factor (for materialized views)
    pub autovacuum_vacuum_scale_factor: Option<i32>,
    /// Autovacuum vacuum cost delay (for materialized views)
    pub autovacuum_vacuum_cost_delay: Option<i32>,
    /// Autovacuum vacuum cost limit (for materialized views)
    pub autovacuum_vacuum_cost_limit: Option<i32>,
    /// Autovacuum freeze min age (for materialized views)
    pub autovacuum_freeze_min_age: Option<i64>,
    /// Autovacuum freeze max age (for materialized views)
    pub autovacuum_freeze_max_age: Option<i64>,
    /// Autovacuum freeze table age (for materialized views)
    pub autovacuum_freeze_table_age: Option<i64>,
    /// Autovacuum multixact freeze min age (for materialized views)
    pub autovacuum_multixact_freeze_min_age: Option<i64>,
    /// Autovacuum multixact freeze max age (for materialized views)
    pub autovacuum_multixact_freeze_max_age: Option<i64>,
    /// Autovacuum multixact freeze table age (for materialized views)
    pub autovacuum_multixact_freeze_table_age: Option<i64>,
    /// Log autovacuum min duration (for materialized views)
    pub log_autovacuum_min_duration: Option<i32>,
    /// User catalog table (for materialized views)
    pub user_catalog_table: Option<bool>,
}

impl ViewWithOptionDef {
    /// Create a new view WITH options definition
    #[must_use]
    pub const fn new() -> Self {
        Self {
            check_option: None,
            security_barrier: false,
            security_invoker: false,
            fillfactor: None,
            toast_tuple_target: None,
            parallel_workers: None,
            autovacuum_enabled: None,
            vacuum_index_cleanup: None,
            vacuum_truncate: None,
            autovacuum_vacuum_threshold: None,
            autovacuum_vacuum_scale_factor: None,
            autovacuum_vacuum_cost_delay: None,
            autovacuum_vacuum_cost_limit: None,
            autovacuum_freeze_min_age: None,
            autovacuum_freeze_max_age: None,
            autovacuum_freeze_table_age: None,
            autovacuum_multixact_freeze_min_age: None,
            autovacuum_multixact_freeze_max_age: None,
            autovacuum_multixact_freeze_table_age: None,
            log_autovacuum_min_duration: None,
            user_catalog_table: None,
        }
    }

    /// Set CHECK OPTION
    #[must_use]
    pub const fn check_option(self, option: &'static str) -> Self {
        Self {
            check_option: Some(option),
            ..self
        }
    }

    /// Set security barrier
    #[must_use]
    pub const fn security_barrier(self) -> Self {
        Self {
            security_barrier: true,
            ..self
        }
    }

    /// Set security invoker
    #[must_use]
    pub const fn security_invoker(self) -> Self {
        Self {
            security_invoker: true,
            ..self
        }
    }

    /// Set fillfactor (for materialized views)
    #[must_use]
    pub const fn fillfactor(self, value: i32) -> Self {
        Self {
            fillfactor: Some(value),
            ..self
        }
    }

    /// Set toast tuple target (for materialized views)
    #[must_use]
    pub const fn toast_tuple_target(self, value: i32) -> Self {
        Self {
            toast_tuple_target: Some(value),
            ..self
        }
    }

    /// Set parallel workers (for materialized views)
    #[must_use]
    pub const fn parallel_workers(self, value: i32) -> Self {
        Self {
            parallel_workers: Some(value),
            ..self
        }
    }

    /// Set autovacuum enabled (for materialized views)
    #[must_use]
    pub const fn autovacuum_enabled(self, value: bool) -> Self {
        Self {
            autovacuum_enabled: Some(value),
            ..self
        }
    }

    /// Set vacuum index cleanup (for materialized views): "auto", "on", or "off"
    #[must_use]
    pub const fn vacuum_index_cleanup(self, value: &'static str) -> Self {
        Self {
            vacuum_index_cleanup: Some(value),
            ..self
        }
    }

    /// Set vacuum truncate (for materialized views)
    #[must_use]
    pub const fn vacuum_truncate(self, value: bool) -> Self {
        Self {
            vacuum_truncate: Some(value),
            ..self
        }
    }

    /// Set autovacuum vacuum threshold (for materialized views)
    #[must_use]
    pub const fn autovacuum_vacuum_threshold(self, value: i32) -> Self {
        Self {
            autovacuum_vacuum_threshold: Some(value),
            ..self
        }
    }

    /// Set autovacuum vacuum scale factor (for materialized views)
    #[must_use]
    pub const fn autovacuum_vacuum_scale_factor(self, value: i32) -> Self {
        Self {
            autovacuum_vacuum_scale_factor: Some(value),
            ..self
        }
    }

    /// Set autovacuum vacuum cost delay (for materialized views)
    #[must_use]
    pub const fn autovacuum_vacuum_cost_delay(self, value: i32) -> Self {
        Self {
            autovacuum_vacuum_cost_delay: Some(value),
            ..self
        }
    }

    /// Set autovacuum vacuum cost limit (for materialized views)
    #[must_use]
    pub const fn autovacuum_vacuum_cost_limit(self, value: i32) -> Self {
        Self {
            autovacuum_vacuum_cost_limit: Some(value),
            ..self
        }
    }

    /// Set autovacuum freeze min age (for materialized views)
    #[must_use]
    pub const fn autovacuum_freeze_min_age(self, value: i64) -> Self {
        Self {
            autovacuum_freeze_min_age: Some(value),
            ..self
        }
    }

    /// Set autovacuum freeze max age (for materialized views)
    #[must_use]
    pub const fn autovacuum_freeze_max_age(self, value: i64) -> Self {
        Self {
            autovacuum_freeze_max_age: Some(value),
            ..self
        }
    }

    /// Set autovacuum freeze table age (for materialized views)
    #[must_use]
    pub const fn autovacuum_freeze_table_age(self, value: i64) -> Self {
        Self {
            autovacuum_freeze_table_age: Some(value),
            ..self
        }
    }

    /// Set autovacuum multixact freeze min age (for materialized views)
    #[must_use]
    pub const fn autovacuum_multixact_freeze_min_age(self, value: i64) -> Self {
        Self {
            autovacuum_multixact_freeze_min_age: Some(value),
            ..self
        }
    }

    /// Set autovacuum multixact freeze max age (for materialized views)
    #[must_use]
    pub const fn autovacuum_multixact_freeze_max_age(self, value: i64) -> Self {
        Self {
            autovacuum_multixact_freeze_max_age: Some(value),
            ..self
        }
    }

    /// Set autovacuum multixact freeze table age (for materialized views)
    #[must_use]
    pub const fn autovacuum_multixact_freeze_table_age(self, value: i64) -> Self {
        Self {
            autovacuum_multixact_freeze_table_age: Some(value),
            ..self
        }
    }

    /// Set log autovacuum min duration (for materialized views)
    #[must_use]
    pub const fn log_autovacuum_min_duration(self, value: i32) -> Self {
        Self {
            log_autovacuum_min_duration: Some(value),
            ..self
        }
    }

    /// Set user catalog table (for materialized views)
    #[must_use]
    pub const fn user_catalog_table(self, value: bool) -> Self {
        Self {
            user_catalog_table: Some(value),
            ..self
        }
    }

    /// Convert to runtime type
    #[must_use]
    pub const fn into_view_with_option(self) -> ViewWithOption {
        ViewWithOption {
            check_option: match self.check_option {
                Some(s) => Some(Cow::Borrowed(s)),
                None => None,
            },
            security_barrier: if self.security_barrier {
                Some(true)
            } else {
                None
            },
            security_invoker: if self.security_invoker {
                Some(true)
            } else {
                None
            },
            fillfactor: self.fillfactor,
            toast_tuple_target: self.toast_tuple_target,
            parallel_workers: self.parallel_workers,
            autovacuum_enabled: self.autovacuum_enabled,
            vacuum_index_cleanup: match self.vacuum_index_cleanup {
                Some(s) => Some(Cow::Borrowed(s)),
                None => None,
            },
            vacuum_truncate: self.vacuum_truncate,
            autovacuum_vacuum_threshold: self.autovacuum_vacuum_threshold,
            autovacuum_vacuum_scale_factor: self.autovacuum_vacuum_scale_factor,
            autovacuum_vacuum_cost_delay: self.autovacuum_vacuum_cost_delay,
            autovacuum_vacuum_cost_limit: self.autovacuum_vacuum_cost_limit,
            autovacuum_freeze_min_age: self.autovacuum_freeze_min_age,
            autovacuum_freeze_max_age: self.autovacuum_freeze_max_age,
            autovacuum_freeze_table_age: self.autovacuum_freeze_table_age,
            autovacuum_multixact_freeze_min_age: self.autovacuum_multixact_freeze_min_age,
            autovacuum_multixact_freeze_max_age: self.autovacuum_multixact_freeze_max_age,
            autovacuum_multixact_freeze_table_age: self.autovacuum_multixact_freeze_table_age,
            log_autovacuum_min_duration: self.log_autovacuum_min_duration,
            user_catalog_table: self.user_catalog_table,
        }
    }
}

impl Default for ViewWithOptionDef {
    fn default() -> Self {
        Self::new()
    }
}

/// Runtime view WITH options entity
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct ViewWithOption {
    /// CHECK OPTION ('local' | 'cascaded')
    #[cfg_attr(
        feature = "serde",
        serde(
            skip_serializing_if = "Option::is_none",
            deserialize_with = "cow_option_from_string"
        )
    )]
    pub check_option: Option<Cow<'static, str>>,

    /// Security barrier flag
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub security_barrier: Option<bool>,

    /// Security invoker flag
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub security_invoker: Option<bool>,

    /// Fillfactor (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub fillfactor: Option<i32>,

    /// Toast tuple target (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub toast_tuple_target: Option<i32>,

    /// Parallel workers (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub parallel_workers: Option<i32>,

    /// Autovacuum enabled (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_enabled: Option<bool>,

    /// Vacuum index cleanup (for materialized views): 'auto' | 'on' | 'off'
    #[cfg_attr(
        feature = "serde",
        serde(
            skip_serializing_if = "Option::is_none",
            deserialize_with = "cow_option_from_string"
        )
    )]
    pub vacuum_index_cleanup: Option<Cow<'static, str>>,

    /// Vacuum truncate (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub vacuum_truncate: Option<bool>,

    /// Autovacuum vacuum threshold (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_vacuum_threshold: Option<i32>,

    /// Autovacuum vacuum scale factor (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_vacuum_scale_factor: Option<i32>,

    /// Autovacuum vacuum cost delay (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_vacuum_cost_delay: Option<i32>,

    /// Autovacuum vacuum cost limit (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_vacuum_cost_limit: Option<i32>,

    /// Autovacuum freeze min age (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_freeze_min_age: Option<i64>,

    /// Autovacuum freeze max age (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_freeze_max_age: Option<i64>,

    /// Autovacuum freeze table age (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_freeze_table_age: Option<i64>,

    /// Autovacuum multixact freeze min age (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_multixact_freeze_min_age: Option<i64>,

    /// Autovacuum multixact freeze max age (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_multixact_freeze_max_age: Option<i64>,

    /// Autovacuum multixact freeze table age (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub autovacuum_multixact_freeze_table_age: Option<i64>,

    /// Log autovacuum min duration (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub log_autovacuum_min_duration: Option<i32>,

    /// User catalog table (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub user_catalog_table: Option<bool>,
}

impl Default for ViewWithOption {
    fn default() -> Self {
        ViewWithOptionDef::new().into_view_with_option()
    }
}

impl From<ViewWithOptionDef> for ViewWithOption {
    fn from(def: ViewWithOptionDef) -> Self {
        def.into_view_with_option()
    }
}

// =============================================================================
// Const-friendly Definition Type
// =============================================================================

/// Const-friendly view definition
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ViewDef {
    /// Schema name
    pub schema: &'static str,
    /// View name
    pub name: &'static str,
    /// View definition (AS SELECT ...)
    pub definition: Option<&'static str>,
    /// Is this a materialized view?
    pub materialized: bool,
    /// WITH options
    pub with: Option<ViewWithOptionDef>,
    /// Whether this is an existing view (not managed by drizzle)
    pub is_existing: bool,
    /// WITH NO DATA (for materialized views)
    pub with_no_data: bool,
    /// USING clause (for materialized views)
    pub using: Option<&'static str>,
    /// Tablespace (for materialized views)
    pub tablespace: Option<&'static str>,
}

impl ViewDef {
    /// Create a new view definition
    #[must_use]
    pub const fn new(schema: &'static str, name: &'static str) -> Self {
        Self {
            schema,
            name,
            definition: None,
            materialized: false,
            with: None,
            is_existing: false,
            with_no_data: false,
            using: None,
            tablespace: None,
        }
    }

    /// Set the view definition
    #[must_use]
    pub const fn definition(self, sql: &'static str) -> Self {
        Self {
            definition: Some(sql),
            ..self
        }
    }

    /// Mark as materialized view
    #[must_use]
    pub const fn materialized(self) -> Self {
        Self {
            materialized: true,
            ..self
        }
    }

    /// Set WITH options
    #[must_use]
    pub const fn with_options(self, options: ViewWithOptionDef) -> Self {
        Self {
            with: Some(options),
            ..self
        }
    }

    /// Mark as existing (not managed by drizzle)
    #[must_use]
    pub const fn existing(self) -> Self {
        Self {
            is_existing: true,
            ..self
        }
    }

    /// Set WITH NO DATA
    #[must_use]
    pub const fn with_no_data(self) -> Self {
        Self {
            with_no_data: true,
            ..self
        }
    }

    /// Set USING clause
    #[must_use]
    pub const fn using(self, clause: &'static str) -> Self {
        Self {
            using: Some(clause),
            ..self
        }
    }

    /// Set tablespace
    #[must_use]
    pub const fn tablespace(self, space: &'static str) -> Self {
        Self {
            tablespace: Some(space),
            ..self
        }
    }

    /// Convert to runtime [`View`] type
    ///
    /// Note: This method cannot be const because it needs to convert nested Option types
    /// (with options) which require runtime method calls.
    #[must_use]
    pub fn into_view(self) -> View {
        View {
            schema: Cow::Borrowed(self.schema),
            name: Cow::Borrowed(self.name),
            definition: self.definition.map(Cow::Borrowed),
            materialized: self.materialized,
            with: self.with.map(ViewWithOptionDef::into_view_with_option),
            is_existing: self.is_existing,
            with_no_data: if self.with_no_data { Some(true) } else { None },
            using: self.using.map(Cow::Borrowed),
            tablespace: self.tablespace.map(Cow::Borrowed),
        }
    }
}

impl Default for ViewDef {
    fn default() -> Self {
        Self::new("public", "")
    }
}

// =============================================================================
// Runtime Type for Serde
// =============================================================================

/// Runtime view entity
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct View {
    /// Schema name
    #[cfg_attr(feature = "serde", serde(deserialize_with = "cow_from_string"))]
    pub schema: Cow<'static, str>,

    /// View name
    #[cfg_attr(feature = "serde", serde(deserialize_with = "cow_from_string"))]
    pub name: Cow<'static, str>,

    /// View definition (AS SELECT ...)
    #[cfg_attr(
        feature = "serde",
        serde(
            default,
            skip_serializing_if = "Option::is_none",
            deserialize_with = "cow_option_from_string"
        )
    )]
    pub definition: Option<Cow<'static, str>>,

    /// Is this a materialized view?
    #[cfg_attr(feature = "serde", serde(default))]
    pub materialized: bool,

    /// WITH options
    #[cfg_attr(
        feature = "serde",
        serde(skip_serializing_if = "Option::is_none", rename = "with")
    )]
    pub with: Option<ViewWithOption>,

    /// Whether this is an existing view (not managed by drizzle)
    #[cfg_attr(feature = "serde", serde(default))]
    pub is_existing: bool,

    /// WITH NO DATA (for materialized views)
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub with_no_data: Option<bool>,

    /// USING clause (for materialized views)
    #[cfg_attr(
        feature = "serde",
        serde(
            skip_serializing_if = "Option::is_none",
            deserialize_with = "cow_option_from_string"
        )
    )]
    pub using: Option<Cow<'static, str>>,

    /// Tablespace (for materialized views)
    #[cfg_attr(
        feature = "serde",
        serde(
            skip_serializing_if = "Option::is_none",
            deserialize_with = "cow_option_from_string"
        )
    )]
    pub tablespace: Option<Cow<'static, str>>,
}

impl View {
    /// Create a new view
    #[must_use]
    pub fn new(schema: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self {
        Self {
            schema: schema.into(),
            name: name.into(),
            definition: None,
            materialized: false,
            with: None,
            is_existing: false,
            with_no_data: None,
            using: None,
            tablespace: None,
        }
    }

    /// Get the schema name
    #[inline]
    #[must_use]
    pub fn schema(&self) -> &str {
        &self.schema
    }

    /// Get the view name
    #[inline]
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }
}

impl Default for View {
    fn default() -> Self {
        Self::new("public", "")
    }
}

impl From<ViewDef> for View {
    fn from(def: ViewDef) -> Self {
        def.into_view()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_const_view_def() {
        const VIEW: ViewDef = ViewDef::new("public", "active_users")
            .definition("SELECT * FROM users WHERE active = 1");

        assert_eq!(VIEW.name, "active_users");
        assert_eq!(VIEW.schema, "public");
    }

    #[test]
    fn test_materialized_view_def() {
        const MAT_VIEW: ViewDef = ViewDef::new("public", "user_stats")
            .materialized()
            .with_no_data();

        const {
            assert!(MAT_VIEW.materialized);
        }
    }

    #[test]
    fn test_view_def_to_view() {
        const DEF: ViewDef = ViewDef::new("public", "view").definition("SELECT 1");
        let view = DEF.into_view();
        assert_eq!(view.name(), "view");
        assert_eq!(view.schema(), "public");
    }

    #[test]
    fn test_view_with_option_def_builders() {
        // Test const builder methods for materialized view options
        const OPTIONS: ViewWithOptionDef = ViewWithOptionDef::new()
            .fillfactor(80)
            .parallel_workers(4)
            .autovacuum_enabled(true)
            .vacuum_index_cleanup("auto")
            .vacuum_truncate(false)
            .autovacuum_vacuum_threshold(100)
            .autovacuum_vacuum_scale_factor(20)
            .autovacuum_vacuum_cost_delay(10)
            .autovacuum_vacuum_cost_limit(200)
            .autovacuum_freeze_min_age(50_000_000)
            .autovacuum_freeze_max_age(200_000_000)
            .autovacuum_freeze_table_age(150_000_000)
            .autovacuum_multixact_freeze_min_age(5_000_000)
            .autovacuum_multixact_freeze_max_age(400_000_000)
            .autovacuum_multixact_freeze_table_age(150_000_000)
            .log_autovacuum_min_duration(1000)
            .user_catalog_table(false)
            .toast_tuple_target(128);

        assert_eq!(OPTIONS.fillfactor, Some(80));
        assert_eq!(OPTIONS.parallel_workers, Some(4));
        assert_eq!(OPTIONS.autovacuum_enabled, Some(true));
        assert_eq!(OPTIONS.vacuum_index_cleanup, Some("auto"));
        assert_eq!(OPTIONS.vacuum_truncate, Some(false));
        assert_eq!(OPTIONS.autovacuum_vacuum_threshold, Some(100));
        assert_eq!(OPTIONS.autovacuum_vacuum_scale_factor, Some(20));
        assert_eq!(OPTIONS.autovacuum_vacuum_cost_delay, Some(10));
        assert_eq!(OPTIONS.autovacuum_vacuum_cost_limit, Some(200));
        assert_eq!(OPTIONS.autovacuum_freeze_min_age, Some(50_000_000));
        assert_eq!(OPTIONS.autovacuum_freeze_max_age, Some(200_000_000));
        assert_eq!(OPTIONS.autovacuum_freeze_table_age, Some(150_000_000));
        assert_eq!(OPTIONS.autovacuum_multixact_freeze_min_age, Some(5_000_000));
        assert_eq!(
            OPTIONS.autovacuum_multixact_freeze_max_age,
            Some(400_000_000)
        );
        assert_eq!(
            OPTIONS.autovacuum_multixact_freeze_table_age,
            Some(150_000_000)
        );
        assert_eq!(OPTIONS.log_autovacuum_min_duration, Some(1000));
        assert_eq!(OPTIONS.user_catalog_table, Some(false));
        assert_eq!(OPTIONS.toast_tuple_target, Some(128));
    }

    #[test]
    fn test_view_with_option_def_to_runtime() {
        const OPTIONS: ViewWithOptionDef = ViewWithOptionDef::new()
            .fillfactor(90)
            .security_barrier()
            .security_invoker()
            .check_option("cascaded");

        let runtime = OPTIONS.into_view_with_option();
        assert_eq!(runtime.fillfactor, Some(90));
        assert_eq!(runtime.security_barrier, Some(true));
        assert_eq!(runtime.security_invoker, Some(true));
        assert_eq!(runtime.check_option.as_deref(), Some("cascaded"));
    }

    #[test]
    fn test_materialized_view_with_all_options() {
        const MAT_VIEW: ViewDef = ViewDef::new("analytics", "monthly_sales")
            .materialized()
            .with_no_data()
            .using("btree")
            .tablespace("fast_ssd")
            .with_options(ViewWithOptionDef::new().fillfactor(90).parallel_workers(2))
            .definition("SELECT * FROM sales WHERE date > now() - interval '30 days'");

        const {
            assert!(MAT_VIEW.materialized);
        }
        const {
            assert!(MAT_VIEW.with_no_data);
        }
        assert_eq!(MAT_VIEW.using, Some("btree"));
        assert_eq!(MAT_VIEW.tablespace, Some("fast_ssd"));
        assert!(MAT_VIEW.with.is_some());

        let options = MAT_VIEW.with.unwrap();
        assert_eq!(options.fillfactor, Some(90));
        assert_eq!(options.parallel_workers, Some(2));
    }
}