提交 4e76eab8 编写于 作者: Tyl666's avatar Tyl666

【Liquibase逆向工程】第三版 补充字段内容

上级 d85166d0
......@@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import cn.ibizlab.core.lite.domain.MetaField;
import cn.ibizlab.core.lite.domain.MetaRelationship;
import cn.ibizlab.core.lite.extensions.domain.MetaEntityModel;
import cn.ibizlab.core.lite.extensions.domain.SysModel;
import cn.ibizlab.core.util.config.LiquibaseConfiguration;
......@@ -37,6 +38,7 @@ import java.io.InputStream;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@Slf4j
......@@ -106,14 +108,21 @@ public class DstLiquibaseService{
SysModel sysModel = generateSysModel(liquibaseChangeLogPath);
return sysModel;
}
/**
* 构建一个SysModel对象
* @param path 生成liquibaseChangeLog路径
*/
public SysModel generateSysModel(String path) {
public static SysModel generateSysModel(String path) {
List<MetaEntityModel> metaEntityModels = new CopyOnWriteArrayList<>();
SysModel sysModel = new SysModel();
Map<String, MetaRelationship> parentMap = null;
Map<String, MetaRelationship> subMap = null;
List<Map<String,MetaRelationship>> parentEntities = null;
List<Map<String,MetaRelationship>> subEntities = null;
MetaRelationship parent = null;
MetaRelationship sub = null;
MetaEntityModel metaEntityModel = null;
List<Map> commonMessage = null;
MetaField metaField = null;
......@@ -124,50 +133,108 @@ public class DstLiquibaseService{
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(ConvertXMLtoJSON(path));
head = (Map) jsonObject.get("databaseChangeLog");
commonMessage = (List<Map>) head.get("changeSet");
parentEntities = new CopyOnWriteArrayList<>();
subEntities = new CopyOnWriteArrayList<>();
// 维护父子键实体
for (int i = 0;i < commonMessage.size(); i++) {
temp = (Map) commonMessage.get(i).get("addForeignKeyConstraint");
if(!ObjectUtil.isEmpty(temp)){
// 存入父键map
parent = new MetaRelationship();
parentMap = new ConcurrentHashMap();
parent.setName("DER1N_"+temp.get("referencedTableName").toString().toUpperCase());
parent.setCodeName(temp.get("referencedColumnNames").toString());
parent.setEntityName(temp.get("referencedTableName").toString().toUpperCase());
parent.setRelationType("DER1N");
parent.setSystemId(commonMessage.get(0).get("author").toString());
parent.setRefEntityName(temp.get("baseTableName").toString().toUpperCase());
parentMap.put(temp.get("baseColumnNames").toString(),parent);
parentEntities.add(parentMap);
// 存入子键map
sub = new MetaRelationship();
subMap = new ConcurrentHashMap();
sub.setName("DER1N_"+temp.get("baseTableName").toString().toUpperCase());
sub.setCodeName(temp.get("baseColumnNames").toString());
sub.setEntityName(temp.get("baseTableName").toString().toUpperCase());
sub.setRelationType("DER1N");
sub.setSystemId(commonMessage.get(0).get("author").toString());
sub.setRefEntityName(temp.get("referencedTableName").toString().toUpperCase());
subMap.put(temp.get("referencedColumnNames").toString(),sub);
subEntities.add(subMap);
}
}
// 第一、二层 获取第一个作者以及id
if (!ObjectUtil.isEmpty(commonMessage)){
sysModel.setSystemid((String) commonMessage.get(0).get("author"));
sysModel.setSystemname((String) commonMessage.get(0).get("author"));
}
// 第三层 获取实体对象List 在这层可以放入subEntitys、parentEntitys等关系字段
// 第三层 获取实体对象List 在这层可以放入subEntitys、parentEntitys等关系字段
for (int i = 0;i < commonMessage.size(); i++) {
// createTable主要存储了表明以及字段属性,只要crateTable属性,其余剔除
temp = (Map) commonMessage.get(i).get("createTable");
if(ObjectUtil.isEmpty(temp)){
continue;
}
metaEntityModel = new MetaEntityModel();
// 为了获取MetaEntityModel下那些基本类型字段(此处可补充MetaEntityModel等描述字段)
metaEntityModel.setEntityName(temp.get("tableName") == null ? "" : temp.get("tableName").toString());
metaEntityModel.setCodeName(temp.get("tableName") == null ? "" : temp.get("tableName").toString());
metaEntityModel.setTableName(temp.get("tableName") == null ? "" : temp.get("tableName").toString());
metaEntityModel.setLogicName(temp.get("tableName") == null ? "" : temp.get("tableName").toString());
// 统一关联sysId
metaEntityModel.setEntityName(temp.get("tableName") == null ? "" : temp.get("tableName").toString().toUpperCase());
metaEntityModel.setCodeName(temp.get("tableName") == null ? "" : temp.get("tableName").toString().toUpperCase());
metaEntityModel.setTableName(temp.get("tableName") == null ? "" : temp.get("tableName").toString().toUpperCase());
metaEntityModel.setLogicName(temp.get("tableName") == null ? "" : temp.get("tableName").toString().toUpperCase());
metaEntityModel.setSystemId((String) commonMessage.get(0).get("author"));
// 属性List
column = (List<Map>) temp.get("column");
// 属性List
List<MetaField> metaFields = new CopyOnWriteArrayList<>();
List<MetaRelationship> parentRelationships = new CopyOnWriteArrayList<>();
List<MetaRelationship> subRelationships = new CopyOnWriteArrayList<>();
for (int j = 0; j < column.size();j++){
// (此处可补充MetaField描述字段,以及父子外键关系等)
metaField = new MetaField();
metaField.setFieldName(column.get(j).get("name") == null? "" : column.get(j).get("name").toString());
metaField.setDataType(column.get(j).get("type") == null? "" : fieldToName(column.get(j).get("type").toString()));
metaField.setDataLength(column.get(j).get("type") == null? 0 : fieldToLenth(column.get(j).get("type").toString()));
metaField.setNullable(column.get(j).get("constraints") != null ? 1 : 0);
metaField.setKeyField(column.get(j).get("primaryKey") != null ? 1 : 0);
metaField.setDataLength(column.get(j).get("type") == null? 0 : fieldToLength(column.get(j).get("type").toString()));
Map<String,List<Map>> map = (Map<String, List<Map>>) column.get(j).get("constraints");
if(!ObjectUtil.isEmpty(map)){
metaField.setNullable(map.get("nullable")==null?0:1);
metaField.setKeyField(map.get("primaryKey")==null?0:1);
}else {
metaField.setNullable(0);
metaField.setKeyField(0);
}
String name = column.get(j).get("name") == null? "" : column.get(j).get("name").toString();
// 若在父键表查有该数据且属于同一实体则记录一条
for (int l = 0; l < parentEntities.size();l++){
if(name.equals(parentEntities.get(l).keySet().toString().replaceAll("[\\[\\]]", ""))){
if(temp.get("tableName").toString().toUpperCase().equals(parentEntities.get(l).get(name).getRefEntityName())) {
parentEntities.get(l).get(name).setEntityId(metaField.getEntityId());
parentRelationships.add(parentEntities.get(l).get(name));
}
}
}
metaEntityModel.setParentEntitys(parentRelationships);
// 若在子键表查有该数据且属于同一实体则记录一条
for (int l = 0; l < subEntities.size();l++){
if(name.equals(subEntities.get(l).keySet().toString().replaceAll("[\\[\\]]", ""))){
if(temp.get("tableName").toString().toUpperCase().equals(subEntities.get(l).get(name).getRefEntityName())){
subRelationships.add(subEntities.get(l).get(name));
}
}
}
metaEntityModel.setSubEntitys(subRelationships);
metaField.setPhysicalField(1);
metaFields.add(metaField);
}
metaEntityModel.setFields(metaFields);
metaEntityModels.add(metaEntityModel);
sysModel.setEntities(metaEntityModels);
}
return sysModel;
}
private static Integer fieldToLenth(String s) {
private static Integer fieldToLength(String s) {
if(s.contains("(")) {
String newStr = s.substring(s.indexOf("(") + 1, s.indexOf(")"));
return Integer.valueOf(newStr);
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册