forked from lxm_tools/screw
🎉 H2 数据库支持、优化代码
This commit is contained in:
@@ -55,8 +55,4 @@ public class EngineConfig implements Serializable {
|
||||
* 文件名称
|
||||
*/
|
||||
private String fileName;
|
||||
/**
|
||||
* 模板内容
|
||||
*/
|
||||
private String templateContent;
|
||||
}
|
||||
|
||||
@@ -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,24 +91,21 @@ 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();
|
||||
//如果自定义了模板
|
||||
if (StringUtils.isNotBlank(path)) {
|
||||
template = velocityEngine.getTemplate(path, DEFAULT_ENCODING);
|
||||
}
|
||||
//没有自定义模板,使用核心包自带
|
||||
else {
|
||||
template = velocityEngine
|
||||
.getTemplate(velocity.getTemplateDir()
|
||||
+ getEngineConfig().getFileType().getTemplateNamePrefix()
|
||||
+ velocity.getSuffix(),
|
||||
DEFAULT_ENCODING);
|
||||
}
|
||||
// get template path
|
||||
String path = getEngineConfig().getCustomTemplate();
|
||||
//如果自定义了模板
|
||||
if (StringUtils.isNotBlank(path)) {
|
||||
template = velocityEngine.getTemplate(path, DEFAULT_ENCODING);
|
||||
}
|
||||
//没有自定义模板,使用核心包自带
|
||||
else {
|
||||
template = velocityEngine
|
||||
.getTemplate(velocity.getTemplateDir()
|
||||
+ getEngineConfig().getFileType().getTemplateNamePrefix()
|
||||
+ velocity.getSuffix(),
|
||||
DEFAULT_ENCODING);
|
||||
}
|
||||
// output
|
||||
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
|
||||
@@ -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);
|
||||
}
|
||||
template.merge(context, sw);
|
||||
// open the output directory
|
||||
openOutputDir();
|
||||
}
|
||||
|
||||
@@ -121,6 +121,8 @@ public class Mapping {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据列标签获取列信息
|
||||
*
|
||||
* @param resultSet {@link ResultSet} 对象
|
||||
* @param clazz 领域类型
|
||||
* @param <T> 领域泛型
|
||||
@@ -128,7 +130,7 @@ public class Mapping {
|
||||
* @throws MappingException MappingException
|
||||
*/
|
||||
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);
|
||||
//结果集合
|
||||
|
||||
@@ -74,7 +74,7 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
|
||||
public Database getDataBase() throws QueryException {
|
||||
Db2DatabaseModel model = new Db2DatabaseModel();
|
||||
model.setDatabase(StringUtils.trim(this.getSchema()));
|
||||
return model;
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,13 +88,13 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
|
||||
try {
|
||||
//查询
|
||||
resultSet = getMetaData().getTables(getCatalog(), getSchema(), PERCENT_SIGN,
|
||||
new String[] { "TABLE" });
|
||||
new String[] { "TABLE" });
|
||||
//映射
|
||||
return Mapping.convertList(resultSet, Db2TableModel.class);
|
||||
} catch (SQLException e) {
|
||||
throw ExceptionUtils.mpe(e);
|
||||
} finally {
|
||||
JdbcUtils.close(resultSet,connection);
|
||||
JdbcUtils.close(resultSet, connection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,26 +111,30 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
//查询所有
|
||||
List<Db2ColumnModel> list = null;
|
||||
if(PERCENT_SIGN.equals(table)){
|
||||
List<Db2ColumnModel> list;
|
||||
if (PERCENT_SIGN.equals(table)) {
|
||||
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 中 无法获取表名,循环单张表获取
|
||||
list = new ArrayList<>();
|
||||
for (String tableName : tableNames) {
|
||||
list.addAll(getTableColumns(tableName));
|
||||
}
|
||||
}else {
|
||||
if(!this.columnsCaching.containsKey(table)){
|
||||
} 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 -> {
|
||||
//列名一致
|
||||
@@ -141,16 +145,18 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
|
||||
i.setColumnType(j.getColumnType());
|
||||
i.setTableName(j.getTableName());
|
||||
i.setDecimalDigits(j.getDecimalDigits());
|
||||
if("NO".equals(i.getNullable())){
|
||||
if ("NO".equals(i.getNullable())) {
|
||||
i.setNullable("0");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
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());
|
||||
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());
|
||||
}
|
||||
}
|
||||
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 {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -45,7 +45,7 @@ public interface TypeDialect {
|
||||
return null;
|
||||
}
|
||||
type = type.toLowerCase();
|
||||
|
||||
|
||||
if (type.startsWith("date")) {
|
||||
return map.get("date");
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user