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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
//! Database description. This crate is used heavily in the schema engine.

#![deny(rust_2018_idioms, unsafe_code)]
#![allow(clippy::derive_partial_eq_without_eq)]

pub mod mssql;
pub mod mysql;
pub mod postgres;
pub mod sqlite;
pub mod walkers;

mod connector_data;
mod error;
mod getters;
mod ids;
mod parsers;

pub use self::{
    error::{DescriberError, DescriberErrorKind, DescriberResult},
    ids::*,
    walkers::*,
};
pub use either::Either;
pub use prisma_value::PrismaValue;

use enumflags2::{BitFlag, BitFlags};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{
    any::Any,
    fmt::{self, Debug},
};

/// A database description connector.
#[async_trait::async_trait]
pub trait SqlSchemaDescriberBackend: Send + Sync {
    /// List the database's schemas.
    async fn list_databases(&self) -> DescriberResult<Vec<String>>;

    /// Get the databases metadata.
    async fn get_metadata(&self, schema: &str) -> DescriberResult<SqlMetadata>;

    /// Describe a database schema.
    async fn describe(&self, schemas: &[&str]) -> DescriberResult<SqlSchema>;

    /// Get the database version.
    async fn version(&self) -> DescriberResult<Option<String>>;
}

/// The return type of get_metadata().
pub struct SqlMetadata {
    pub table_count: usize,
    pub size_in_bytes: usize,
}

/// The result of describing a database schema.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SqlSchema {
    /// Namespaces (schemas)
    namespaces: Vec<String>,
    /// The schema's tables.
    tables: Vec<Table>,
    /// The schema's enums.
    enums: Vec<Enum>,
    enum_variants: Vec<EnumVariant>,
    /// The schema's columns that are in tables.
    table_columns: Vec<(TableId, Column)>,
    /// All foreign keys.
    foreign_keys: Vec<ForeignKey>,
    /// All table column default values.
    table_default_values: Vec<(TableColumnId, DefaultValue)>,
    /// All view column default values.
    view_default_values: Vec<(ViewColumnId, DefaultValue)>,
    /// Constrained and referenced columns of foreign keys.
    foreign_key_columns: Vec<ForeignKeyColumn>,
    /// All indexes and unique constraints.
    indexes: Vec<Index>,
    /// All columns of indexes.
    index_columns: Vec<IndexColumn>,
    /// Check constraints for every table.
    check_constraints: Vec<(TableId, String)>,
    /// The schema's views,
    views: Vec<View>,
    /// The schema's columns that are in views.
    view_columns: Vec<(ViewId, Column)>,
    /// The stored procedures.
    procedures: Vec<Procedure>,
    /// The user-defined types procedures.
    user_defined_types: Vec<UserDefinedType>,
    /// Connector-specific data
    connector_data: connector_data::ConnectorData,
}

