Skip to main content

reflect_db/
metadata.rs

1use serde::{Serialize, Deserialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub struct MetaData {
6    pub schema: Option<String>,
7    pub tables: HashMap<String, Table>,
8}
9
10impl MetaData {
11    pub fn new() -> Self {
12        Self {
13            schema: None,
14            tables: HashMap::new(),
15        }
16    }
17
18    /// Serializes the MetaData structure to a JSON string.
19    pub fn extract_json(&self) -> Result<String, serde_json::Error> {
20        serde_json::to_string(self)
21    }
22}
23
24impl Default for MetaData {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct Table {
32    pub name: String,
33    pub schema: Option<String>,
34    pub columns: Vec<Column>,
35    pub primary_key: Option<PrimaryKey>,
36    pub foreign_keys: Vec<ForeignKey>,
37    pub indexes: Vec<Index>,
38    pub is_view: bool,
39}
40
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub struct Column {
43    pub name: String,
44    pub data_type: SqlType,
45    pub nullable: bool,
46    pub default: Option<String>,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50pub enum SqlType {
51    Integer,
52    BigInt,
53    Float,
54    Double,
55    Boolean,
56    Text,
57    Varchar(Option<u32>),
58    Date,
59    Timestamp,
60    Json,
61    Uuid,
62    Custom(String),
63}
64
65#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
66pub struct ForeignKey {
67    pub column: String,
68    pub referenced_table: String,
69    pub referenced_column: String,
70}
71
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
73pub struct Index {
74    pub name: String,
75    pub columns: Vec<String>,
76    pub unique: bool,
77}
78
79#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
80pub struct PrimaryKey {
81    pub columns: Vec<String>,
82}