|
@@ -9,11 +9,20 @@ import com.xzl.web.model.dataGovernance.dto.DataReplicationParam;
|
|
|
import com.xzl.web.model.dataGovernance.dto.SysDataLogQueryParam;
|
|
|
import com.xzl.web.model.dataGovernance.entity.*;
|
|
|
import com.xzl.web.service.DataGovernanceService;
|
|
|
+import org.apache.ibatis.io.Resources;
|
|
|
+import org.apache.ibatis.jdbc.ScriptRunner;
|
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.Driver;
|
|
|
+import java.sql.DriverManager;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -25,6 +34,18 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
private DataGovernanceMapper dataGovernanceMapper;
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SqlSessionTemplate sqlSessionTemplate;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.druid.master.url}")
|
|
|
+ private String url;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.druid.master.username}")
|
|
|
+ private String username;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.druid.master.password}")
|
|
|
+ private String password;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public List<DatabaseInfo> getDatabaseList() {
|
|
@@ -33,17 +54,17 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
|
|
|
@Override
|
|
|
public List<TableInfo> getTableListByDatabaseName(String databaseName) {
|
|
|
- Map<String,String> param=new HashMap<>();
|
|
|
- param.put("databaseName",databaseName);
|
|
|
+ Map<String, String> param = new HashMap<>();
|
|
|
+ param.put("databaseName", databaseName);
|
|
|
return dataGovernanceMapper.getTableListByDatabaseName(param);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Map<String, String> createDatabase(String databaseName) {
|
|
|
- Map<String,String> result = new HashMap<>();
|
|
|
- if(verifySQLName(databaseName)){
|
|
|
- result.put("message","数据库名称不符合规则");
|
|
|
- result.put("type","error");
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+ if (verifySQLName(databaseName)) {
|
|
|
+ result.put("message", "数据库名称不符合规则");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -51,67 +72,67 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
dataGovernanceMapper.createDatabase(databaseName);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- result.put("message","创建失败!");
|
|
|
- result.put("type","error");
|
|
|
+ result.put("message", "创建失败!");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- result.put("message","创建成功!");
|
|
|
- result.put("type","success");
|
|
|
+ result.put("message", "创建成功!");
|
|
|
+ result.put("type", "success");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Map<String, String> dropDatabase(List<String> list) {
|
|
|
- Map<String,String> result = new HashMap<>();
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
try {
|
|
|
for (String databaseName : list) {
|
|
|
dataGovernanceMapper.dropDatabase(databaseName);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- result.put("message","删除失败!");
|
|
|
- result.put("type","error");
|
|
|
+ result.put("message", "删除失败!");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- result.put("message","删除成功!");
|
|
|
- result.put("type","success");
|
|
|
+ result.put("message", "删除成功!");
|
|
|
+ result.put("type", "success");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Map<String, String> createTable(CreateTableDTO createTableDTO) {
|
|
|
- Map<String,String> result = new HashMap<>();
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
String tableName = createTableDTO.getTableName();
|
|
|
- if(verifySQLName(tableName)){
|
|
|
- result.put("message","数据表名称不符合规则");
|
|
|
- result.put("type","error");
|
|
|
+ if (verifySQLName(tableName)) {
|
|
|
+ result.put("message", "数据表名称不符合规则");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
List<TableColumnParam> columns = createTableDTO.getColumns();
|
|
|
for (TableColumnParam column : columns) {
|
|
|
- if(verifySQLName(column.getName())){
|
|
|
- result.put("message","字段名称不符合规则");
|
|
|
- result.put("type","error");
|
|
|
+ if (verifySQLName(column.getName())) {
|
|
|
+ result.put("message", "字段名称不符合规则");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- if(!column.getType().equals("text") && column.getLength().isEmpty()){
|
|
|
- result.put("message","类型为varchar或int时长度不能为空!");
|
|
|
- result.put("type","error");
|
|
|
+ if (!column.getType().equals("text") && column.getLength().isEmpty()) {
|
|
|
+ result.put("message", "类型为varchar或int时长度不能为空!");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- if(!column.getType().equals("text") && Integer.parseInt(column.getLength())>255){
|
|
|
+ if (!column.getType().equals("text") && Integer.parseInt(column.getLength()) > 255) {
|
|
|
column.setLength("255");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- Map<String,String> param=new HashMap<>();
|
|
|
- param.put("databaseName",createTableDTO.getDatabaseName());
|
|
|
- param.put("tableName",createTableDTO.getTableName());
|
|
|
+ Map<String, String> param = new HashMap<>();
|
|
|
+ param.put("databaseName", createTableDTO.getDatabaseName());
|
|
|
+ param.put("tableName", createTableDTO.getTableName());
|
|
|
List<TableInfo> list = dataGovernanceMapper.getTableListByDatabaseName(param);
|
|
|
- if(list.size() >0){
|
|
|
- result.put("message","数据表名称已存在!请更换名称!");
|
|
|
- result.put("type","error");
|
|
|
+ if (list.size() > 0) {
|
|
|
+ result.put("message", "数据表名称已存在!请更换名称!");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -119,40 +140,40 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
dataGovernanceMapper.createTable(createTableDTO);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- result.put("message","创建失败!");
|
|
|
- result.put("type","error");
|
|
|
+ result.put("message", "创建失败!");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- result.put("message","创建成功!");
|
|
|
- result.put("type","success");
|
|
|
+ result.put("message", "创建成功!");
|
|
|
+ result.put("type", "success");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Map<String, String> dropTable(List<String> list) {
|
|
|
- Map<String,String> result = new HashMap<>();
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
try {
|
|
|
for (String tableName : list) {
|
|
|
dataGovernanceMapper.dropTable(tableName);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- result.put("message","删除失败!");
|
|
|
- result.put("type","error");
|
|
|
+ result.put("message", "删除失败!");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- result.put("message","删除成功!");
|
|
|
- result.put("type","success");
|
|
|
+ result.put("message", "删除成功!");
|
|
|
+ result.put("type", "success");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Map> getTableTitleList(String tableName) {
|
|
|
- Map<String,String> param=new HashMap<>();
|
|
|
+ Map<String, String> param = new HashMap<>();
|
|
|
String[] strings = tableName.split("\\.");
|
|
|
- param.put("dataBaseName",strings[0]);
|
|
|
- param.put("tableName",strings[1]);
|
|
|
+ param.put("dataBaseName", strings[0]);
|
|
|
+ param.put("tableName", strings[1]);
|
|
|
return dataGovernanceMapper.getTableTitleListByTableName(param);
|
|
|
}
|
|
|
|
|
@@ -163,52 +184,52 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
|
|
|
@Override
|
|
|
public Map<String, String> dataReplication(DataReplicationParam param) {
|
|
|
- Map<String,String> result = new HashMap<>();
|
|
|
- if(verifySQLName(param.getResourceDatabaseName()) && verifySQLName(param.getResourceTableName()) && verifySQLName(param.getTargetDatabaseName()) && verifySQLName(param.getTargetTableName())){
|
|
|
- result.put("message","传入参数不符合规则!");
|
|
|
- result.put("type","error");
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+ if (verifySQLName(param.getResourceDatabaseName()) && verifySQLName(param.getResourceTableName()) && verifySQLName(param.getTargetDatabaseName()) && verifySQLName(param.getTargetTableName())) {
|
|
|
+ result.put("message", "传入参数不符合规则!");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- Map<String,String> resource = new HashMap<>();
|
|
|
- Map<String,String> target = new HashMap<>();
|
|
|
- resource.put("databaseName",param.getResourceDatabaseName());
|
|
|
- resource.put("tableName",param.getResourceTableName());
|
|
|
- target.put("databaseName",param.getTargetDatabaseName());
|
|
|
- target.put("tableName",param.getTargetTableName());
|
|
|
+ Map<String, String> resource = new HashMap<>();
|
|
|
+ Map<String, String> target = new HashMap<>();
|
|
|
+ resource.put("databaseName", param.getResourceDatabaseName());
|
|
|
+ resource.put("tableName", param.getResourceTableName());
|
|
|
+ target.put("databaseName", param.getTargetDatabaseName());
|
|
|
+ target.put("tableName", param.getTargetTableName());
|
|
|
List<TableStructure> resourceTableStructure = dataGovernanceMapper.getTableStructure(resource);
|
|
|
List<TableStructure> targetTableStructure = dataGovernanceMapper.getTableStructure(target);
|
|
|
- if(resourceTableStructure.size()!=targetTableStructure.size()){
|
|
|
- result.put("message","复制失败!选择的表结构不同");
|
|
|
- result.put("type","error");
|
|
|
+ if (resourceTableStructure.size() != targetTableStructure.size()) {
|
|
|
+ result.put("message", "复制失败!选择的表结构不同");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
- for(int i=0;i<resourceTableStructure.size();i++){
|
|
|
- if(resourceTableStructure.get(0)!=targetTableStructure.get(0)){
|
|
|
- result.put("message","复制失败!选择的表结构不同");
|
|
|
- result.put("type","error");
|
|
|
+ for (int i = 0; i < resourceTableStructure.size(); i++) {
|
|
|
+ if (resourceTableStructure.get(0) != targetTableStructure.get(0)) {
|
|
|
+ result.put("message", "复制失败!选择的表结构不同");
|
|
|
+ result.put("type", "error");
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
dataGovernanceMapper.dataReplication(param);
|
|
|
- result.put("message","复制完成!");
|
|
|
- result.put("type","success");
|
|
|
+ result.put("message", "复制完成!");
|
|
|
+ result.put("type", "success");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Result<SysDataLog> getDataLog(SysDataLogQueryParam param) {
|
|
|
- Result<SysDataLog> result= new Result<>();
|
|
|
+ Result<SysDataLog> result = new Result<>();
|
|
|
PageParam pageParam = param.getPageParam();
|
|
|
result.setPageParam(pageParam);
|
|
|
- Map<String,String> queryParam=new HashMap<>();
|
|
|
+ Map<String, String> queryParam = new HashMap<>();
|
|
|
Integer page = null;
|
|
|
- if(pageParam.getPageNum()==1){
|
|
|
- page=0;
|
|
|
- }else{
|
|
|
- page=(pageParam.getPageNum()-1) * pageParam.getPageSize();
|
|
|
+ if (pageParam.getPageNum() == 1) {
|
|
|
+ page = 0;
|
|
|
+ } else {
|
|
|
+ page = (pageParam.getPageNum() - 1) * pageParam.getPageSize();
|
|
|
}
|
|
|
queryParam.put("page", String.valueOf(page));
|
|
|
- queryParam.put("pageSize",String.valueOf(pageParam.getPageSize()));
|
|
|
+ queryParam.put("pageSize", String.valueOf(pageParam.getPageSize()));
|
|
|
queryParam.put("actionType", param.getActionType());
|
|
|
Integer logCount = dataGovernanceMapper.getSysDataLogCount(queryParam);
|
|
|
result.setCount(logCount);
|
|
@@ -217,8 +238,45 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, String> importData(String databaseName, MultipartFile file) throws Exception {
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+ Connection conn = null;
|
|
|
+ //第一步以?号分割字符串
|
|
|
+ String[] urlSplit = url.split("\\?");
|
|
|
+ //第二步将?号前的内容以/号分割,然后弃用最后一段使用databaseName形成新的url
|
|
|
+ String[] urlSplit2 = urlSplit[0].split("/");
|
|
|
+ StringBuilder newUrlBuilder = new StringBuilder();
|
|
|
+ if (!verifySQLName(databaseName)) {
|
|
|
+ newUrlBuilder.append(urlSplit2[0]);
|
|
|
+ newUrlBuilder.append("//");
|
|
|
+ newUrlBuilder.append(urlSplit2[2]);
|
|
|
+ newUrlBuilder.append("/");
|
|
|
+ newUrlBuilder.append(databaseName);
|
|
|
+ newUrlBuilder.append("?");
|
|
|
+ newUrlBuilder.append(urlSplit[1]);
|
|
|
+ }
|
|
|
+ String newUrl = newUrlBuilder.toString();
|
|
|
+ InputStreamReader isr = null;
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ isr = new InputStreamReader(inputStream);
|
|
|
+ conn = DriverManager.getConnection(newUrl, username, password);
|
|
|
+ ScriptRunner scriptRunner = new ScriptRunner(conn);
|
|
|
+ // 设置编码,防止中文乱码
|
|
|
+ Resources.setCharset(Charset.forName("UTF-8"));
|
|
|
+ // 必须为true,不然容易报错
|
|
|
+ scriptRunner.setSendFullScript(true);
|
|
|
+ // 执行
|
|
|
+ scriptRunner.runScript(isr);
|
|
|
+ scriptRunner.closeConnection();
|
|
|
+ isr.close();
|
|
|
+ result.put("message", "复制失败!选择的表结构不同");
|
|
|
+ result.put("type", "error");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- private Boolean verifySQLName(String text){
|
|
|
+ private Boolean verifySQLName(String text) {
|
|
|
return text.isEmpty() || !text.matches("^[a-zA-Z_][a-zA-Z0-9_]{0,63}$");
|
|
|
}
|
|
|
}
|