impl SqlSchema {
    /// Extract connector-specific constructs. The type parameter must be the right one.
    #[track_caller]
    pub fn downcast_connector_data<T: 'static>(&self) -> &T {
        self.connector_data.data.as_ref().unwrap().downcast_ref().unwrap()
    }

    /// The id of the next column
    pub fn next_table_column_id(&self) -> TableColumnId {
        TableColumnId(self.table_columns.len() as u32)
    }

    /// The id of the next column
    pub fn next_view_column_id(&self) -> ViewColumnId {
        ViewColumnId(self.view_columns.len() as u32)
    }

    /// The given enum is used in tables.
    pub fn enum_used_in_tables(&self, id: EnumId) -> bool {
        self.table_columns
            .iter()
            .any(|col| col.1.tpe.family == ColumnTypeFamily::Enum(id))
    }

    /// Extract connector-specific constructs mutably. The type parameter must be the right one.
    #[track_caller]
    pub fn downcast_connector_data_mut<T: 'static>(&mut self) -> &mut T {
        self.connector_data.data.as_mut().unwrap().downcast_mut().unwrap()
    }

    /// Remove all namespaces from the schema.
    pub fn clear_namespaces(&mut self) {
        self.namespaces.clear();
    }

    /// Insert connector-specific data into the schema. This will replace existing connector data.
    pub fn set_connector_data(&mut self, data: Box<dyn Any + Send + Sync>) {
        self.connector_data.data = Some(data);
    }

    /// Get a view.
    pub fn get_view(&self, name: &str) -> Option<&View> {
        self.views.iter().find(|v| v.name == name)
    }

    /// Try to find an enum by name.
    pub fn find_enum(&self, name: &str, namespace: Option<&str>) -> Option<EnumId> {
        let ns_id = namespace.and_then(|ns| self.get_namespace(ns));

        self.enums
            .iter()
            .position(|e| e.name == name && ns_id.map(|id| id == e.namespace_id).unwrap_or(true))
            .map(|i| EnumId(i as u32))
    }

    fn get_namespace(&self, name: &str) -> Option<NamespaceId> {
        self.namespaces
            .iter()
            .position(|ns| ns == name)
            .map(|i| NamespaceId(i as u32))
    }

    /// Try to find a table by name.
    pub fn find_table(&self, name: &str, namespace: Option<&str>) -> Option<TableId> {
        let ns_id = namespace.and_then(|ns| self.get_namespace(ns));

        self.tables
            .iter()
            .position(|t| t.name == name && ns_id.map(|id| id == t.namespace_id).unwrap_or(true))
            .map(|i| TableId(i as u32))
    }

    /// Try to find a table by name.
    pub fn find_view(&self, name: &str, namespace: Option<&str>) -> Option<ViewId> {
        let ns_id = namespace.and_then(|ns| self.get_namespace(ns));

        self.views
            .iter()
            .position(|t| t.name == name && ns_id.map(|id| id == t.namespace_id).unwrap_or(true))
            .map(|i| ViewId(i as u32))
    }

    /// Get a procedure.
    pub fn get_procedure(&self, name: &str) -> Option<&Procedure> {
        self.procedures.iter().find(|x| x.name == name)
    }

    /// Get a user defined type by name.
    pub fn get_user_defined_type(&self, name: &str) -> Option<&UserDefinedType> {
        self.user_defined_types.iter().find(|x| x.name == name)
    }

    /// Find a namespace by name.
    pub fn get_namespace_id(&self, name: &str) -> Option<NamespaceId> {
        self.namespaces
            .binary_search_by(|ns_name| ns_name.as_str().cmp(name))
            .ok()
            .map(|pos| NamespaceId(pos as u32))
    }

    /// The total number of indexes in the schema.
    pub fn indexes_count(&self) -> usize {
        self.indexes.len()
    }

    /// Make all fulltext indexes non-fulltext, for the preview feature's purpose.
    pub fn make_fulltext_indexes_normal(&mut self) {
        for idx in self.indexes.iter_mut() {
            if matches!(idx.tpe, IndexType::Fulltext) {
                idx.tpe = IndexType::Normal;
            }
        }
    }

    /// Add a table column to the schema.
    pub fn push_table_column(&mut self, table_id: TableId, column: Column) -> TableColumnId {
        let id = TableColumnId(self.table_columns.len() as u32);
        self.table_columns.push((table_id, column));
        id
    }

    /// Add a view column to the schema.
    pub fn push_view_column(&mut self, view_id: ViewId, column: Column) -> ViewColumnId {
        let id = ViewColumnId(self.view_columns.len() as u32);
        self.view_columns.push((view_id, column));
        id
    }

    /// Add an enum to the schema.
    pub fn push_enum(&mut self, namespace_id: NamespaceId, enum_name: String, description: Option<String>) -> EnumId {
        let id = EnumId(self.enums.len() as u32);

        self.enums.push(Enum {
            namespace_id,
            name: enum_name,
            description,
        });

        id
    }

    /// Add a variant to an enum.
    pub fn push_enum_variant(&mut self, enum_id: EnumId, variant_name: String) -> EnumVariantId {
        let id = EnumVariantId(self.enum_variants.len() as u32);
        self.enum_variants.push(EnumVariant { enum_id, variant_name });
        id
    }

    /// Add a fulltext index to the schema.
    pub fn push_fulltext_index(&mut self, table_id: TableId, index_name: String) -> IndexId {
        let id = IndexId(self.indexes.len() as u32);
        self.indexes.push(Index {
            table_id,
            index_name,
            tpe: IndexType::Fulltext,
        });
        id
    }

    /// Add an index to the schema.
    pub fn push_index(&mut self, table_id: TableId, index_name: String) -> IndexId {
        let id = IndexId(self.indexes.len() as u32);
        self.indexes.push(Index {
            table_id,
            index_name,
            tpe: IndexType::Normal,
        });
        id
    }

    /// Add table default value to the schema.
    pub fn push_table_default_value(&mut self, column_id: TableColumnId, value: DefaultValue) -> TableDefaultValueId {
        let id = TableDefaultValueId(self.table_default_values.len() as u32);
        self.table_default_values.push((column_id, value));
        id
    }

    /// Add table default value to the schema.
    pub fn push_view_default_value(&mut self, column_id: ViewColumnId, value: DefaultValue) -> ViewDefaultValueId {
        let id = ViewDefaultValueId(self.view_default_values.len() as u32);
        self.view_default_values.push((column_id, value));
        id
    }

    /// Add a primary key to the schema.
    pub fn push_primary_key(&mut self, table_id: TableId, index_name: String) -> IndexId {
        let id = IndexId(self.indexes.len() as u32);
        self.indexes.push(Index {
            table_id,
            index_name,
            tpe: IndexType::PrimaryKey,
        });
        id
    }

    /// Add a unique constraint/index to the schema.
    pub fn push_unique_constraint(&mut self, table_id: TableId, index_name: String) -> IndexId {
        let id = IndexId(self.indexes.len() as u32);
        self.indexes.push(Index {
            table_id,
            index_name,
            tpe: IndexType::Unique,
        });
        id
    }

    pub fn push_index_column(&mut self, column: IndexColumn) -> IndexColumnId {
        let id = IndexColumnId(self.index_columns.len() as u32);
        self.index_columns.push(column);
        id
    }

    pub fn push_foreign_key(
        &mut self,
        constraint_name: Option<String>,
        [constrained_table, referenced_table]: [TableId; 2],
        [on_delete_action, on_update_action]: [ForeignKeyAction; 2],
    ) -> ForeignKeyId {
        let id = ForeignKeyId(self.foreign_keys.len() as u32);
        self.foreign_keys.push(ForeignKey {
            constrained_table,
            constraint_name,
            referenced_table,
            on_delete_action,
            on_update_action,
        });
        id
    }

    pub fn push_foreign_key_column(
        &mut self,
        foreign_key_id: ForeignKeyId,
        [constrained_column, referenced_column]: [TableColumnId; 2],
    ) {
        self.foreign_key_columns.push(ForeignKeyColumn {
            foreign_key_id,
            constrained_column,
            referenced_column,
        });
    }

    pub fn push_namespace(&mut self, name: String) -> NamespaceId {
        let id = NamespaceId(self.namespaces.len() as u32);
        self.namespaces.push(name);
        id
    }

    pub fn push_table(&mut self, name: String, namespace_id: NamespaceId, description: Option<String>) -> TableId {
        let id = TableId(self.tables.len() as u32);

        self.tables.push(Table {
            namespace_id,
            name,
            properties: TableProperties::empty(),
            description,
        });

        id
    }

    pub fn push_view(
        &mut self,
        name: String,
        namespace_id: NamespaceId,
        definition: Option<String>,
        description: Option<String>,
    ) -> ViewId {
        let id = ViewId(self.views.len() as u32);

        self.views.push(View {
            namespace_id,
            name,
            definition,
            description,
        });

        id
    }

    pub fn push_table_with_properties(
        &mut self,
        name: String,
        namespace_id: NamespaceId,
        properties: BitFlags<TableProperties>,
        description: Option<String>,
    ) -> TableId {
        let id = TableId(self.tables.len() as u32);

        self.tables.push(Table {
            namespace_id,
            name,
            properties,
            description,
        });

        id
    }

    pub fn namespaces_count(&self) -> usize {
        self.namespaces.len()
    }

    pub fn namespace_walker<'a>(&'a self, name: &str) -> Option<NamespaceWalker<'a>> {
        let namespace_idx = self.namespaces.iter().position(|ns| ns == name)?;
        Some(self.walk(NamespaceId(namespace_idx as u32)))
    }

    pub fn tables_count(&self) -> usize {
        self.tables.len()
    }

    pub fn views_count(&self) -> usize {
        self.views.len()
    }

    pub fn table_walker<'a>(&'a self, name: &str) -> Option<TableWalker<'a>> {
        let table_idx = self.tables.iter().position(|table| table.name == name)?;
        Some(self.walk(TableId(table_idx as u32)))
    }

    pub fn table_walker_ns<'a>(&'a self, namespace: &str, name: &str) -> Option<TableWalker<'a>> {
        let namespace_idx = self.namespace_walker(namespace)?.id;

        let table_idx = self
            .tables
            .iter()
            .position(|table| table.name == name && table.namespace_id == namespace_idx)?;

        Some(self.walk(TableId(table_idx as u32)))
    }

    pub fn view_walker<'a>(&'a self, name: &str) -> Option<ViewWalker<'a>> {
        let view_idx = self.views.iter().position(|view| view.name == name)?;
        Some(self.walk(ViewId(view_idx as u32)))
    }

    pub fn view_walker_ns<'a>(&'a self, namespace: &str, name: &str) -> Option<ViewWalker<'a>> {
        let namespace_idx = self.namespace_walker(namespace)?.id;

        let view_idx = self
            .views
            .iter()
            .position(|view| view.name == name && view.namespace_id == namespace_idx)?;

        Some(self.walk(ViewId(view_idx as u32)))
    }

    pub fn table_walkers(&self) -> impl ExactSizeIterator<Item = TableWalker<'_>> {
        (0..self.tables.len()).map(move |table_index| self.walk(TableId(table_index as u32)))
    }

    pub fn view_walkers(&self) -> impl ExactSizeIterator<Item = ViewWalker<'_>> {
        (0..self.views.len()).map(move |view_index| self.walk(ViewId(view_index as u32)))
    }

    pub fn udt_walkers(&self) -> impl Iterator<Item = UserDefinedTypeWalker<'_>> {
        (0..self.user_defined_types.len()).map(move |udt_index| self.walk(UdtId(udt_index as u32)))
    }

    pub fn enum_walkers(&self) -> impl ExactSizeIterator<Item = EnumWalker<'_>> {
        (0..self.enums.len()).map(move |enum_index| self.walk(EnumId(enum_index as u32)))
    }

    pub fn walk_foreign_keys(&self) -> impl Iterator<Item = ForeignKeyWalker<'_>> {
        (0..self.foreign_keys.len()).map(move |fk_idx| ForeignKeyWalker {
            schema: self,
            id: ForeignKeyId(fk_idx as u32),
        })
    }

    /// Traverse a schema item by id.
    pub fn walk<I>(&self, id: I) -> Walker<'_, I> {
        Walker { id, schema: self }
    }

    /// Traverse all the table columns in the schema.
    pub fn walk_table_columns(&self) -> impl Iterator<Item = TableColumnWalker<'_>> {
        (0..self.table_columns.len()).map(|idx| self.walk(TableColumnId(idx as u32)))
    }

    /// Traverse all the table columns in the schema.
    pub fn walk_view_columns(&self) -> impl Iterator<Item = ViewColumnWalker<'_>> {
        (0..self.view_columns.len()).map(|idx| self.walk(ViewColumnId(idx as u32)))
    }

    /// Traverse all namespaces in the catalog.
    pub fn walk_namespaces(&self) -> impl ExactSizeIterator<Item = NamespaceWalker<'_>> {
        (0..self.namespaces.len()).map(|idx| self.walk(NamespaceId(idx as u32)))
    }

    /// No tables or enums in the catalog.
    pub fn is_empty(&self) -> bool {
        self.tables.is_empty() && self.enums.is_empty()
    }
}

