diff --git a/README.md b/README.md index 5fae9d2..bb75173 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ To use Schematic, you need to add the following Maven dependency to your project com.bjoernkw schematic - 0.1.10 + 0.1.11 ``` @@ -25,7 +25,7 @@ If you're using Spring Boot 2.7.x and Java 11 you can add this version of Schema com.bjoernkw schematic - 0.1.10.jre11 + 0.1.11.jre11 ``` diff --git a/pom.xml b/pom.xml index 694386a..3e158bb 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.bjoernkw schematic - 0.1.11-SNAPSHOT + 0.1.11 Schematic Database management UI for Spring Boot https://github.com/BjoernKW/Schematic diff --git a/src/main/java/com/bjoernkw/schematic/TablesController.java b/src/main/java/com/bjoernkw/schematic/TablesController.java index 388304e..faaaf35 100644 --- a/src/main/java/com/bjoernkw/schematic/TablesController.java +++ b/src/main/java/com/bjoernkw/schematic/TablesController.java @@ -7,6 +7,8 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import javax.sql.DataSource; +import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -26,12 +28,15 @@ public class TablesController { private final JdbcTemplate jdbcTemplate; - public TablesController(JdbcTemplate jdbcTemplate) { + private final DataSource dataSource; + + public TablesController(JdbcTemplate jdbcTemplate, DataSource dataSource) { this.jdbcTemplate = jdbcTemplate; + this.dataSource = dataSource; } @GetMapping - public String listTables(Model model) { + public String showDatabaseStructure(Model model) { model.addAttribute( TABLE_VIEW_MODEL_NAME, getTables() @@ -144,8 +149,16 @@ private List getTables() { } private String generateERDiagram() { - // See https://www.cybertec-postgresql.com/en/er-diagrams-with-sql-and-mermaid/# - String sqlQuery = """ + String driverClassName = ""; + try { + driverClassName = DriverManager.getDriver(dataSource.getConnection().getMetaData().getURL()).getClass().toString(); + } catch (SQLException e) { + e.printStackTrace(); + } + + if (driverClassName.equals("class org.postgresql.Driver")) { + // See https://www.cybertec-postgresql.com/en/er-diagrams-with-sql-and-mermaid/# + String sqlQuery = """ SELECT 'erDiagram' AS mermaid_diagram_line UNION ALL SELECT @@ -176,12 +189,16 @@ c.relkind IN ('r', 'p')\s NOT c1.relispartition AND NOT c2.relispartition; """; - StringBuilder output = new StringBuilder(); - List> queryResultRows = jdbcTemplate.queryForList(sqlQuery); - for (Map queryResultRow : queryResultRows) { - output.append(queryResultRow.get(ER_DIAGRAM_RESULT_SET_COLUMN_NAME)); - } + StringBuilder output = new StringBuilder(); + List> queryResultRows = jdbcTemplate.queryForList(sqlQuery); + for (Map queryResultRow : queryResultRows) { + output.append(queryResultRow.get(ER_DIAGRAM_RESULT_SET_COLUMN_NAME)); + } - return output.toString(); + return output.toString(); + } else { + // Empty diagram + return "erDiagram"; + } } } diff --git a/src/main/java/com/bjoernkw/schematic/autoconfiguration/SchematicAutoConfiguration.java b/src/main/java/com/bjoernkw/schematic/autoconfiguration/SchematicAutoConfiguration.java index 1c25ddf..de68e85 100644 --- a/src/main/java/com/bjoernkw/schematic/autoconfiguration/SchematicAutoConfiguration.java +++ b/src/main/java/com/bjoernkw/schematic/autoconfiguration/SchematicAutoConfiguration.java @@ -8,20 +8,25 @@ import org.springframework.context.annotation.Bean; import org.springframework.jdbc.core.JdbcTemplate; +import javax.sql.DataSource; + @AutoConfiguration @ConditionalOnWebApplication public class SchematicAutoConfiguration { private final JdbcTemplate jdbcTemplate; - public SchematicAutoConfiguration(JdbcTemplate jdbcTemplate) { + private final DataSource dataSource; + + public SchematicAutoConfiguration(JdbcTemplate jdbcTemplate, DataSource dataSource) { this.jdbcTemplate = jdbcTemplate; + this.dataSource = dataSource; } @Bean @ConditionalOnMissingBean public TablesController tablesController() { - return new TablesController(jdbcTemplate); + return new TablesController(jdbcTemplate, dataSource); } @Bean