1#[derive(Debug, Clone)]
11pub struct ColumnDef {
12 pub name: String,
13 pub col_type: ColumnType,
14 pub nullable: bool,
15 pub primary_key: bool,
16 pub unique: bool,
17 pub default: Option<String>,
18 pub references: Option<ForeignKey>,
19}
20
21#[derive(Debug, Clone, PartialEq)]
27pub enum ColumnType {
28 Bool,
29 SmallInt,
30 Int,
31 BigInt,
32 Float,
33 Double,
34 Decimal { precision: u8, scale: u8 },
35 Text,
36 Varchar(u32),
37 Char(u32),
38 Bytes,
39 Uuid,
40 Timestamp,
41 TimestampTz,
42 Date,
43 Time,
44 Json,
45 Jsonb,
46 Array(Box<ColumnType>),
47 Dynamic,
49}
50
51#[derive(Debug, Clone)]
52pub struct ForeignKey {
53 pub table: String,
54 pub column: String,
55 pub on_delete: ReferentialAction,
56 pub on_update: ReferentialAction,
57}
58
59#[derive(Debug, Clone, Default)]
60pub enum ReferentialAction {
61 #[default]
62 NoAction,
63 Cascade,
64 SetNull,
65 Restrict,
66}
67
68impl ColumnDef {
69 pub fn new(name: impl Into<String>, col_type: ColumnType) -> Self {
70 Self {
71 name: name.into(),
72 col_type,
73 nullable: true,
74 primary_key: false,
75 unique: false,
76 default: None,
77 references: None,
78 }
79 }
80
81 pub fn primary_key(mut self) -> Self {
82 self.primary_key = true;
83 self.nullable = false;
84 self
85 }
86
87 pub fn not_null(mut self) -> Self {
88 self.nullable = false;
89 self
90 }
91
92 pub fn unique(mut self) -> Self {
93 self.unique = true;
94 self
95 }
96
97 pub fn default(mut self, expr: impl Into<String>) -> Self {
98 self.default = Some(expr.into());
99 self
100 }
101
102 pub fn references(mut self, table: impl Into<String>, column: impl Into<String>) -> Self {
103 self.references = Some(ForeignKey {
104 table: table.into(),
105 column: column.into(),
106 on_delete: ReferentialAction::NoAction,
107 on_update: ReferentialAction::NoAction,
108 });
109 self
110 }
111}