Skip to content

Commit 321c601

Browse files
committed
Add Materialzied views
1 parent 5b168d3 commit 321c601

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

puml-lib/db_ent.puml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
!return 'entity "**' + $name + '**"' + " as " + $name
1919
!endfunction
2020
21+
!function view($name, $materialized=false)
22+
!if ($materialized == false)
23+
!return 'entity "**' + $name + ' **<color:SkyBlue>**(V)**</color>"' + " as " + $name
24+
!else
25+
!return 'entity "**' + $name + ' **<color:DarkBlue>**(MV)**</color>"' + " as " + $name
26+
!endif
27+
!endfunction
28+
2129
!procedure enum($name, $variants)
2230
!$list = %splitstr($variants, ",")
2331
@@ -35,5 +43,7 @@
3543
|<color:#aaaaaa><&key></color>| Foreign Key |
3644
| &#8226; | Mandatory field (Not Null) |
3745
| <color:purple>**(E)**</color> | Enum |
46+
| <color:SkyBlue>**(V)**</color> | View |
47+
| <color:DarkBlue>**(MV)**</color> | Materialized View |
3848
endlegend
3949
!endprocedure

src/plantuml_generator.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ static PUML_TEMPLATE: &str = "@startuml\n\n\
2020

2121
static ENTITY_TEMPLATE: &str = "table({name}) \\{\n{pks} ---\n{fks}{nns}{others}}\n";
2222

23-
static VIEW_TEMPLATE: &str = "view({name}) \\{\n{pks} ---\n{fks}{nns}{others}}\n";
23+
static VIEW_TEMPLATE: &str =
24+
"view({name}{{ if materialized}}, $materialized=true{{ endif }}) \\{\n{columns}}\n";
2425

2526
static COLUMN_TEMPLATE: &str = " column({col.name}, \"{col.datatype}\"{{ if is_pk }}, $pk=true{{ endif }}{{ if is_fk }}, $fk=true{{ endif }}{{if is_nn}}, $nn=true{{ endif }})\n";
2627

@@ -47,6 +48,13 @@ struct SColumn<'a> {
4748
is_nn_and_not_pk: bool,
4849
}
4950

51+
#[derive(Serialize)]
52+
struct SView {
53+
name: String,
54+
columns: String,
55+
materialized: bool,
56+
}
57+
5058
#[derive(Serialize)]
5159
struct SEntity {
5260
name: String,
@@ -206,12 +214,10 @@ impl<'a> PlantUmlDefaultGenerator<'a> {
206214
};
207215
Ok(self.str_templates.render(
208216
"view",
209-
&SEntity {
210-
pks: String::default(),
211-
fks: String::default(),
212-
nns: String::default(),
213-
others: columns_render(view.columns.clone())?,
217+
&SView {
218+
columns: columns_render(view.columns.clone())?,
214219
name: view.name.clone(),
220+
materialized: view.materizlied,
215221
},
216222
)?)
217223
}

src/psql_erd_loader.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ WHERE table_schema = $1
2525
ORDER BY table_name;
2626
"#;
2727

28+
static GET_MATERIALIZED_VIEWS: &str = r#"
29+
SELECT trim(both '"' from matviewname) AS matview_name
30+
FROM pg_matviews
31+
WHERE schemaname = $1
32+
ORDER BY matviewname;
33+
"#;
34+
2835
/// https://www.postgresql.org/docs/current/catalog-pg-attribute.html
2936
static GET_COLUMNS_BASIC_INFO_QUERY: &str = r#"
3037
SELECT attname AS col_name,
@@ -405,7 +412,34 @@ impl SqlERDataLoader for PostgreSqlERDLoader {
405412
let (tables_and_views, enums) = self.load_tables(table_names).await?;
406413
let foreign_keys = self.get_fks(&tables_and_views)?;
407414

408-
let mut views: Vec<Arc<View>> = vec![];
415+
let mat_views_name: Vec<String> = self
416+
.client
417+
.query(GET_MATERIALIZED_VIEWS, &[&self.schema_name])
418+
.await?
419+
.iter()
420+
.map(|row| row.get("matview_name"))
421+
.collect();
422+
let (mat_views, _) = self.load_tables(mat_views_name).await?;
423+
424+
// Collect table names and types as a vector of tuples
425+
let table_names_with_types: Vec<(String, TableType)> = res
426+
.iter()
427+
.map(|row| (row.get("table_name"), row.get("table_type")))
428+
.collect();
429+
430+
let mut views: Vec<Arc<View>> = mat_views
431+
.into_iter()
432+
.map(|v| {
433+
let v = Arc::try_unwrap(v).unwrap();
434+
View {
435+
materizlied: true,
436+
name: v.name,
437+
columns: v.columns,
438+
}
439+
.into()
440+
})
441+
.collect();
442+
409443
let mut tables: Vec<Arc<Table>> = vec![];
410444

411445
for entity in tables_and_views.into_iter() {

0 commit comments

Comments
 (0)