🎉 H2 数据库支持、优化代码

This commit is contained in:
SanLi
2021-06-06 17:55:19 +08:00
parent 4a8c84f97b
commit 6ed6a3e194
7 changed files with 46 additions and 59 deletions

View File

@@ -55,8 +55,4 @@ public class EngineConfig implements Serializable {
* 文件名称 * 文件名称
*/ */
private String fileName; private String fileName;
/**
* 模板内容
*/
private String templateContent;
} }

View File

@@ -24,17 +24,16 @@ import cn.smallbun.screw.core.metadata.model.DataModel;
import cn.smallbun.screw.core.util.Assert; import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.ExceptionUtils; import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.StringUtils; import cn.smallbun.screw.core.util.StringUtils;
import java.util.HashMap;
import org.apache.velocity.Template; import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity; import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.log.NullLogChute;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import org.apache.velocity.runtime.log.NullLogChute;
import static cn.smallbun.screw.core.constant.DefaultConstants.DEFAULT_ENCODING; import static cn.smallbun.screw.core.constant.DefaultConstants.DEFAULT_ENCODING;
import static cn.smallbun.screw.core.engine.EngineTemplateType.velocity; import static cn.smallbun.screw.core.engine.EngineTemplateType.velocity;
@@ -92,24 +91,21 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
@Override @Override
public void produce(DataModel info, String docName) throws ProduceException { public void produce(DataModel info, String docName) throws ProduceException {
Assert.notNull(info, "DataModel can not be empty!"); Assert.notNull(info, "DataModel can not be empty!");
Template template = null; Template template;
try { try {
String templateContent = getEngineConfig().getTemplateContent(); // get template path
if (templateContent != null) { String path = getEngineConfig().getCustomTemplate();
// get template path //如果自定义了模板
String path = getEngineConfig().getCustomTemplate(); if (StringUtils.isNotBlank(path)) {
//如果自定义了模板 template = velocityEngine.getTemplate(path, DEFAULT_ENCODING);
if (StringUtils.isNotBlank(path)) { }
template = velocityEngine.getTemplate(path, DEFAULT_ENCODING); //没有自定义模板,使用核心包自带
} else {
//没有自定义模板,使用核心包自带 template = velocityEngine
else { .getTemplate(velocity.getTemplateDir()
template = velocityEngine + getEngineConfig().getFileType().getTemplateNamePrefix()
.getTemplate(velocity.getTemplateDir() + velocity.getSuffix(),
+ getEngineConfig().getFileType().getTemplateNamePrefix() DEFAULT_ENCODING);
+ velocity.getSuffix(),
DEFAULT_ENCODING);
}
} }
// output // output
try (FileOutputStream outStream = new FileOutputStream(getFile(docName)); try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
@@ -118,21 +114,8 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
//put data //put data
VelocityContext context = new VelocityContext(); VelocityContext context = new VelocityContext();
context.put(DATA, info); context.put(DATA, info);
context.put("markdown", new HashMap<String, String>() {
{
put("h1", "#");
put("h2", "##");
put("h3", "###");
put("h4", "####");
put("h5", "#####");
}
});
//generate //generate
if (template != null) { template.merge(context, sw);
template.merge(context, sw);
} else {
velocityEngine.evaluate(context, sw, "test", templateContent);
}
// open the output directory // open the output directory
openOutputDir(); openOutputDir();
} }

View File

@@ -121,6 +121,8 @@ public class Mapping {
} }
/** /**
* 根据列标签获取列信息
*
* @param resultSet {@link ResultSet} 对象 * @param resultSet {@link ResultSet} 对象
* @param clazz 领域类型 * @param clazz 领域类型
* @param <T> 领域泛型 * @param <T> 领域泛型
@@ -128,7 +130,7 @@ public class Mapping {
* @throws MappingException MappingException * @throws MappingException MappingException
*/ */
public static <T> List<T> convertListByColumnLabel(ResultSet resultSet, public static <T> List<T> convertListByColumnLabel(ResultSet resultSet,
Class<T> clazz) throws MappingException { Class<T> clazz) throws MappingException {
//存放列名和结果 //存放列名和结果
List<Map<String, Object>> values = new ArrayList<>(16); List<Map<String, Object>> values = new ArrayList<>(16);
//结果集合 //结果集合

View File

@@ -74,7 +74,7 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
public Database getDataBase() throws QueryException { public Database getDataBase() throws QueryException {
Db2DatabaseModel model = new Db2DatabaseModel(); Db2DatabaseModel model = new Db2DatabaseModel();
model.setDatabase(StringUtils.trim(this.getSchema())); model.setDatabase(StringUtils.trim(this.getSchema()));
return model; return model;
} }
/** /**
@@ -88,13 +88,13 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
try { try {
//查询 //查询
resultSet = getMetaData().getTables(getCatalog(), getSchema(), PERCENT_SIGN, resultSet = getMetaData().getTables(getCatalog(), getSchema(), PERCENT_SIGN,
new String[] { "TABLE" }); new String[] { "TABLE" });
//映射 //映射
return Mapping.convertList(resultSet, Db2TableModel.class); return Mapping.convertList(resultSet, Db2TableModel.class);
} catch (SQLException e) { } catch (SQLException e) {
throw ExceptionUtils.mpe(e); throw ExceptionUtils.mpe(e);
} finally { } finally {
JdbcUtils.close(resultSet,connection); JdbcUtils.close(resultSet, connection);
} }
} }
@@ -111,26 +111,30 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
//查询所有 //查询所有
List<Db2ColumnModel> list = null; List<Db2ColumnModel> list;
if(PERCENT_SIGN.equals(table)){ if (PERCENT_SIGN.equals(table)) {
List<String> tableNames = getTables().stream().map(Table::getTableName) List<String> tableNames = getTables().stream().map(Table::getTableName)
.collect(Collectors.toList()).stream().distinct().collect(Collectors.toList()); .collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//db2 getMetaData().getColumns 中 无法获取表名,循环单张表获取 //db2 getMetaData().getColumns 中 无法获取表名,循环单张表获取
list = new ArrayList<>(); list = new ArrayList<>();
for (String tableName : tableNames) { for (String tableName : tableNames) {
list.addAll(getTableColumns(tableName)); list.addAll(getTableColumns(tableName));
} }
}else { } else {
if(!this.columnsCaching.containsKey(table)){ if (!this.columnsCaching.containsKey(table)) {
//查询 //查询
resultSet = getMetaData().getColumns(getCatalog(), getSchema(), table, PERCENT_SIGN); resultSet = getMetaData().getColumns(getCatalog(), getSchema(), table,
PERCENT_SIGN);
//映射 //映射
list = Mapping.convertList(resultSet, Db2ColumnModel.class); list = Mapping.convertList(resultSet, Db2ColumnModel.class);
//单表查询 //单表查询
String sql = "SELECT COLNAME as NAME,TABNAME as TABLE_NAME,REMARKS,LENGTH as COLUMN_LENGTH, TYPENAME ||'('|| LENGTH ||')' as COLUMN_TYPE,SCALE as DECIMAL_DIGITS FROM SYSCAT.COLUMNS WHERE TABSCHEMA='"+StringUtils.trim(getSchema())+"' and TABNAME = '%s' ORDER BY COLNO"; String sql = "SELECT COLNAME as NAME,TABNAME as TABLE_NAME,REMARKS,LENGTH as COLUMN_LENGTH, TYPENAME ||'('|| LENGTH ||')' as COLUMN_TYPE,SCALE as DECIMAL_DIGITS FROM SYSCAT.COLUMNS WHERE TABSCHEMA='"
+ StringUtils.trim(getSchema())
+ "' and TABNAME = '%s' ORDER BY COLNO";
resultSet = prepareStatement(String.format(sql, table)).executeQuery(); resultSet = prepareStatement(String.format(sql, table)).executeQuery();
//db2 ColumnName 获取的不是 as column 值,使用ColumnLabel获取 //db2 ColumnName 获取的不是 as column 值,使用ColumnLabel获取
List<Db2ColumnModel> inquires = Mapping.convertListByColumnLabel(resultSet, Db2ColumnModel.class); List<Db2ColumnModel> inquires = Mapping.convertListByColumnLabel(resultSet,
Db2ColumnModel.class);
for (Db2ColumnModel i : list) { for (Db2ColumnModel i : list) {
inquires.forEach(j -> { inquires.forEach(j -> {
//列名一致 //列名一致
@@ -141,16 +145,18 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
i.setColumnType(j.getColumnType()); i.setColumnType(j.getColumnType());
i.setTableName(j.getTableName()); i.setTableName(j.getTableName());
i.setDecimalDigits(j.getDecimalDigits()); i.setDecimalDigits(j.getDecimalDigits());
if("NO".equals(i.getNullable())){ if ("NO".equals(i.getNullable())) {
i.setNullable("0"); i.setNullable("0");
} }
} }
}); });
} }
this.columnsCaching.put(table,list.stream() this.columnsCaching.put(table,
.filter(i -> StringUtils.isNotBlank(i.getColumnName())).collect(Collectors.toList())); list.stream().filter(i -> StringUtils.isNotBlank(i.getColumnName()))
}else{ .collect(Collectors.toList()));
list = this.columnsCaching.get(table).stream().map(c -> (Db2ColumnModel)c).collect(Collectors.toList()); } else {
list = this.columnsCaching.get(table).stream().map(c -> (Db2ColumnModel) c)
.collect(Collectors.toList());
} }
} }
return list; return list;
@@ -206,7 +212,8 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
try { try {
// 由于单条循环查询存在性能问题所以这里通过自定义SQL查询数据库主键信息 // 由于单条循环查询存在性能问题所以这里通过自定义SQL查询数据库主键信息
String sql = "SELECT TABSCHEMA as TABLE_SCHEM, TABNAME as TABLE_NAME ,COLNAME as COLUMN_NAME,KEYSEQ as KEY_SEQ FROM SYSCAT.COLUMNS WHERE TABSCHEMA = '%s' AND KEYSEQ IS NOT NULL ORDER BY KEYSEQ"; String sql = "SELECT TABSCHEMA as TABLE_SCHEM, TABNAME as TABLE_NAME ,COLNAME as COLUMN_NAME,KEYSEQ as KEY_SEQ FROM SYSCAT.COLUMNS WHERE TABSCHEMA = '%s' AND KEYSEQ IS NOT NULL ORDER BY KEYSEQ";
resultSet = prepareStatement(String.format(sql, getDataBase().getDatabase())).executeQuery(); resultSet = prepareStatement(String.format(sql, getDataBase().getDatabase()))
.executeQuery();
return Mapping.convertListByColumnLabel(resultSet, Db2PrimaryKeyModel.class); return Mapping.convertListByColumnLabel(resultSet, Db2PrimaryKeyModel.class);
} catch (SQLException e) { } catch (SQLException e) {
throw new QueryException(e); throw new QueryException(e);
@@ -215,5 +222,4 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
} }
} }
} }

View File

@@ -15,7 +15,7 @@
<dependency> <dependency>
<groupId>cn.smallbun.screw</groupId> <groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId> <artifactId>screw-core</artifactId>
<version>1.0.5</version> <version>${project.version}</version>
</dependency> </dependency>
<!--lombok--> <!--lombok-->
<dependency> <dependency>

View File

@@ -45,7 +45,7 @@ public interface TypeDialect {
return null; return null;
} }
type = type.toLowerCase(); type = type.toLowerCase();
if (type.startsWith("date")) { if (type.startsWith("date")) {
return map.get("date"); return map.get("date");
} }

View File

@@ -18,7 +18,7 @@
<dependency> <dependency>
<groupId>cn.smallbun.screw</groupId> <groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId> <artifactId>screw-core</artifactId>
<version>1.0.5</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.maven</groupId> <groupId>org.apache.maven</groupId>