Skip to content

Commit

Permalink
Diagram output bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BjoernKW committed Mar 17, 2023
1 parent 0cb25c2 commit 6b97a38
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 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.12</version>
<version>0.2.0</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.12.jre11</version>
<version>0.2.0.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.12.jre11</version>
<version>0.2.0.jre11</version>
<name>Schematic</name>
<description>Database management UI for Spring Boot</description>
<url>https://github.com/BjoernKW/Schematic</url>
Expand Down
56 changes: 28 additions & 28 deletions src/main/java/com/bjoernkw/schematic/TablesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,34 +158,34 @@ private String generateERDiagram() {

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\n" +
" UNION ALL\n" +
" SELECT\n" +
" format(E'\\t%s {\\n%s\\n\\t}', \n" +
" c.relname, \n" +
" string_agg(format(E'\\t\\t%s %s', \n" +
" t.typname, \n" +
" a.attname\n" +
" ), E'\\n'))\n" +
" FROM\n" +
" pg_class c \n" +
" JOIN pg_namespace n ON n.oid = c.relnamespace\n" +
" LEFT JOIN pg_attribute a ON c.oid = a.attrelid AND a.attnum > 0 AND NOT a.attisdropped\n" +
" LEFT JOIN pg_type t ON a.atttypid = t.oid\n" +
" WHERE\n" +
" c.relkind IN ('r', 'p') \n" +
" AND NOT c.relispartition\n" +
" AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n" +
" GROUP BY c.relname\n" +
" UNION ALL\n" +
" SELECT\n" +
" format('%s }|..|| %s : %s', c1.relname, c2.relname, c.conname)\n" +
" FROM\n" +
" pg_constraint c\n" +
" JOIN pg_class c1 ON c.conrelid = c1.oid AND c.contype = 'f'\n" +
" JOIN pg_class c2 ON c.confrelid = c2.oid\n" +
" WHERE\n" +
" NOT c1.relispartition AND NOT c2.relispartition;\n";
String sqlQuery = " SELECT 'erDiagram' AS mermaid_diagram_line " +
" UNION ALL " +
" SELECT " +
" format(E'\\t%s {\\n%s\\n\\t}\\n', " +
" c.relname, " +
" string_agg(format(E'\\t\\t%s %s', " +
" t.typname, " +
" a.attname " +
" ), E'\\n')) " +
" FROM " +
" pg_class c " +
" JOIN pg_namespace n ON n.oid = c.relnamespace " +
" LEFT JOIN pg_attribute a ON c.oid = a.attrelid AND a.attnum > 0 AND NOT a.attisdropped " +
" LEFT JOIN pg_type t ON a.atttypid = t.oid " +
" WHERE " +
" c.relkind IN ('r', 'p') " +
" AND NOT c.relispartition " +
" AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' " +
" GROUP BY c.relname " +
" UNION ALL " +
" SELECT " +
" format(E'\\t%s }|..|| %s : %s\\n', c1.relname, c2.relname, c.conname) " +
" FROM " +
" pg_constraint c " +
" JOIN pg_class c1 ON c.conrelid = c1.oid AND c.contype = 'f' " +
" JOIN pg_class c2 ON c.confrelid = c2.oid " +
" WHERE " +
" NOT c1.relispartition AND NOT c2.relispartition;";

StringBuilder output = new StringBuilder();
List<Map<String, Object>> queryResultRows = jdbcTemplate.queryForList(sqlQuery);
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/templates/fragments/er-diagram.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<div class="container fade-in" th:if="${erDiagram}" th:fragment="er-diagram">
<button type="button"
id="copy-er-diagram-mermaid-source-code"
class="btn btn-sm btn-info float-end"
title="Copy Mermaid diagram source code to clipboard"
onclick="copyToClipboard()">
<i class="fas fa-copy"></i>
</button>
<pre class="mermaid">
[[${erDiagram}]]
</pre>
<textarea class="d-none"
id="er-diagram-mermaid-source-code">[[${erDiagram}]]</textarea>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
</script>
<script>
function copyToClipboard() {
document.getElementById('copy-er-diagram-mermaid-source-code').classList.remove('fade-in');

navigator.clipboard.writeText(document.getElementById('er-diagram-mermaid-source-code').value).then(
() => {
document.getElementById('copy-er-diagram-mermaid-source-code').classList.add('fade-in');
}
);
}
</script>
</div>
</html>
2 changes: 1 addition & 1 deletion src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h2>Tables</h2>
<div id="tables" th:insert="~{fragments/tables}"></div>

<h2>ER Diagram</h2>
<em>- PostgreSQL-only for now - </em>
<em>- <strong>Experimental</strong>. PostgreSQL-only for now. - </em>
<div id="er-diagram" th:insert="~{fragments/er-diagram}"></div>
</div>
</section>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/layouts/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<link rel="icon" th:href="@{/images/favicon.png}">

<link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}">
<link rel="stylesheet" th:href="@{/webjars/font-awesome/css/all.css}">
<link rel="stylesheet" th:href="@{/webjars/font-awesome/6.3.0/css/all.css}">
<link rel="stylesheet" th:href="@{/css/styles.css}">

<script th:src="@{/webjars/popper.js/umd/popper.min.js}"></script>
Expand Down

0 comments on commit 6b97a38

Please sign in to comment.