#[enumflags2::bitflags]
#[repr(u8)]
#[derive(Clone, Copy, Debug)]
pub enum TableProperties {
    IsPartition,
    HasSubclass,
    HasRowLevelSecurity,
}

/// A table found in a schema.
#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
pub struct Table {
    namespace_id: NamespaceId,
    name: String,
    properties: BitFlags<TableProperties>,
    description: Option<String>,
}

/// The type of an index.
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
pub enum IndexType {
    /// Unique type.
    Unique,
    /// Normal type.
    Normal,
    /// Fulltext type.
    Fulltext,
    /// The table's primary key
    PrimaryKey,
}

/// The sort order of an index.
#[derive(Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
pub enum SQLSortOrder {
    Asc,
    Desc,
}

impl Default for SQLSortOrder {
    fn default() -> Self {
        Self::Asc
    }
}

impl AsRef<str> for SQLSortOrder {
    fn as_ref(&self) -> &str {
        match self {
            SQLSortOrder::Asc => "ASC",
            SQLSortOrder::Desc => "DESC",
        }
    }
}

impl fmt::Display for SQLSortOrder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_ref())
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct IndexColumn {
    pub index_id: IndexId,
    pub column_id: TableColumnId,
    pub sort_order: Option<SQLSortOrder>,
    pub length: Option<u32>,
}

