mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-08 18:10:26 +08:00
feat: init api (#2)
This commit is contained in:
5
plugin/build.gradle
Normal file
5
plugin/build.gradle
Normal file
@@ -0,0 +1,5 @@
|
||||
dependencies {
|
||||
testImplementation 'mysql:mysql-connector-java:8.0.27'
|
||||
implementation 'org.commonmark:commonmark:0.18.1'
|
||||
implementation 'org.freemarker:freemarker:2.3.31'
|
||||
}
|
74
plugin/src/main/java/com/databasir/core/Databasir.java
Normal file
74
plugin/src/main/java/com/databasir/core/Databasir.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package com.databasir.core;
|
||||
|
||||
import com.databasir.core.meta.data.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;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.sql.Connection;
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public class Databasir {
|
||||
|
||||
private final DatabasirConfig config;
|
||||
|
||||
public Optional<DatabaseMeta> get(Connection connection, String databaseName) {
|
||||
Condition condition = Condition.builder()
|
||||
.databaseName(databaseName)
|
||||
.ignoreTableNameRegex(config.getIgnoreTableNameRegex())
|
||||
.ignoreTableColumnNameRegex(config.getIgnoreTableColumnNameRegex())
|
||||
.build();
|
||||
return config.getDatabaseMetaRepository().select(connection, condition);
|
||||
}
|
||||
|
||||
public void renderAsMarkdown(DatabaseMeta meta, OutputStream out) throws IOException {
|
||||
renderAsMarkdown(new RenderConfig(), meta, out);
|
||||
}
|
||||
|
||||
public void renderAsMarkdown(RenderConfig config, DatabaseMeta meta, OutputStream stream) throws IOException {
|
||||
Render.markdownRender(config).rendering(meta, stream);
|
||||
}
|
||||
|
||||
public static Databasir of() {
|
||||
return of(new DatabasirConfig());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
37
plugin/src/main/java/com/databasir/core/DatabasirConfig.java
Normal file
37
plugin/src/main/java/com/databasir/core/DatabasirConfig.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.databasir.core;
|
||||
|
||||
import com.databasir.core.meta.repository.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class DatabasirConfig {
|
||||
|
||||
private IndexMetaRepository indexMetaRepository;
|
||||
|
||||
private TriggerMetaRepository triggerMetaRepository;
|
||||
|
||||
private ColumnMetaRepository columnMetaRepository;
|
||||
|
||||
private TableMetaRepository tableMetaRepository;
|
||||
|
||||
private DatabaseMetaRepository databaseMetaRepository;
|
||||
|
||||
private Collection<String> ignoreTableNameRegex = new HashSet<>();
|
||||
|
||||
private Collection<String> ignoreTableColumnNameRegex = new HashSet<>();
|
||||
|
||||
public DatabasirConfig ignoreTable(String tableNameRegex) {
|
||||
ignoreTableNameRegex.add(tableNameRegex);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DatabasirConfig ignoreColumn(String columnNameRegex) {
|
||||
ignoreTableColumnNameRegex.add(columnNameRegex);
|
||||
return this;
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.databasir.core.meta.data;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class ColumnMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
private String comment;
|
||||
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* if default value is empty string, will be converted to ''.
|
||||
*/
|
||||
private String defaultValue;
|
||||
|
||||
private Integer size;
|
||||
|
||||
private Integer decimalDigits;
|
||||
|
||||
private String nullable;
|
||||
|
||||
private String autoIncrement;
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package com.databasir.core.meta.data;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class DatabaseMeta {
|
||||
|
||||
/**
|
||||
* product_name
|
||||
*/
|
||||
private String productName;
|
||||
|
||||
/**
|
||||
* product_version
|
||||
*/
|
||||
private String productVersion;
|
||||
|
||||
/**
|
||||
* driver_name
|
||||
*/
|
||||
private String driverName;
|
||||
|
||||
/**
|
||||
* driver_version
|
||||
*/
|
||||
private String driverVersion;
|
||||
|
||||
/**
|
||||
* database_name
|
||||
*/
|
||||
private String databaseName;
|
||||
|
||||
@Builder.Default
|
||||
private List<TableMeta> tables = Collections.emptyList();
|
||||
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.databasir.core.meta.data;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class IndexMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
@Builder.Default
|
||||
private List<String> columnNames = Collections.emptyList();
|
||||
|
||||
private Boolean isPrimaryKey;
|
||||
|
||||
private Boolean isUniqueKey;
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.databasir.core.meta.data;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class TableMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
|
||||
private String comment;
|
||||
|
||||
@Builder.Default
|
||||
private List<ColumnMeta> columns = Collections.emptyList();
|
||||
|
||||
@Builder.Default
|
||||
private List<TriggerMeta> triggers = Collections.emptyList();
|
||||
|
||||
@Builder.Default
|
||||
private List<IndexMeta> indexes = Collections.emptyList();
|
||||
|
||||
private String remark;
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package com.databasir.core.meta.data;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* now: only support mysql, postgresql.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class TriggerMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* example: BEFORE, AFTER
|
||||
*/
|
||||
private String timing;
|
||||
|
||||
/**
|
||||
* example: INSERT, UPDATE
|
||||
*/
|
||||
private String manipulation;
|
||||
|
||||
private String statement;
|
||||
|
||||
private String createAt;
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package com.databasir.core.meta.provider;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* TODO use to extension repository
|
||||
*/
|
||||
public interface SqlProvider {
|
||||
|
||||
Default DEFAULT = new Default();
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* generate sql to select database information, should return the follow columns
|
||||
* </p>
|
||||
*
|
||||
* <table>
|
||||
* <tr>
|
||||
* <th> column name </th>
|
||||
* <th> column type </th>
|
||||
* <th> description </th>
|
||||
* <th> nullable </th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td> TABLE_CAT </td>
|
||||
* <td> String </td>
|
||||
* <td> catalog name </td>
|
||||
* <td> NO </td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* <br>
|
||||
*
|
||||
* @param databaseName
|
||||
* @return
|
||||
*/
|
||||
default Optional<String> databaseMetaSql(String databaseName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* generate sql to select table information, should return the follow columns
|
||||
* <table>
|
||||
* <tr>
|
||||
* <th> column name </th>
|
||||
* <th> column type </th>
|
||||
* <th> description </th>
|
||||
* <th> nullable </th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td> TABLE_CAT </td>
|
||||
* <td> String </td>
|
||||
* <td> catalog name </td>
|
||||
* <td> NO </td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* @param databaseName
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
default Optional<String> tableMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<String> tableColumnMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<String> tableIndexMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<String> tableTriggerMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
class Default implements SqlProvider {
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.data.ColumnMeta;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface ColumnMetaRepository {
|
||||
|
||||
List<ColumnMeta> selectColumns(Connection connection, TableCondition condition);
|
||||
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.data.DatabaseMeta;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface DatabaseMetaRepository {
|
||||
|
||||
Optional<DatabaseMeta> select(Connection connection, Condition condition);
|
||||
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.data.IndexMeta;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface IndexMetaRepository {
|
||||
|
||||
List<IndexMeta> selectIndexes(Connection connection, TableCondition condition);
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.data.TableMeta;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface TableMetaRepository {
|
||||
|
||||
List<TableMeta> selectTables(Connection connection, Condition condition);
|
||||
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.data.TriggerMeta;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface TriggerMetaRepository {
|
||||
|
||||
List<TriggerMeta> selectTriggers(Connection connection, TableCondition condition);
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.databasir.core.meta.repository.condition;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class Condition {
|
||||
|
||||
@NonNull
|
||||
private String databaseName;
|
||||
|
||||
@Builder.Default
|
||||
private Collection<String> ignoreTableNameRegex = Collections.emptyList();
|
||||
|
||||
@Builder.Default
|
||||
private Collection<String> ignoreTableColumnNameRegex = Collections.emptyList();
|
||||
|
||||
public boolean tableIsIgnored(String tableName) {
|
||||
return ignoreTableNameRegex.stream().anyMatch(regex -> Pattern.matches(regex, tableName));
|
||||
}
|
||||
|
||||
public boolean columnIsIgnored(String column) {
|
||||
return ignoreTableColumnNameRegex.stream().anyMatch(regex -> Pattern.matches(regex, column));
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package com.databasir.core.meta.repository.condition;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TableCondition extends Condition {
|
||||
|
||||
@NonNull
|
||||
private String tableName;
|
||||
|
||||
public static TableCondition of(Condition condition, String tableName) {
|
||||
return TableCondition.builder()
|
||||
.databaseName(condition.getDatabaseName())
|
||||
.tableName(tableName)
|
||||
.ignoreTableNameRegex(condition.getIgnoreTableNameRegex())
|
||||
.ignoreTableColumnNameRegex(condition.getIgnoreTableColumnNameRegex())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package com.databasir.core.meta.repository.impl.extension;
|
||||
|
||||
import com.databasir.core.meta.data.TriggerMeta;
|
||||
import com.databasir.core.meta.repository.TriggerMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class MysqlTableTriggerMetaRepository implements TriggerMetaRepository {
|
||||
|
||||
@Override
|
||||
public List<TriggerMeta> selectTriggers(Connection connection, TableCondition condition) {
|
||||
String sql = "SELECT TRIGGER_CATALOG,\n" +
|
||||
" TRIGGER_SCHEMA,\n" +
|
||||
" TRIGGER_NAME,\n" +
|
||||
" EVENT_MANIPULATION,\n" +
|
||||
" EVENT_OBJECT_CATALOG,\n" +
|
||||
" EVENT_OBJECT_SCHEMA,\n" +
|
||||
" EVENT_OBJECT_TABLE,\n" +
|
||||
" ACTION_ORDER,\n" +
|
||||
" ACTION_CONDITION,\n" +
|
||||
" ACTION_STATEMENT,\n" +
|
||||
" ACTION_ORIENTATION,\n" +
|
||||
" ACTION_TIMING,\n" +
|
||||
" ACTION_REFERENCE_OLD_TABLE,\n" +
|
||||
" ACTION_REFERENCE_NEW_TABLE,\n" +
|
||||
" ACTION_REFERENCE_OLD_ROW,\n" +
|
||||
" ACTION_REFERENCE_NEW_ROW,\n" +
|
||||
" CREATED,\n" +
|
||||
" SQL_MODE,\n" +
|
||||
" DEFINER\n " +
|
||||
"FROM information_schema.TRIGGERS WHERE EVENT_OBJECT_SCHEMA = ? AND EVENT_OBJECT_TABLE = ?";
|
||||
try {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setObject(1, condition.getDatabaseName());
|
||||
preparedStatement.setObject(2, condition.getTableName());
|
||||
ResultSet results = preparedStatement.executeQuery();
|
||||
List<TriggerMeta> triggers = new ArrayList<>();
|
||||
while (results.next()) {
|
||||
String name = results.getString("TRIGGER_NAME");
|
||||
String statement = results.getString("ACTION_STATEMENT");
|
||||
String timing = results.getString("ACTION_TIMING");
|
||||
String manipulation = results.getString("EVENT_MANIPULATION");
|
||||
String created = results.getString("CREATED");
|
||||
TriggerMeta meta = TriggerMeta.builder()
|
||||
.name(name)
|
||||
.manipulation(manipulation)
|
||||
.timing(timing)
|
||||
.statement(statement)
|
||||
.createAt(created)
|
||||
.build();
|
||||
triggers.add(meta);
|
||||
}
|
||||
return triggers;
|
||||
} catch (SQLException e) {
|
||||
log.warn("create trigger doc failed", e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.data.ColumnMeta;
|
||||
import com.databasir.core.meta.repository.ColumnMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class JdbcColumnMetaRepository implements ColumnMetaRepository {
|
||||
|
||||
@Override
|
||||
public List<ColumnMeta> selectColumns(Connection connection, TableCondition tableCondition) {
|
||||
try {
|
||||
return doSelect(connection, tableCondition);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ColumnMeta> doSelect(Connection connection, TableCondition tableCondition) throws SQLException {
|
||||
List<ColumnMeta> columnDocs = new ArrayList<>();
|
||||
String databaseName = tableCondition.getDatabaseName();
|
||||
String tableName = tableCondition.getTableName();
|
||||
ResultSet columnsResult;
|
||||
try {
|
||||
columnsResult = connection.getMetaData().getColumns(databaseName, null, tableName, null);
|
||||
} catch (SQLException e) {
|
||||
log.warn("warn: ignore columns in " + databaseName + "." + tableName);
|
||||
return columnDocs;
|
||||
}
|
||||
while (columnsResult.next()) {
|
||||
String columnName = columnsResult.getString("COLUMN_NAME");
|
||||
if (tableCondition.columnIsIgnored(columnName)) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("ignore column: " + columnName);
|
||||
}
|
||||
} else {
|
||||
String columnType = columnsResult.getString("TYPE_NAME");
|
||||
Integer columnSize = columnsResult.getInt("COLUMN_SIZE");
|
||||
Integer decimalDigits;
|
||||
if (columnsResult.getObject("DECIMAL_DIGITS") == null) {
|
||||
decimalDigits = null;
|
||||
} else {
|
||||
decimalDigits = columnsResult.getInt("DECIMAL_DIGITS");
|
||||
}
|
||||
String defaultValue = columnsResult.getString("COLUMN_DEF");
|
||||
String isNullable = columnsResult.getString("IS_NULLABLE");
|
||||
if (isNullable.trim().equals("")) {
|
||||
isNullable = "UNKNOWN";
|
||||
}
|
||||
String isAutoIncrement = columnsResult.getString("IS_AUTOINCREMENT");
|
||||
if (isAutoIncrement.trim().equals("")) {
|
||||
isAutoIncrement = "UNKNOWN";
|
||||
}
|
||||
String columnComment = columnsResult.getString("REMARKS");
|
||||
if (defaultValue != null && defaultValue.trim().equals("")) {
|
||||
defaultValue = "'" + defaultValue + "'";
|
||||
}
|
||||
ColumnMeta columnMeta = ColumnMeta.builder()
|
||||
.name(columnName)
|
||||
.type(columnType)
|
||||
.size(columnSize)
|
||||
.decimalDigits(decimalDigits)
|
||||
.nullable(isNullable)
|
||||
.autoIncrement(isAutoIncrement)
|
||||
.comment(columnComment)
|
||||
.defaultValue(defaultValue)
|
||||
.build();
|
||||
columnDocs.add(columnMeta);
|
||||
}
|
||||
|
||||
}
|
||||
return columnDocs;
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.data.DatabaseMeta;
|
||||
import com.databasir.core.meta.data.TableMeta;
|
||||
import com.databasir.core.meta.repository.DatabaseMetaRepository;
|
||||
import com.databasir.core.meta.repository.TableMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class JdbcDatabaseMetaRepository implements DatabaseMetaRepository {
|
||||
|
||||
private final TableMetaRepository tableMetaRepository;
|
||||
|
||||
@Override
|
||||
public Optional<DatabaseMeta> select(Connection connection, Condition condition) {
|
||||
try {
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
ResultSet catalogs = metaData.getCatalogs();
|
||||
while (catalogs.next()) {
|
||||
String catalogName = catalogs.getString("TABLE_CAT");
|
||||
if (Objects.equals(condition.getDatabaseName(), catalogName)) {
|
||||
List<TableMeta> tableDocs = tableMetaRepository.selectTables(connection, condition);
|
||||
DatabaseMeta meta = DatabaseMeta.builder()
|
||||
.productName(metaData.getDatabaseProductName())
|
||||
.productVersion(metaData.getDatabaseProductVersion())
|
||||
.databaseName(catalogName)
|
||||
.tables(tableDocs)
|
||||
.build();
|
||||
return Optional.of(meta);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.data.IndexMeta;
|
||||
import com.databasir.core.meta.repository.IndexMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class JdbcIndexMetaRepository implements IndexMetaRepository {
|
||||
@Override
|
||||
public List<IndexMeta> selectIndexes(Connection connection, TableCondition condition) {
|
||||
try {
|
||||
return doCreateIndexDocs(connection, condition);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<IndexMeta> doCreateIndexDocs(Connection connection, TableCondition condition)
|
||||
throws SQLException {
|
||||
String databaseName = condition.getDatabaseName();
|
||||
String tableName = condition.getTableName();
|
||||
List<IndexMeta> indexMetas = new ArrayList<>();
|
||||
ResultSet indexResults;
|
||||
try {
|
||||
indexResults = connection.getMetaData().getIndexInfo(databaseName, null, tableName, false, false);
|
||||
} catch (SQLException e) {
|
||||
log.warn("warn: ignore " + databaseName + "." + tableName);
|
||||
return indexMetas;
|
||||
}
|
||||
|
||||
Map<String, IndexMeta> pojoGroupByName = new HashMap<>();
|
||||
while (indexResults.next()) {
|
||||
Boolean nonUnique = indexResults.getBoolean("NON_UNIQUE");
|
||||
String indexName = indexResults.getString("INDEX_NAME");
|
||||
String columnName = indexResults.getString("COLUMN_NAME");
|
||||
if (pojoGroupByName.containsKey(indexName)) {
|
||||
pojoGroupByName.get(indexName).getColumnNames().add(columnName);
|
||||
} else {
|
||||
List<String> columns = new ArrayList<>();
|
||||
columns.add(columnName);
|
||||
IndexMeta indexMeta = IndexMeta.builder()
|
||||
.name(indexName)
|
||||
.columnNames(columns)
|
||||
.isPrimaryKey(Objects.equals("PRIMARY", indexName))
|
||||
.isUniqueKey(Objects.equals(nonUnique, false))
|
||||
.build();
|
||||
pojoGroupByName.put(indexName, indexMeta);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(pojoGroupByName.values());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.data.ColumnMeta;
|
||||
import com.databasir.core.meta.data.TableMeta;
|
||||
import com.databasir.core.meta.repository.ColumnMetaRepository;
|
||||
import com.databasir.core.meta.repository.IndexMetaRepository;
|
||||
import com.databasir.core.meta.repository.TableMetaRepository;
|
||||
import com.databasir.core.meta.repository.TriggerMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdbcTableMetaRepository implements TableMetaRepository {
|
||||
|
||||
private final ColumnMetaRepository columnMetaRepository;
|
||||
|
||||
private final IndexMetaRepository indexMetaRepository;
|
||||
|
||||
private final TriggerMetaRepository triggerMetaRepository;
|
||||
|
||||
@Override
|
||||
public List<TableMeta> selectTables(Connection connection, Condition condition) {
|
||||
try {
|
||||
return doSelect(connection, condition);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<TableMeta> doSelect(Connection connection, Condition condition) throws SQLException {
|
||||
List<TableMeta> tableMetas = new ArrayList<>();
|
||||
String databaseName = condition.getDatabaseName();
|
||||
ResultSet tablesResult = connection.getMetaData()
|
||||
.getTables(databaseName, null, null, new String[]{"TABLE"});
|
||||
while (tablesResult.next()) {
|
||||
String tableName = tablesResult.getString("TABLE_NAME");
|
||||
if (condition.tableIsIgnored(tableName)) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("ignored table: " + databaseName + "." + tableName);
|
||||
}
|
||||
} else {
|
||||
String tableType = tablesResult.getString("TABLE_TYPE");
|
||||
String tableComment = tablesResult.getString("REMARKS");
|
||||
TableCondition tableCondition = TableCondition.of(condition, tableName);
|
||||
List<ColumnMeta> columns = columnMetaRepository.selectColumns(connection, tableCondition);
|
||||
if (columns.isEmpty()) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("ignored table: " + databaseName + "." + tableName + ", caused by get empty columns");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
TableMeta tableMeta = TableMeta.builder()
|
||||
.name(tableName)
|
||||
.type(tableType)
|
||||
.comment(tableComment)
|
||||
.columns(columns)
|
||||
.indexes(indexMetaRepository.selectIndexes(connection, tableCondition))
|
||||
.triggers(triggerMetaRepository.selectTriggers(connection, tableCondition))
|
||||
.build();
|
||||
tableMetas.add(tableMeta);
|
||||
}
|
||||
}
|
||||
return tableMetas;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.data.TriggerMeta;
|
||||
import com.databasir.core.meta.repository.TriggerMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class JdbcTriggerMetaRepository implements TriggerMetaRepository {
|
||||
|
||||
@Override
|
||||
public List<TriggerMeta> selectTriggers(Connection connection, TableCondition condition) {
|
||||
// note: jdbc not support get triggers
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
17
plugin/src/main/java/com/databasir/core/render/Render.java
Normal file
17
plugin/src/main/java/com/databasir/core/render/Render.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.databasir.core.render;
|
||||
|
||||
import com.databasir.core.meta.data.DatabaseMeta;
|
||||
import com.databasir.core.render.markdown.MarkdownTemplateRender;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public interface Render {
|
||||
|
||||
void rendering(DatabaseMeta meta, OutputStream outputStream) throws IOException;
|
||||
|
||||
static Render markdownRender(RenderConfig configuration) {
|
||||
return new MarkdownTemplateRender(configuration);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
package com.databasir.core.render;
|
||||
|
||||
import com.databasir.core.meta.data.ColumnMeta;
|
||||
import com.databasir.core.meta.data.IndexMeta;
|
||||
import com.databasir.core.meta.data.TriggerMeta;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Data
|
||||
public class RenderConfig {
|
||||
|
||||
private Boolean renderTables = true;
|
||||
|
||||
private Boolean renderColumns = true;
|
||||
|
||||
private Boolean renderIndexes = true;
|
||||
|
||||
private Boolean renderTriggers = true;
|
||||
|
||||
private LinkedHashMap<String, Function<ColumnMeta, String>> columnTitleAndValueMapping = columnTitleAndValueMapping();
|
||||
|
||||
private LinkedHashMap<String, Function<IndexMeta, String>> indexTitleAndValueMapping = indexTitleAndValueMapping();
|
||||
|
||||
private LinkedHashMap<String, Function<TriggerMeta, String>> triggerTitleAndValueMapping = triggerTitleAndValueMapping();
|
||||
|
||||
protected LinkedHashMap<String, Function<ColumnMeta, String>> columnTitleAndValueMapping() {
|
||||
LinkedHashMap<String, Function<ColumnMeta, String>> mapping = new LinkedHashMap<>();
|
||||
mapping.put("Name", ColumnMeta::getName);
|
||||
mapping.put("Type", column -> {
|
||||
String type;
|
||||
if (column.getDecimalDigits() == null || column.getDecimalDigits().equals(0)) {
|
||||
type = column.getType()
|
||||
+ "(" + column.getSize().toString() + ")";
|
||||
} else {
|
||||
type = column.getType()
|
||||
+ "(" + column.getSize().toString() + ", " + column.getDecimalDigits().toString() + ")";
|
||||
}
|
||||
return type;
|
||||
});
|
||||
// mapping.put("Not Null", column -> column.getIsNullable() ? "" : "YES");
|
||||
// mapping.put("Auto Increment", column -> column.getIsAutoIncrement() ? "YES" : "");
|
||||
// mapping.put("Default", column -> {
|
||||
// if (column.getDefaultValue() == null) {
|
||||
// return "";
|
||||
// }
|
||||
// if (column.getDefaultValue().trim().equals("")) {
|
||||
// return "'" + column.getDefaultValue() + "'";
|
||||
// }
|
||||
// return column.getDefaultValue();
|
||||
// });
|
||||
// mapping.put("Comment", ColumnMeta::getComment);
|
||||
return mapping;
|
||||
}
|
||||
|
||||
protected LinkedHashMap<String, Function<IndexMeta, String>> indexTitleAndValueMapping() {
|
||||
LinkedHashMap<String, Function<IndexMeta, String>> mapping = new LinkedHashMap<>();
|
||||
mapping.put("Name", IndexMeta::getName);
|
||||
mapping.put("IsPrimary", index -> index.getIsPrimaryKey() ? "YES" : "");
|
||||
mapping.put("IsUnique", index -> index.getIsUniqueKey() ? "YES" : "");
|
||||
mapping.put("Columns", index -> String.join(", ", index.getColumnNames()));
|
||||
return mapping;
|
||||
}
|
||||
|
||||
protected LinkedHashMap<String, Function<TriggerMeta, String>> triggerTitleAndValueMapping() {
|
||||
LinkedHashMap<String, Function<TriggerMeta, String>> mapping = new LinkedHashMap<>();
|
||||
mapping.put("Name", TriggerMeta::getName);
|
||||
mapping.put("Timing", trigger -> trigger.getTiming() + " " + trigger.getManipulation());
|
||||
mapping.put("Statement", trigger -> trigger.getStatement().replace("\n", " ")
|
||||
.replace("\r", " "));
|
||||
mapping.put("Create At", TriggerMeta::getCreateAt);
|
||||
return mapping;
|
||||
}
|
||||
}
|
@@ -0,0 +1,108 @@
|
||||
package com.databasir.core.render.markdown;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MarkdownBuilder {
|
||||
|
||||
private static final String LINE = "\n";
|
||||
|
||||
private static final String DOUBLE_LINE = LINE + LINE;
|
||||
|
||||
private StringBuilder builder = new StringBuilder(1024);
|
||||
|
||||
private MarkdownBuilder() {
|
||||
}
|
||||
|
||||
public static MarkdownBuilder builder() {
|
||||
return new MarkdownBuilder();
|
||||
}
|
||||
|
||||
public MarkdownBuilder primaryTitle(String title) {
|
||||
builder.append("# ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder secondTitle(String title) {
|
||||
builder.append("## ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder thirdTitle(String title) {
|
||||
builder.append("### ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder text(String text) {
|
||||
builder.append(text).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder table(List<String> titles, List<List<String>> rows) {
|
||||
if (titles == null || titles.isEmpty()) {
|
||||
throw new IllegalArgumentException("titles must not be null or empty");
|
||||
}
|
||||
// build titles
|
||||
builder.append("| ");
|
||||
for (String title : titles) {
|
||||
builder.append(title).append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
|
||||
// build separators
|
||||
builder.append("| ");
|
||||
for (String title : titles) {
|
||||
builder.append("------").append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
|
||||
// build rows
|
||||
for (List<String> row : rows) {
|
||||
builder.append("| ");
|
||||
for (String column : row) {
|
||||
builder.append(column).append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder orderedList(List<String> list) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
builder.append(i + 1).append(". ").append(list.get(i)).append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder unorderedList(List<String> list) {
|
||||
for (String item : list) {
|
||||
builder.append("- ").append(item).append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder blockquotes(String content) {
|
||||
builder.append("> ").append(content).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder code(String languageType, String statement) {
|
||||
builder.append("```").append(languageType).append(LINE)
|
||||
.append(statement)
|
||||
.append("```")
|
||||
.append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder link(String text, String link) {
|
||||
builder.append("[").append(text).append("]")
|
||||
.append("(").append(link).append(")");
|
||||
return this;
|
||||
}
|
||||
|
||||
public String build() {
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,114 @@
|
||||
package com.databasir.core.render.markdown;
|
||||
|
||||
import com.databasir.core.meta.data.DatabaseMeta;
|
||||
import com.databasir.core.meta.data.TableMeta;
|
||||
import com.databasir.core.render.Render;
|
||||
import com.databasir.core.render.RenderConfig;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MarkdownRender implements Render {
|
||||
|
||||
@Getter
|
||||
private final RenderConfig config;
|
||||
|
||||
protected MarkdownRender(RenderConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public static MarkdownRender of(RenderConfig config) {
|
||||
return new MarkdownRender(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rendering(DatabaseMeta meta,
|
||||
OutputStream outputStream) throws IOException {
|
||||
MarkdownBuilder contentBuilder = MarkdownBuilder.builder();
|
||||
contentBuilder.primaryTitle(meta.getDatabaseName());
|
||||
if (config.getRenderTables()) {
|
||||
for (TableMeta table : meta.getTables()) {
|
||||
buildTableName(contentBuilder, table);
|
||||
if (config.getRenderColumns()) {
|
||||
buildColumns(contentBuilder, table);
|
||||
}
|
||||
if (config.getRenderIndexes()) {
|
||||
buildIndexes(contentBuilder, table);
|
||||
}
|
||||
if (config.getRenderTriggers()) {
|
||||
buildTriggers(contentBuilder, table);
|
||||
}
|
||||
}
|
||||
}
|
||||
outputStream.write(contentBuilder.build().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void buildTableName(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
String tableName;
|
||||
if (table.getComment() == null || table.getComment().trim().isEmpty()) {
|
||||
tableName = table.getName();
|
||||
} else {
|
||||
tableName = table.getName() + "(" + table.getComment() + ")";
|
||||
}
|
||||
contentBuilder.secondTitle(tableName);
|
||||
}
|
||||
|
||||
private void buildColumns(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
contentBuilder.unorderedList(Collections.singletonList("columns"));
|
||||
List<List<String>> allColumnRows = table.getColumns()
|
||||
.stream()
|
||||
.map(column -> config.getColumnTitleAndValueMapping()
|
||||
.values()
|
||||
.stream()
|
||||
.map(mapping -> mapping.apply(column))
|
||||
.collect(Collectors.toList()))
|
||||
.collect(Collectors.toList());
|
||||
contentBuilder.table(tableTitles(), allColumnRows);
|
||||
}
|
||||
|
||||
private void buildIndexes(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
contentBuilder.unorderedList(Collections.singletonList("indexes"));
|
||||
List<List<String>> allIndexRows = table.getIndexes().stream()
|
||||
.map(index -> config.getIndexTitleAndValueMapping()
|
||||
.values()
|
||||
.stream()
|
||||
.map(mapping -> mapping.apply(index))
|
||||
.collect(Collectors.toList()))
|
||||
.collect(Collectors.toList());
|
||||
contentBuilder.table(indexTitles(), allIndexRows);
|
||||
}
|
||||
|
||||
private void buildTriggers(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
if (table.getTriggers() == null || table.getTriggers().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
contentBuilder.unorderedList(Collections.singletonList("triggers"));
|
||||
List<List<String>> allRows = table.getTriggers().stream()
|
||||
.map(trigger -> config.getTriggerTitleAndValueMapping()
|
||||
.values()
|
||||
.stream()
|
||||
.map(mapping -> mapping.apply(trigger))
|
||||
.collect(Collectors.toList()))
|
||||
.collect(Collectors.toList());
|
||||
contentBuilder.table(triggerTitles(), allRows);
|
||||
}
|
||||
|
||||
private List<String> tableTitles() {
|
||||
return new ArrayList<>(config.getColumnTitleAndValueMapping().keySet());
|
||||
}
|
||||
|
||||
private List<String> indexTitles() {
|
||||
return new ArrayList<>(config.getIndexTitleAndValueMapping().keySet());
|
||||
}
|
||||
|
||||
private List<String> triggerTitles() {
|
||||
return new ArrayList<>(config.getTriggerTitleAndValueMapping().keySet());
|
||||
}
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
package com.databasir.core.render.markdown;
|
||||
|
||||
import com.databasir.core.meta.data.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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
# ${meta.databaseName}
|
||||
|
||||
| seq | name | comment |
|
||||
| ---- | --------------- | ------ |
|
||||
<#list meta.tables as table>
|
||||
| ${table_index+1} | [${table.name}](#${table.name}) | ${table.comment!'N/A'} |
|
||||
</#list>
|
||||
|
||||
<#if config.renderTables>
|
||||
<#list meta.tables as table>
|
||||
## ${table.name}
|
||||
|
||||
<#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','NO')} | ${column.isAutoIncrement?then('YES', 'NO')} | ${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.name} | ${index.isUniqueKey?then('YES', 'NO')} | ${index.isPrimaryKey?then('YES','NO')} | ${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>
|
39
plugin/src/test/java/App.java
Normal file
39
plugin/src/test/java/App.java
Normal file
@@ -0,0 +1,39 @@
|
||||
import com.databasir.core.Databasir;
|
||||
import com.databasir.core.meta.data.DatabaseMeta;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class App {
|
||||
|
||||
@Test
|
||||
public void testRenderAsMarkdown() throws SQLException, ClassNotFoundException {
|
||||
try (FileOutputStream out = new FileOutputStream("user.md")) {
|
||||
Connection connection = getJdbcConnection();
|
||||
Databasir databasir = Databasir.of();
|
||||
DatabaseMeta doc = databasir.get(connection, "user").orElseThrow();
|
||||
databasir.renderAsMarkdown(doc, out);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Connection getJdbcConnection() throws SQLException, ClassNotFoundException {
|
||||
// get database connection
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
Properties info = new Properties();
|
||||
info.put("user", "root");
|
||||
info.put("password", "123456");
|
||||
// this config is used by mysql
|
||||
info.put("useInformationSchema", "true");
|
||||
|
||||
String url = "jdbc:mysql://localhost:3306/patient?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true";
|
||||
return DriverManager.getConnection(url, info);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user