From 75a5412c2bdbcec1fd94411ac58be977cbffca0c Mon Sep 17 00:00:00 2001 From: vran Date: Thu, 30 Dec 2021 12:36:19 +0800 Subject: [PATCH] feat: use freemarker to render markdown --- core/build.gradle | 4 +- .../java/com/databasir/core/Databasir.java | 28 +++++++++ .../com/databasir/core/DatabasirConfig.java | 12 ++-- .../databasir/core/meta/pojo/ColumnMeta.java | 3 + .../impl/jdbc/JdbcColumnMetaRepository.java | 3 + .../com/databasir/core/render/Render.java | 5 +- .../markdown/MarkdownTemplateRender.java | 57 +++++++++++++++++++ .../template/render/markdown/markdown.ftlh | 44 ++++++++++++++ core/src/test/java/App.java | 5 +- 9 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 core/src/main/java/com/databasir/core/render/markdown/MarkdownTemplateRender.java create mode 100644 core/src/main/resources/template/render/markdown/markdown.ftlh diff --git a/core/build.gradle b/core/build.gradle index 9a94a40..914243b 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,5 +1,5 @@ dependencies { - implementation 'mysql:mysql-connector-java:8.0.27' - implementation 'commons-io:commons-io:2.11.0' + testImplementation 'mysql:mysql-connector-java:8.0.27' implementation 'org.commonmark:commonmark:0.18.1' + implementation 'org.freemarker:freemarker:2.3.31' } diff --git a/core/src/main/java/com/databasir/core/Databasir.java b/core/src/main/java/com/databasir/core/Databasir.java index 13a1ea4..5d94c68 100644 --- a/core/src/main/java/com/databasir/core/Databasir.java +++ b/core/src/main/java/com/databasir/core/Databasir.java @@ -1,7 +1,9 @@ package com.databasir.core; import com.databasir.core.meta.pojo.DatabaseMeta; +import com.databasir.core.meta.repository.*; import com.databasir.core.meta.repository.condition.Condition; +import com.databasir.core.meta.repository.impl.jdbc.*; import com.databasir.core.render.Render; import com.databasir.core.render.RenderConfig; import lombok.Getter; @@ -40,6 +42,32 @@ public class Databasir { } public static Databasir of(DatabasirConfig config) { + TriggerMetaRepository triggerMetaRepository = config.getTriggerMetaRepository(); + if (triggerMetaRepository == null) { + triggerMetaRepository = new JdbcTriggerMetaRepository(); + } + IndexMetaRepository indexMetaRepository = config.getIndexMetaRepository(); + if (indexMetaRepository == null) { + indexMetaRepository = new JdbcIndexMetaRepository(); + } + ColumnMetaRepository columnMetaRepository = config.getColumnMetaRepository(); + if (columnMetaRepository == null) { + columnMetaRepository = new JdbcColumnMetaRepository(); + } + TableMetaRepository tableMetaRepository = config.getTableMetaRepository(); + if (tableMetaRepository == null) { + tableMetaRepository = + new JdbcTableMetaRepository(columnMetaRepository, indexMetaRepository, triggerMetaRepository); + } + DatabaseMetaRepository databaseMetaRepository = config.getDatabaseMetaRepository(); + if (databaseMetaRepository == null) { + databaseMetaRepository = new JdbcDatabaseMetaRepository(tableMetaRepository); + } + config.setTriggerMetaRepository(triggerMetaRepository); + config.setIndexMetaRepository(indexMetaRepository); + config.setColumnMetaRepository(columnMetaRepository); + config.setTableMetaRepository(tableMetaRepository); + config.setDatabaseMetaRepository(databaseMetaRepository); return new Databasir(config); } diff --git a/core/src/main/java/com/databasir/core/DatabasirConfig.java b/core/src/main/java/com/databasir/core/DatabasirConfig.java index 226bbbf..74a58bb 100644 --- a/core/src/main/java/com/databasir/core/DatabasirConfig.java +++ b/core/src/main/java/com/databasir/core/DatabasirConfig.java @@ -1,7 +1,6 @@ package com.databasir.core; import com.databasir.core.meta.repository.*; -import com.databasir.core.meta.repository.impl.jdbc.*; import lombok.Getter; import lombok.Setter; @@ -12,16 +11,15 @@ import java.util.HashSet; @Setter public class DatabasirConfig { - private IndexMetaRepository indexMetaRepository = new JdbcIndexMetaRepository(); + private IndexMetaRepository indexMetaRepository; - private TriggerMetaRepository triggerMetaRepository = new JdbcTriggerMetaRepository(); + private TriggerMetaRepository triggerMetaRepository; - private ColumnMetaRepository columnMetaRepository = new JdbcColumnMetaRepository(); + private ColumnMetaRepository columnMetaRepository; - private TableMetaRepository tableMetaRepository = - new JdbcTableMetaRepository(columnMetaRepository, indexMetaRepository, triggerMetaRepository); + private TableMetaRepository tableMetaRepository; - private DatabaseMetaRepository databaseMetaRepository = new JdbcDatabaseMetaRepository(tableMetaRepository); + private DatabaseMetaRepository databaseMetaRepository; private Collection ignoreTableNameRegex = new HashSet<>(); diff --git a/core/src/main/java/com/databasir/core/meta/pojo/ColumnMeta.java b/core/src/main/java/com/databasir/core/meta/pojo/ColumnMeta.java index 6535eef..0f01c1e 100644 --- a/core/src/main/java/com/databasir/core/meta/pojo/ColumnMeta.java +++ b/core/src/main/java/com/databasir/core/meta/pojo/ColumnMeta.java @@ -13,6 +13,9 @@ public class ColumnMeta { private String type; + /** + * if default value is empty string, will be converted to ''. + */ private String defaultValue; private Integer size; diff --git a/core/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java b/core/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java index 1795554..6f4aab4 100644 --- a/core/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java +++ b/core/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java @@ -51,6 +51,9 @@ public class JdbcColumnMetaRepository implements ColumnMetaRepository { boolean isNullable = Objects.equals("YES", columnsResult.getString("IS_NULLABLE")); boolean isAutoIncrement = Objects.equals("YES", columnsResult.getString("IS_AUTOINCREMENT")); String columnComment = columnsResult.getString("REMARKS"); + if (defaultValue != null && defaultValue.trim().equals("")) { + defaultValue = "'" + defaultValue + "'"; + } ColumnMeta columnMeta = ColumnMeta.builder() .name(columnName) .type(columnType) diff --git a/core/src/main/java/com/databasir/core/render/Render.java b/core/src/main/java/com/databasir/core/render/Render.java index d79f9ca..07a661f 100644 --- a/core/src/main/java/com/databasir/core/render/Render.java +++ b/core/src/main/java/com/databasir/core/render/Render.java @@ -1,7 +1,7 @@ package com.databasir.core.render; import com.databasir.core.meta.pojo.DatabaseMeta; -import com.databasir.core.render.markdown.MarkdownRender; +import com.databasir.core.render.markdown.MarkdownTemplateRender; import java.io.IOException; import java.io.OutputStream; @@ -11,6 +11,7 @@ public interface Render { void rendering(DatabaseMeta meta, OutputStream outputStream) throws IOException; static Render markdownRender(RenderConfig configuration) { - return MarkdownRender.of(configuration); + return new MarkdownTemplateRender(configuration); } + } diff --git a/core/src/main/java/com/databasir/core/render/markdown/MarkdownTemplateRender.java b/core/src/main/java/com/databasir/core/render/markdown/MarkdownTemplateRender.java new file mode 100644 index 0000000..826a9d5 --- /dev/null +++ b/core/src/main/java/com/databasir/core/render/markdown/MarkdownTemplateRender.java @@ -0,0 +1,57 @@ +package com.databasir.core.render.markdown; + +import com.databasir.core.meta.pojo.DatabaseMeta; +import com.databasir.core.render.Render; +import com.databasir.core.render.RenderConfig; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; +import freemarker.template.TemplateExceptionHandler; +import lombok.RequiredArgsConstructor; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.HashMap; +import java.util.Map; + +/** + * use freemarker template to render markdown + */ +@RequiredArgsConstructor +public class MarkdownTemplateRender implements Render { + + private final RenderConfig renderConfig; + + private String templatePath = "template/render/markdown/markdown.ftlh"; + + public MarkdownTemplateRender(RenderConfig config, String templatePath) { + this(config); + this.templatePath = templatePath; + } + + @Override + public void rendering(DatabaseMeta meta, OutputStream outputStream) throws IOException { + doRendering(meta, outputStream); + } + + public void doRendering(DatabaseMeta meta, OutputStream outputStream) throws IOException { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_29); + cfg.setClassForTemplateLoading(getClass(), "/"); + cfg.setDefaultEncoding("UTF-8"); + cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + cfg.setLogTemplateExceptions(false); + cfg.setWrapUncheckedExceptions(true); + cfg.setFallbackOnNullLoopVariable(false); + + Map root = new HashMap<>(); + root.put("meta", meta); + root.put("config", renderConfig); + Template template = cfg.getTemplate(templatePath); + try { + template.process(root, new OutputStreamWriter(outputStream)); + } catch (TemplateException e) { + throw new IllegalStateException(e); + } + } +} diff --git a/core/src/main/resources/template/render/markdown/markdown.ftlh b/core/src/main/resources/template/render/markdown/markdown.ftlh new file mode 100644 index 0000000..ab8169c --- /dev/null +++ b/core/src/main/resources/template/render/markdown/markdown.ftlh @@ -0,0 +1,44 @@ +# ${meta.databaseName} + +| seq | name | comment | +| ---- | --------------- | ------ | +<#list meta.tables as table> +| ${table_index+1} | [${table.tableName}](#${table.tableName}) | ${table.tableComment!'N/A'} | + + +<#if config.renderTables> + <#list meta.tables as table> +## ${table.tableName} + + <#if config.renderColumns> +### Columns + +| seq | name | type | nullable | auto increment| default | comment | +| ---- | ------ | ------ | ------ | -------- | ------ | ------ | +<#list table.columns as column> +| ${column_index+1} | ${column.name} | ${column.type} | ${column.isNullable?then('YES','')} | ${column.isAutoIncrement?then('YES', '')} | ${column.isNullable?then(column.defaultValue!'NULL', column.defaultValue!'')} | ${column.comment!''} | + + + + <#if config.renderIndexes> +### Indexes + +| seq | name | unique | primary key | columns | +| ---- | ---- | -------- | -------- | ------ | +<#list table.indexes as index> +| ${index_index+1} | ${index.indexName} | ${index.isUniqueKey?then('YES', '')} | ${index.isPrimaryKey?then('YES','')} | ${index.columnNames?join(', ')} | + + + + <#if config.renderTriggers> +### Triggers + +| seq | name | timing | statement | created | +| ---- | ---- | -------- | --------- | -------- | + <#list table.triggers as trigger> +| ${trigger_index} | ${trigger.name} | ${trigger.timing + " " + trigger.manipulation } | ${trigger.statement?replace("\n", "
")?replace("\r", " ")} | ${trigger.createAt} | + + + + + \ No newline at end of file diff --git a/core/src/test/java/App.java b/core/src/test/java/App.java index f06a5a4..4515334 100644 --- a/core/src/test/java/App.java +++ b/core/src/test/java/App.java @@ -1,5 +1,6 @@ import com.databasir.core.Databasir; import com.databasir.core.meta.pojo.DatabaseMeta; +import org.junit.jupiter.api.Test; import java.io.FileOutputStream; import java.io.IOException; @@ -9,7 +10,9 @@ import java.sql.SQLException; import java.util.Properties; public class App { - public static void main(String[] args) throws SQLException, ClassNotFoundException { + + @Test + public void testRenderAsMarkdown() throws SQLException, ClassNotFoundException { try (FileOutputStream out = new FileOutputStream("user.md")) { Connection connection = getJdbcConnection(); Databasir databasir = Databasir.of();