/// An index on a table.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Index {
    table_id: TableId,
    index_name: String,
    tpe: IndexType,
}

/// A stored procedure (like, the function inside your database).
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Procedure {
    ///Namespace of the procedure
    namespace_id: NamespaceId,
    /// Procedure name.
    pub name: String,
    /// The definition of the procedure.
    pub definition: Option<String>,
}

/// A user-defined type. Can map to another type, or be declared as assembly.
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct UserDefinedType {
    ///Namespace of the procedure
    namespace_id: NamespaceId,
    /// Type name
    pub name: String,
    /// Type mapping
    pub definition: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Column {
    /// Column name.
    pub name: String,
    /// Column type.
    pub tpe: ColumnType,
    /// Is the column auto-incrementing?
    pub auto_increment: bool,
    /// The comment in the database
    pub description: Option<String>,
}

/// The type of a column.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ColumnType {
    /// The full SQL data type, the sql string necessary to recreate the column, drawn directly from the db, used when there is no native type.
    pub full_data_type: String,
    /// The family of the raw type.
    pub family: ColumnTypeFamily,
    /// The arity of the column.
    pub arity: ColumnArity,
    /// The Native type of the column.
    #[serde(skip)]
    pub native_type: Option<psl::datamodel_connector::NativeTypeInstance>,
}

