Browse Source

feat: use freemarker to render markdown

master
vran 3 years ago
parent
commit
75a5412c2b
  1. 4
      core/build.gradle
  2. 28
      core/src/main/java/com/databasir/core/Databasir.java
  3. 12
      core/src/main/java/com/databasir/core/DatabasirConfig.java
  4. 3
      core/src/main/java/com/databasir/core/meta/pojo/ColumnMeta.java
  5. 3
      core/src/main/java/com/databasir/core/meta/repository/impl/jdbc/JdbcColumnMetaRepository.java
  6. 5
      core/src/main/java/com/databasir/core/render/Render.java
  7. 57
      core/src/main/java/com/databasir/core/render/markdown/MarkdownTemplateRender.java
  8. 44
      core/src/main/resources/template/render/markdown/markdown.ftlh
  9. 5
      core/src/test/java/App.java

4
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'
}

28
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);
}

12
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<String> ignoreTableNameRegex = new HashSet<>();

3
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;

3
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)

5
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);
}
}

57
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<String, Object> 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);
}
}
}

44
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'} |
</#list>
<#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!''} |
</#list>
</#if>
<#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(', ')} |
</#list>
</#if>
<#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", "<br>")?replace("\r", " ")} | ${trigger.createAt} |
</#list>
</#if>
</#list>
</#if>

5
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();

Loading…
Cancel
Save