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 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.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,10 +91,8 @@ 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();
|
|
||||||
if (templateContent != null) {
|
|
||||||
// get template path
|
// get template path
|
||||||
String path = getEngineConfig().getCustomTemplate();
|
String path = getEngineConfig().getCustomTemplate();
|
||||||
//如果自定义了模板
|
//如果自定义了模板
|
||||||
@@ -110,7 +107,6 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
+ velocity.getSuffix(),
|
+ velocity.getSuffix(),
|
||||||
DEFAULT_ENCODING);
|
DEFAULT_ENCODING);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// output
|
// output
|
||||||
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
|
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(outStream, DEFAULT_ENCODING);
|
OutputStreamWriter writer = new OutputStreamWriter(outStream, DEFAULT_ENCODING);
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,6 +121,8 @@ public class Mapping {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 根据列标签获取列信息
|
||||||
|
*
|
||||||
* @param resultSet {@link ResultSet} 对象
|
* @param resultSet {@link ResultSet} 对象
|
||||||
* @param clazz 领域类型
|
* @param clazz 领域类型
|
||||||
* @param <T> 领域泛型
|
* @param <T> 领域泛型
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ 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());
|
||||||
@@ -123,14 +123,18 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
|
|||||||
} 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 -> {
|
||||||
//列名一致
|
//列名一致
|
||||||
@@ -147,10 +151,12 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
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()))
|
||||||
|
.collect(Collectors.toList()));
|
||||||
} else {
|
} 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;
|
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 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user