impl ColumnType {
    pub fn pure(family: ColumnTypeFamily, arity: ColumnArity) -> Self {
        ColumnType {
            full_data_type: "".to_string(),
            family,
            arity,
            native_type: None,
        }
    }

    pub fn with_full_data_type(family: ColumnTypeFamily, arity: ColumnArity, full_data_type: String) -> Self {
        ColumnType {
            full_data_type,
            family,
            arity,
            native_type: None,
        }
    }
}

/// Enumeration of column type families.
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum ColumnTypeFamily {
    /// Integer types.
    Int,
    /// BigInt types.
    BigInt,
    /// Floating point types.
    Float,
    /// Decimal Types.
    Decimal,
    /// Boolean types.
    Boolean,
    /// String types.
    String,
    /// DateTime types.
    DateTime,
    /// Binary types.
    Binary,
    /// JSON types.
    Json,
    /// UUID types.
    Uuid,
    ///Enum
    Enum(EnumId),
    /// Unsupported
    Unsupported(String),
}

impl ColumnTypeFamily {
    pub fn as_enum(&self) -> Option<EnumId> {
        match self {
            ColumnTypeFamily::Enum(id) => Some(*id),
            _ => None,
        }
    }

    pub fn is_bigint(&self) -> bool {
        matches!(self, ColumnTypeFamily::BigInt)
    }

