rdbi_codegen/parser/
metadata.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct TableMetadata {
8 pub name: String,
10
11 pub comment: Option<String>,
13
14 pub columns: Vec<ColumnMetadata>,
16
17 pub indexes: Vec<IndexMetadata>,
19
20 pub foreign_keys: Vec<ForeignKeyMetadata>,
22
23 pub primary_key: Option<PrimaryKey>,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct ColumnMetadata {
30 pub name: String,
32
33 pub data_type: String,
35
36 pub nullable: bool,
38
39 pub default_value: Option<String>,
41
42 pub is_auto_increment: bool,
44
45 pub is_unsigned: bool,
47
48 pub enum_values: Option<Vec<String>>,
50
51 pub comment: Option<String>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct IndexMetadata {
58 pub name: String,
60
61 pub columns: Vec<String>,
63
64 pub unique: bool,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct PrimaryKey {
71 pub columns: Vec<String>,
73}
74
75impl PrimaryKey {
76 pub fn is_composite(&self) -> bool {
78 self.columns.len() > 1
79 }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct ForeignKeyMetadata {
85 pub column_name: String,
87
88 pub referenced_table: String,
90
91 pub referenced_column: String,
93}
94
95impl TableMetadata {
96 pub fn get_column(&self, name: &str) -> Option<&ColumnMetadata> {
98 self.columns.iter().find(|c| c.name == name)
99 }
100
101 pub fn get_indexed_columns(&self) -> Vec<&str> {
103 self.indexes
104 .iter()
105 .filter(|idx| idx.columns.len() == 1)
106 .map(|idx| idx.columns[0].as_str())
107 .collect()
108 }
109
110 pub fn is_primary_key_column(&self, column_name: &str) -> bool {
112 self.primary_key
113 .as_ref()
114 .map(|pk| pk.columns.contains(&column_name.to_string()))
115 .unwrap_or(false)
116 }
117}
118
119impl ColumnMetadata {
120 pub fn is_enum(&self) -> bool {
122 self.enum_values.is_some()
123 }
124}