-
Notifications
You must be signed in to change notification settings - Fork 4
Add view and materialized view support (PlantUML) #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
80d04e4
ecf62af
5b168d3
321c601
070a060
9a70b5b
7431607
5a9b6b5
8d1e4bb
93f3a0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| services: | ||
| db: | ||
| image: postgres | ||
| image: postgres:17.4 | ||
| environment: | ||
| POSTGRES_USER: sql | ||
| POSTGRES_PASSWORD: sql | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,16 @@ | ||||||||||||||
| use native_tls::TlsConnector; | ||||||||||||||
| use postgres_native_tls::MakeTlsConnector; | ||||||||||||||
| use std::collections::{BTreeMap, BTreeSet}; | ||||||||||||||
| use std::sync::Arc; | ||||||||||||||
| use tokio_postgres::Client; | ||||||||||||||
| use std::{ | ||||||||||||||
| collections::{BTreeMap, BTreeSet}, | ||||||||||||||
| error::Error, | ||||||||||||||
| sync::Arc, | ||||||||||||||
| }; | ||||||||||||||
| use tokio_postgres::{ | ||||||||||||||
| types::{FromSql, Type}, | ||||||||||||||
| Client, | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| use crate::sql_entities::View; | ||||||||||||||
| use crate::{ | ||||||||||||||
| sql_entities::{ | ||||||||||||||
| ColumnConstraints, ForeignKey, SqlERData, SqlERDataLoader, SqlEnums, Table, TableColumn, | ||||||||||||||
|
|
@@ -12,12 +19,19 @@ use crate::{ | |||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| static GET_TABLES_LIST_QUERY: &str = r#" | ||||||||||||||
| SELECT trim(both '"' from table_name) as table_name | ||||||||||||||
| SELECT trim(both '"' from table_name) as table_name, table_type | ||||||||||||||
| FROM information_schema.tables | ||||||||||||||
| WHERE table_schema = $1 | ||||||||||||||
| ORDER BY table_name; | ||||||||||||||
| "#; | ||||||||||||||
|
|
||||||||||||||
| static GET_MATERIALIZED_VIEWS: &str = r#" | ||||||||||||||
| SELECT trim(both '"' from matviewname) AS matview_name | ||||||||||||||
| FROM pg_matviews | ||||||||||||||
| WHERE schemaname = $1 | ||||||||||||||
| ORDER BY matviewname; | ||||||||||||||
| "#; | ||||||||||||||
|
|
||||||||||||||
| /// https://www.postgresql.org/docs/current/catalog-pg-attribute.html | ||||||||||||||
| static GET_COLUMNS_BASIC_INFO_QUERY: &str = r#" | ||||||||||||||
| SELECT attname AS col_name, | ||||||||||||||
|
|
@@ -72,7 +86,7 @@ WHERE pg_type.oid = $1 | |||||||||||||
| ORDER BY pg_enum; | ||||||||||||||
| "#; | ||||||||||||||
|
|
||||||||||||||
| /// https://www.postgresql.org/docs/current/view-pg-indexes.html | ||||||||||||||
| // https://www.postgresql.org/docs/current/view-pg-indexes.html | ||||||||||||||
| // If you'll need to add indexers support | ||||||||||||||
| // static GET_INDEXES_QUERY: &'static str = r#" | ||||||||||||||
| // SELECT | ||||||||||||||
|
|
@@ -188,7 +202,6 @@ impl PostgreSqlERDLoader { | |||||||||||||
| .query(GET_COLUMNS_BASIC_INFO_QUERY, &[&table_names]) | ||||||||||||||
| .await?; | ||||||||||||||
| for row in rows { | ||||||||||||||
| // I don't know how to get rid this | ||||||||||||||
| let col_num: i16 = row.get("col_num"); | ||||||||||||||
| let col_name: &str = row.get("col_name"); | ||||||||||||||
| let not_null: bool = row.get("not_null"); | ||||||||||||||
|
|
@@ -345,6 +358,27 @@ impl PostgreSqlERDLoader { | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[derive(Debug, PartialEq)] | ||||||||||||||
| enum TableType { | ||||||||||||||
| BaseTable, | ||||||||||||||
| View, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl FromSql<'_> for TableType { | ||||||||||||||
| fn from_sql(_ty: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> { | ||||||||||||||
| let s = std::str::from_utf8(raw)?; | ||||||||||||||
| match s { | ||||||||||||||
| "BASE TABLE" => Ok(TableType::BaseTable), | ||||||||||||||
| "VIEW" => Ok(TableType::View), | ||||||||||||||
| other => Err(format!("Unknown table type: {}", other).into()), | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| fn accepts(ty: &Type) -> bool { | ||||||||||||||
| *ty == Type::TEXT || *ty == Type::VARCHAR | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[async_trait::async_trait] | ||||||||||||||
| impl SqlERDataLoader for PostgreSqlERDLoader { | ||||||||||||||
| async fn load_erd_data(&mut self) -> Result<SqlERData, crate::SqlantError> { | ||||||||||||||
|
|
@@ -362,14 +396,78 @@ impl SqlERDataLoader for PostgreSqlERDLoader { | |||||||||||||
| .client | ||||||||||||||
| .query(GET_TABLES_LIST_QUERY, &[&self.schema_name]) | ||||||||||||||
| .await?; | ||||||||||||||
| let table_names: Vec<String> = res.iter().map(|row| row.get("table_name")).collect(); | ||||||||||||||
| let (tables, enums) = self.load_tables(table_names).await?; | ||||||||||||||
| let foreign_keys = self.get_fks(&tables)?; | ||||||||||||||
|
|
||||||||||||||
| // Collect table names and types as a vector of tuples | ||||||||||||||
| let table_names_with_types: Vec<(String, TableType)> = res | ||||||||||||||
| .iter() | ||||||||||||||
| .map(|row| (row.get("table_name"), row.get("table_type"))) | ||||||||||||||
| .collect(); | ||||||||||||||
|
|
||||||||||||||
| // Extract just the table names for loading | ||||||||||||||
| let table_names: Vec<String> = table_names_with_types | ||||||||||||||
| .iter() | ||||||||||||||
| .map(|(name, _)| name.clone()) | ||||||||||||||
| .collect(); | ||||||||||||||
|
|
||||||||||||||
| let (tables_and_views, enums) = self.load_tables(table_names).await?; | ||||||||||||||
| let foreign_keys = self.get_fks(&tables_and_views)?; | ||||||||||||||
|
|
||||||||||||||
| let mat_views_name: Vec<String> = self | ||||||||||||||
| .client | ||||||||||||||
| .query(GET_MATERIALIZED_VIEWS, &[&self.schema_name]) | ||||||||||||||
| .await? | ||||||||||||||
| .iter() | ||||||||||||||
| .map(|row| row.get("matview_name")) | ||||||||||||||
| .collect(); | ||||||||||||||
| let (mat_views, _) = self.load_tables(mat_views_name).await?; | ||||||||||||||
|
|
||||||||||||||
| // Collect table names and types as a vector of tuples | ||||||||||||||
| let table_names_with_types: Vec<(String, TableType)> = res | ||||||||||||||
| .iter() | ||||||||||||||
| .map(|row| (row.get("table_name"), row.get("table_type"))) | ||||||||||||||
| .collect(); | ||||||||||||||
|
|
||||||||||||||
|
Comment on lines
+424
to
+429
|
||||||||||||||
| // Collect table names and types as a vector of tuples | |
| let table_names_with_types: Vec<(String, TableType)> = res | |
| .iter() | |
| .map(|row| (row.get("table_name"), row.get("table_type"))) | |
| .collect(); | |
| // Reuse the previously collected table_names_with_types vector |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This placeholder comment appears to be a leftover note. Consider removing or clarifying it to keep comments directly relevant.