sqlant/
plantuml_generator.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::sync::Arc;

use super::sql_entities::{SqlERData, Table, TableColumn};
use crate::{GeneratorConfigOptions, ViewGenerator};
use serde::Serialize;
use tinytemplate::{format_unescaped, TinyTemplate};

pub struct PlantUmlDefaultGenerator<'a> {
    str_templates: TinyTemplate<'a>,
}

static PUML_TEMPLATE: &str = "@startuml\n\n\
    hide circle\n\
    skinparam linetype ortho\n\n\
    {{ for ent in entities}}{ent}\n{{ endfor }}\n\
    {{ for fk in foreign_keys}}{fk}\n{{ endfor }}\n\
    {{ for e in enums}}{e}\n{{ endfor }}{legend}@enduml\n";

static ENTITY_TEMPLATE: &str = "entity \"**{name}**\" \\{\n{pks}---\n{fks}{nns}{others}}\n";

static COLUMN_TEMPLATE: &str =
    "{{ if is_nn_and_not_pk }}*{{ endif }}{{ if is_pk }}<b><color:#d99d1c><&key></color>{{else}}{{ endif }}{{ if is_fk }}<color:#aaaaaa><&key></color>{{ endif }}<b>\"\"{col.name}\"\"</b>: //\"\"{col.datatype}\"\" //\n";

static REL_TEMPLATE: &str =
    "\"**{source_table_name}**\" {{ if is_zero_one_to_one }}|o--||{{else}}}o--||{{ endif }} \"**{target_table_name}**\"\n";

static ENUM_TEMPLATE: &str =
    "object \"<color:BlueViolet>**{name}**</color> (enum)\" as {name} \\{\n{{ for v in values}} {v}\n{{ endfor }}}\n";

static PUML_LEGEND: &str = r#"
legend right
 <#GhostWhite,#GhostWhite>|   |= __Legend__ |
 |<b><color:#b8861b><&key></color></b>| Primary Key |
 |<color:#aaaaaa><&key></color>| Foreign Key |
 | &#8226; | Mandatory field (Not Null) |
endlegend
"#;

#[derive(Serialize)]
struct SSqlEnum {
    name: String,
    values: Vec<String>,
}

#[derive(Serialize)]
struct SColumn<'a> {
    col: &'a TableColumn,
    is_fk: bool,
    is_pk: bool,
    is_nn: bool,
    is_nn_and_not_pk: bool,
}

#[derive(Serialize)]
struct SEntity {
    name: String,
    pks: String,    // Columns that contain PK
    fks: String,    // Columns that contain FK and don't contain PK
    nns: String,    // NOT NULL Columns that don't contain both PK and FK
    others: String, // Columns that don't contain both PK and FK
}

#[derive(Serialize)]
struct SLegend(String);

#[derive(Serialize)]
struct SPuml {
    entities: Vec<String>,
    foreign_keys: Vec<String>,
    enums: Vec<String>,
    legend: Option<SLegend>,
}

#[derive(Serialize)]
struct SForeignKey {
    source_table_name: String,
    target_table_name: String,
    is_zero_one_to_one: bool,
}

struct SortedColumns {
    pks: Vec<Arc<TableColumn>>,
    fks: Vec<Arc<TableColumn>>,
    nns: Vec<Arc<TableColumn>>,
    others: Vec<Arc<TableColumn>>,
}

