🎉 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 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.ExceptionUtils;
import cn.smallbun.screw.core.util.StringUtils;
import java.util.HashMap;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.log.NullLogChute;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
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.engine.EngineTemplateType.velocity;
@@ -92,10 +91,8 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
@Override
public void produce(DataModel info, String docName) throws ProduceException {
Assert.notNull(info, "DataModel can not be empty!");
Template template = null;
Template template;
try {
String templateContent = getEngineConfig().getTemplateContent();
if (templateContent != null) {
// get template path
String path = getEngineConfig().getCustomTemplate();
//如果自定义了模板
@@ -110,7 +107,6 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
+ velocity.getSuffix(),
DEFAULT_ENCODING);
}
}
// output
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
OutputStreamWriter writer = new OutputStreamWriter(outStream, DEFAULT_ENCODING);
@@ -118,21 +114,8 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
//put data
VelocityContext context = new VelocityContext();
context.put(DATA, info);
context.put("markdown", new HashMap<String, String>() {
{
put("h1", "#");
put("h2", "##");
put("h3", "###");
put("h4", "####");
put("h5", "#####");
}
});
//generate
if (template != null) {
template.merge(context, sw);
} else {
velocityEngine.evaluate(context, sw, "test", templateContent);
}
// open the output directory
openOutputDir();
}

View File

@@ -121,6 +121,8 @@ public class Mapping {
}
/**
* 根据列标签获取列信息
*
* @param resultSet {@link ResultSet} 对象
* @param clazz 领域类型
* @param <T> 领域泛型

View File

@@ -111,7 +111,7 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
ResultSet resultSet = null;
try {
//查询所有
List<Db2ColumnModel> list = null;
List<Db2ColumnModel> list;
if (PERCENT_SIGN.equals(table)) {
List<String> tableNames = getTables().stream().map(Table::getTableName)
.collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
@@ -123,14 +123,18 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
} else {
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);
//单表查询
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();
//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) {
inquires.forEach(j -> {
//列名一致
@@ -147,10 +151,12 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
}
});
}
this.columnsCaching.put(table,list.stream()
.filter(i -> StringUtils.isNotBlank(i.getColumnName())).collect(Collectors.toList()));
this.columnsCaching.put(table,
list.stream().filter(i -> StringUtils.isNotBlank(i.getColumnName()))
.collect(Collectors.toList()));
} else {
list = this.columnsCaching.get(table).stream().map(c -> (Db2ColumnModel)c).collect(Collectors.toList());
list = this.columnsCaching.get(table).stream().map(c -> (Db2ColumnModel) c)
.collect(Collectors.toList());
}
}
return list;
@@ -206,7 +212,8 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
try {
// 由于单条循环查询存在性能问题所以这里通过自定义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";
resultSet = prepareStatement(String.format(sql, getDataBase().getDatabase())).executeQuery();
resultSet = prepareStatement(String.format(sql, getDataBase().getDatabase()))
.executeQuery();
return Mapping.convertListByColumnLabel(resultSet, Db2PrimaryKeyModel.class);
} catch (SQLException e) {
throw new QueryException(e);
@@ -215,5 +222,4 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
}
}
}

View File

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

View File

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