    pub fn is_boolean(&self) -> bool {
        matches!(self, ColumnTypeFamily::Boolean)
    }

    pub fn is_datetime(&self) -> bool {
        matches!(self, ColumnTypeFamily::DateTime)
    }

    pub fn is_enum(&self) -> bool {
        matches!(self, ColumnTypeFamily::Enum(_))
    }

    pub fn is_int(&self) -> bool {
        matches!(self, ColumnTypeFamily::Int)
    }

    pub fn is_json(&self) -> bool {
        matches!(self, ColumnTypeFamily::Json)
    }

    pub fn is_string(&self) -> bool {
        matches!(self, ColumnTypeFamily::String)
    }

    pub fn is_unsupported(&self) -> bool {
        matches!(self, ColumnTypeFamily::Unsupported(_))
    }
}

/// A column's arity.
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
pub enum ColumnArity {
    /// Required column.
    Required,
    /// Nullable column.
    Nullable,
    /// List type column.
    List,
}

impl ColumnArity {
    /// The arity is ColumnArity::List.
    pub fn is_list(&self) -> bool {
        matches!(self, ColumnArity::List)
    }

    /// The arity is ColumnArity::Nullable.
    pub fn is_nullable(&self) -> bool {
        matches!(self, ColumnArity::Nullable)
    }

    /// The arity is ColumnArity::Required.
    pub fn is_required(&self) -> bool {
        matches!(self, ColumnArity::Required)
    }
}

/// Foreign key action types (for ON DELETE|ON UPDATE).
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
pub enum ForeignKeyAction {
    /// Produce an error indicating that the deletion or update would create a foreign key
    /// constraint violation. If the constraint is deferred, this error will be produced at
    /// constraint check time if there still exist any referencing rows. This is the default action.
    NoAction,
    /// Produce an error indicating that the deletion or update would create a foreign key
    /// constraint violation. This is the same as NO ACTION except that the check is not deferrable.
    Restrict,
    /// Delete any rows referencing the deleted row, or update the values of the referencing
    /// column(s) to the new values of the referenced columns, respectively.
    Cascade,
    /// Set the referencing column(s) to null.
    SetNull,
    /// Set the referencing column(s) to their default values. (There must be a row in the
    /// referenced table matching the default values, if they are not null, or the operation
    /// will fail).
    SetDefault,
}

impl ForeignKeyAction {
    pub fn is_cascade(&self) -> bool {
        matches!(self, ForeignKeyAction::Cascade)
    }
}

#[derive(Serialize, Deserialize, Debug)]
struct ForeignKey {
    /// The table the foreign key is defined on.
    constrained_table: TableId,
    /// Referenced table.
    referenced_table: TableId,
    /// The foreign key constraint name, when available.
    constraint_name: Option<String>,
    on_delete_action: ForeignKeyAction,
    on_update_action: ForeignKeyAction,
}

