Skip to main content

ic_dbms_api/dbms/table/
column_def.rs

1use crate::dbms::types::DataTypeKind;
2
3/// Defines a column in a database table.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub struct ColumnDef {
6    /// The name of the column.
7    pub name: &'static str,
8    /// The data type of the column.
9    pub data_type: DataTypeKind,
10    /// Indicates if this column can contain NULL values.
11    pub nullable: bool,
12    /// Indicates if this column is part of the primary key.
13    pub primary_key: bool,
14    /// Foreign key definition, if any.
15    pub foreign_key: Option<ForeignKeyDef>,
16}
17
18/// Defines a foreign key relationship for a column.
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub struct ForeignKeyDef {
21    /// Name of the local column that holds the foreign key (es: "user_id")
22    pub local_column: &'static str,
23    /// Name of the foreign table (e.g., "users")
24    pub foreign_table: &'static str,
25    /// Name of the foreign column that the FK points to (e.g., "id")
26    pub foreign_column: &'static str,
27}
28
29#[cfg(test)]
30mod test {
31
32    use super::*;
33    use crate::dbms::types::DataTypeKind;
34
35    #[test]
36    fn test_should_create_column_def() {
37        let column = ColumnDef {
38            name: "id",
39            data_type: DataTypeKind::Uint32,
40            nullable: false,
41            primary_key: true,
42            foreign_key: None,
43        };
44
45        assert_eq!(column.name, "id");
46        assert_eq!(column.data_type, DataTypeKind::Uint32);
47        assert!(!column.nullable);
48        assert!(column.primary_key);
49        assert!(column.foreign_key.is_none());
50    }
51
52    #[test]
53    fn test_should_create_column_def_with_foreign_key() {
54        let fk = ForeignKeyDef {
55            local_column: "user_id",
56            foreign_table: "users",
57            foreign_column: "id",
58        };
59
60        let column = ColumnDef {
61            name: "user_id",
62            data_type: DataTypeKind::Uint32,
63            nullable: false,
64            primary_key: false,
65            foreign_key: Some(fk),
66        };
67
68        assert_eq!(column.name, "user_id");
69        assert!(column.foreign_key.is_some());
70        let fk_def = column.foreign_key.unwrap();
71        assert_eq!(fk_def.local_column, "user_id");
72        assert_eq!(fk_def.foreign_table, "users");
73        assert_eq!(fk_def.foreign_column, "id");
74    }
75
76    #[test]
77    #[allow(clippy::clone_on_copy)]
78    fn test_should_clone_column_def() {
79        let column = ColumnDef {
80            name: "email",
81            data_type: DataTypeKind::Text,
82            nullable: true,
83            primary_key: false,
84            foreign_key: None,
85        };
86
87        let cloned = column.clone();
88        assert_eq!(column, cloned);
89    }
90
91    #[test]
92    fn test_should_compare_column_defs() {
93        let column1 = ColumnDef {
94            name: "id",
95            data_type: DataTypeKind::Uint32,
96            nullable: false,
97            primary_key: true,
98            foreign_key: None,
99        };
100
101        let column2 = ColumnDef {
102            name: "id",
103            data_type: DataTypeKind::Uint32,
104            nullable: false,
105            primary_key: true,
106            foreign_key: None,
107        };
108
109        let column3 = ColumnDef {
110            name: "name",
111            data_type: DataTypeKind::Text,
112            nullable: true,
113            primary_key: false,
114            foreign_key: None,
115        };
116
117        assert_eq!(column1, column2);
118        assert_ne!(column1, column3);
119    }
120
121    #[test]
122    fn test_should_create_foreign_key_def() {
123        let fk = ForeignKeyDef {
124            local_column: "post_id",
125            foreign_table: "posts",
126            foreign_column: "id",
127        };
128
129        assert_eq!(fk.local_column, "post_id");
130        assert_eq!(fk.foreign_table, "posts");
131        assert_eq!(fk.foreign_column, "id");
132    }
133
134    #[test]
135    #[allow(clippy::clone_on_copy)]
136    fn test_should_clone_foreign_key_def() {
137        let fk = ForeignKeyDef {
138            local_column: "author_id",
139            foreign_table: "authors",
140            foreign_column: "id",
141        };
142
143        let cloned = fk.clone();
144        assert_eq!(fk, cloned);
145    }
146
147    #[test]
148    fn test_should_compare_foreign_key_defs() {
149        let fk1 = ForeignKeyDef {
150            local_column: "user_id",
151            foreign_table: "users",
152            foreign_column: "id",
153        };
154
155        let fk2 = ForeignKeyDef {
156            local_column: "user_id",
157            foreign_table: "users",
158            foreign_column: "id",
159        };
160
161        let fk3 = ForeignKeyDef {
162            local_column: "category_id",
163            foreign_table: "categories",
164            foreign_column: "id",
165        };
166
167        assert_eq!(fk1, fk2);
168        assert_ne!(fk1, fk3);
169    }
170}