Skip to content

Commit

Permalink
Automatically generated ER diagram with Mermaid
Browse files Browse the repository at this point in the history
  • Loading branch information
BjoernKW committed Mar 16, 2023
1 parent fc74926 commit 9304f7a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To use Schematic, you need to add the following Maven dependency to your project
<dependency>
<groupId>com.bjoernkw</groupId>
<artifactId>schematic</artifactId>
<version>0.1.10</version>
<version>0.1.11</version>
</dependency>
```

Expand All @@ -25,7 +25,7 @@ If you're using Spring Boot 2.7.x and Java 11 you can add this version of Schema
<dependency>
<groupId>com.bjoernkw</groupId>
<artifactId>schematic</artifactId>
<version>0.1.10.jre11</version>
<version>0.1.11.jre11</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.bjoernkw</groupId>
<artifactId>schematic</artifactId>
<version>0.1.11-SNAPSHOT</version>
<version>0.1.11</version>
<name>Schematic</name>
<description>Database management UI for Spring Boot</description>
<url>https://github.com/BjoernKW/Schematic</url>
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/com/bjoernkw/schematic/TablesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -144,8 +149,16 @@ private List<Table> 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
Expand Down Expand Up @@ -176,12 +189,16 @@ c.relkind IN ('r', 'p')\s
NOT c1.relispartition AND NOT c2.relispartition;
""";

StringBuilder output = new StringBuilder();
List<Map<String, Object>> queryResultRows = jdbcTemplate.queryForList(sqlQuery);
for (Map<String, Object> queryResultRow : queryResultRows) {
output.append(queryResultRow.get(ER_DIAGRAM_RESULT_SET_COLUMN_NAME));
}
StringBuilder output = new StringBuilder();
List<Map<String, Object>> queryResultRows = jdbcTemplate.queryForList(sqlQuery);
for (Map<String, Object> queryResultRow : queryResultRows) {
output.append(queryResultRow.get(ER_DIAGRAM_RESULT_SET_COLUMN_NAME));
}

return output.toString();
return output.toString();
} else {
// Empty diagram
return "erDiagram";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9304f7a

Please sign in to comment.