#[derive(Serialize, Deserialize, Debug)]
struct ForeignKeyColumn {
    foreign_key_id: ForeignKeyId,
    constrained_column: TableColumnId,
    referenced_column: TableColumnId,
}

/// A SQL enum.
#[derive(Serialize, Deserialize, Debug)]
struct Enum {
    /// The namespace the enum type belongs to, if applicable.
    namespace_id: NamespaceId,
    name: String,
    description: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
struct EnumVariant {
    enum_id: EnumId,
    variant_name: String,
}

/// An SQL view.
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct View {
    /// Namespace of the view
    namespace_id: NamespaceId,
    /// Name of the view.
    pub name: String,
    /// The SQL definition of the view.
    pub definition: Option<String>,
    /// The comment in the database
    pub description: Option<String>,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct DefaultValue {
    kind: DefaultKind,
    constraint_name: Option<String>,
}

/// A DefaultValue
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum DefaultKind {
    /// A constant value, parsed as String
    Value(PrismaValue),
    /// An expression generating a current timestamp.
    Now,
    /// An expression generating a sequence.
    Sequence(String),
    /// A unique row ID,
    UniqueRowid,
    /// An unrecognized Default Value
    DbGenerated(Option<String>),
}

impl DefaultValue {
    pub fn db_generated(val: impl Into<String>) -> Self {
        Self::new(DefaultKind::DbGenerated(Some(val.into())))
    }

    pub fn constraint_name(&self) -> Option<&str> {
        self.constraint_name.as_deref()
    }

    pub fn now() -> Self {
        Self::new(DefaultKind::Now)
    }

    pub fn value(val: impl Into<PrismaValue>) -> Self {
        Self::new(DefaultKind::Value(val.into()))
    }

    pub fn sequence(val: impl ToString) -> Self {
        Self::new(DefaultKind::Sequence(val.to_string()))
    }

    pub fn kind(&self) -> &DefaultKind {
        &self.kind
    }

    pub fn new(kind: DefaultKind) -> Self {
        Self {
            kind,
            constraint_name: None,
        }
    }

    pub fn set_constraint_name(&mut self, name: impl ToString) {
        self.constraint_name = Some(name.to_string())
    }

    pub(crate) fn as_value(&self) -> Option<&PrismaValue> {
        match self.kind {
            DefaultKind::Value(ref v) => Some(v),
            _ => None,
        }
    }

    #[cfg(test)]
    pub(crate) fn as_sequence(&self) -> Option<&str> {
        match self.kind {
            DefaultKind::Sequence(ref name) => Some(name),
            _ => None,
        }
    }

    #[cfg(test)]
    pub(crate) fn is_db_generated(&self) -> bool {
        matches!(self.kind, DefaultKind::DbGenerated(_))
    }

    pub fn unique_rowid() -> Self {
        Self::new(DefaultKind::UniqueRowid)
    }

    pub fn with_constraint_name(mut self, constraint_name: Option<String>) -> Self {
        self.constraint_name = constraint_name;
        self
    }

    /// If the default value is the deprecated `dbgenerated()`
    /// variant.
    pub fn is_empty_dbgenerated(&self) -> bool {
        matches!(self.kind, DefaultKind::DbGenerated(None))
    }
}

fn unquote_string(val: &str) -> String {
    val.trim_start_matches('\'')
        .trim_end_matches('\'')
        .trim_start_matches('\\')
        .trim_start_matches('"')
        .trim_end_matches('"')
        .trim_end_matches('\\')
        .into()
}

#[derive(Debug)]
struct Precision {
    character_maximum_length: Option<u32>,
    numeric_precision: Option<u32>,
    numeric_scale: Option<u32>,
    time_precision: Option<u32>,
}

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

    #[test]
    fn unquoting_works() {
        let quoted_str = "'abc $$ def'".to_string();

        assert_eq!(unquote_string(&quoted_str), "abc $$ def");

        assert_eq!(unquote_string("heh "), "heh ");
    }
}