impl<'a> PlantUmlDefaultGenerator<'a> {
    pub fn new() -> Result<PlantUmlDefaultGenerator<'a>, crate::SqlantError> {
        let mut str_templates = TinyTemplate::new();
        str_templates.add_template("puml", PUML_TEMPLATE)?;
        str_templates.add_template("pk", COLUMN_TEMPLATE)?;
        str_templates.add_template("ent", ENTITY_TEMPLATE)?;
        str_templates.add_template("rel", REL_TEMPLATE)?;
        str_templates.add_template("enum", ENUM_TEMPLATE)?;
        str_templates.add_template("legend", PUML_LEGEND)?;
        str_templates.set_default_formatter(&format_unescaped);
        Ok(PlantUmlDefaultGenerator { str_templates })
    }

    // Sorts columns in next order:
    // 1. PKs
    // 2. FKs
    // 3. NN
    // 4. Others
    fn sort_columns(cols: &[Arc<TableColumn>]) -> SortedColumns {
        let mut cloned_cols = cols.to_owned();
        let mut pks = Vec::new();
        let mut fks = Vec::new();
        let mut nns = Vec::new();
        let mut others = Vec::new();

        cloned_cols.retain(|col| {
            if col.is_pk() {
                pks.push(Arc::clone(col));
                false
            } else {
                true
            }
        });

        cloned_cols.retain(|col| {
            if col.is_fk() && !col.is_pk() {
                fks.push(Arc::clone(col));
                false
            } else {
                true
            }
        });

        cloned_cols.retain(|col| {
            if col.is_nn() && !col.is_pk() && !col.is_fk() {
                nns.push(Arc::clone(col));
                false
            } else {
                true
            }
        });

        others.extend(cloned_cols);

        SortedColumns {
            pks,
            fks,
            nns,
            others,
        }
    }
    fn entity_render(&self, tbl: &Table) -> Result<String, crate::SqlantError> {
        let sorted_columns = Self::sort_columns(&tbl.columns);

        let columns_render = |columns: Vec<Arc<TableColumn>>| -> Result<String, _> {
            Ok::<std::string::String, crate::SqlantError>(columns.iter().try_fold(
                String::new(),
                |acc, col| {
                    let r = self.str_templates.render(
                        "pk",
                        &SColumn {
                            col: col.as_ref(),
                            is_fk: col.is_fk(),
                            is_pk: col.is_pk(),
                            is_nn: col.is_nn(),
                            is_nn_and_not_pk: col.is_nn() && (!col.is_pk()),
                        },
                    );
                    match r {
                        Ok(r) => Ok(acc + &r),
                        Err(e) => Err(e),
                    }
                },
            )?)
        };
        Ok(self.str_templates.render(
            "ent",
            &SEntity {
                pks: columns_render(sorted_columns.pks)?,
                fks: columns_render(sorted_columns.fks)?,
                nns: columns_render(sorted_columns.nns)?,
                others: columns_render(sorted_columns.others)?,
                name: tbl.name.clone(),
            },
        )?)
    }
}

impl<'a> ViewGenerator for PlantUmlDefaultGenerator<'a> {
    fn generate(
        &self,
        sql_erd: SqlERData,
        opts: &GeneratorConfigOptions,
    ) -> Result<String, crate::SqlantError> {
        let entities: Vec<String> = sql_erd
            .tables
            .iter()
            .map(|tbl| self.entity_render(tbl))
            .collect::<Result<Vec<String>, crate::SqlantError>>()?;
        let foreign_keys: Vec<String> = sql_erd
            .foreign_keys
            .iter()
            .map(|fk| {
                self.str_templates.render(
                    "rel",
                    &SForeignKey {
                        source_table_name: fk.source_table.name.clone(),
                        target_table_name: fk.target_table.name.clone(),
                        is_zero_one_to_one: fk.is_zero_one_to_one,
                    },
                )
            })
            .collect::<Result<Vec<String>, _>>()?;

        let enums: Vec<String> = if opts.draw_enums {
            sql_erd
                .enums
                .iter()
                .map(|(name, values)| {
                    self.str_templates.render(
                        "enum",
                        &SSqlEnum {
                            name: name.to_string(),
                            values: values.to_vec(),
                        },
                    )
                })
                .collect::<Result<Vec<String>, _>>()?
        } else {
            vec![]
        };

        let legend = if opts.draw_legend {
            Some(SLegend(self.str_templates.render("legend", &())?))
        } else {
            None
        };

        Ok(self.str_templates.render(
            "puml",
            &SPuml {
                entities,
                foreign_keys,
                enums,
                legend,
            },
        )?)
    }
}