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
use crate::datamodel_connector::ReferentialAction;
use enumflags2::{bitflags, BitFlags};
use std::fmt;

/// Defines the part of the stack where referential actions are handled.
#[bitflags]
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum RelationMode {
    /// Enforced in the database. Needs support from the underlying database
    /// server.
    ForeignKeys,
    /// Enforced in Prisma. Slower, but for databases that do not support
    /// foreign keys.
    Prisma,
}

impl RelationMode {
    pub fn allowed_emulated_referential_actions_default() -> BitFlags<ReferentialAction> {
        use ReferentialAction::*;

        Restrict | SetNull | NoAction | Cascade
    }

    pub fn is_prisma(&self) -> bool {
        matches!(self, Self::Prisma)
    }

    /// True, if integrity is in database foreign keys
    pub fn uses_foreign_keys(&self) -> bool {
        matches!(self, Self::ForeignKeys)
    }
}

impl Default for RelationMode {
    fn default() -> Self {
        Self::ForeignKeys
    }
}

impl fmt::Display for RelationMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RelationMode::ForeignKeys => write!(f, "foreignKeys"),
            RelationMode::Prisma => write!(f, "prisma"),
        }
    }
}