|
@@ -14,6 +14,7 @@ import com.xzl.web.utils.SqlHelper;
|
|
|
import org.apache.ibatis.io.Resources;
|
|
|
import org.apache.ibatis.jdbc.ScriptRunner;
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
+import org.apache.poi.ss.formula.functions.T;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -22,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.io.*;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.sql.*;
|
|
|
import java.util.ArrayList;
|
|
@@ -29,16 +31,24 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+
|
|
|
@Service
|
|
|
public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
|
|
|
+ //mapper层
|
|
|
@Autowired
|
|
|
private DataGovernanceMapper dataGovernanceMapper;
|
|
|
|
|
|
+ //数据库连接
|
|
|
private Connection connection;
|
|
|
|
|
|
- private Map<String,String> driver;
|
|
|
+ //驱动字符串集
|
|
|
+ private Map<String, String> driver;
|
|
|
+
|
|
|
+ //当前连接id
|
|
|
+ private String connectionId;
|
|
|
|
|
|
+ //项目默认连接模板
|
|
|
@Autowired
|
|
|
private SqlSessionFactory sqlSessionFactory;
|
|
|
|
|
@@ -54,49 +64,38 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
@Value("${sqlFilePath}")
|
|
|
private String sqlFilePath;
|
|
|
|
|
|
- private static final String NAME_SPACE="com.xzl.web.mapper.DataGovernanceMapper";
|
|
|
+ //sql对应的XML
|
|
|
+ private static final String NAME_SPACE = "com.xzl.web.mapper.DataGovernanceMapper";
|
|
|
|
|
|
|
|
|
+ //初始化方法
|
|
|
@PostConstruct
|
|
|
private void init() {
|
|
|
- DatabaseConfig databaseConfig = dataGovernanceMapper.getDatabaseConfig("2");
|
|
|
+ //初始化驱动字符串集
|
|
|
this.driverInit();
|
|
|
- try {
|
|
|
- Class.forName(driver.get(databaseConfig.getDatabaseType()));
|
|
|
- this.connection=DriverManager.getConnection(databaseConfig.getUrl(),databaseConfig.getUsername(), databaseConfig.getPassword());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ //初始化数据库连接
|
|
|
+ this.changeConnection(null);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前数据库连接下的数据库信息
|
|
|
+ *
|
|
|
+ * @return 数据库信息
|
|
|
+ */
|
|
|
@Override
|
|
|
public List<DatabaseInfo> getDatabaseList() {
|
|
|
-
|
|
|
- String sql = SqlHelper.getNamespaceSql(sqlSessionFactory, NAME_SPACE+".getDatabaseList", null);
|
|
|
- List<DatabaseInfo> data=new ArrayList<>();
|
|
|
- try {
|
|
|
- Statement statement = this.connection.createStatement();
|
|
|
- ResultSet resultSet = statement.executeQuery(sql);
|
|
|
- while (resultSet.next()) {
|
|
|
- DatabaseInfo databaseInfo=new DatabaseInfo();
|
|
|
- databaseInfo.getClass().getFields();
|
|
|
- databaseInfo.setDatabaseName(resultSet.getString(1));
|
|
|
- databaseInfo.setCharacterName(resultSet.getString(2));
|
|
|
- databaseInfo.setCollationName(resultSet.getString(3));
|
|
|
- data.add(databaseInfo);
|
|
|
- }
|
|
|
- System.out.println(data);
|
|
|
- } catch (SQLException throwable) {
|
|
|
- throwable.printStackTrace();
|
|
|
- }
|
|
|
- return dataGovernanceMapper.getDatabaseList();
|
|
|
+ //获取mapper中的sql语句
|
|
|
+ String sql = SqlHelper.getNamespaceSql(sqlSessionFactory, NAME_SPACE + ".getDatabaseList", null);
|
|
|
+ return this.getDataList(DatabaseInfo.class, sql);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<TableInfo> getTableListByDatabaseName(String databaseName) {
|
|
|
Map<String, String> param = new HashMap<>();
|
|
|
param.put("databaseName", databaseName);
|
|
|
- return dataGovernanceMapper.getTableListByDatabaseName(param);
|
|
|
+ String sql = SqlHelper.getNamespaceSql(sqlSessionFactory, NAME_SPACE + ".getTableListByDatabaseName", param);
|
|
|
+ return this.getDataList(TableInfo.class, sql);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -109,7 +108,7 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- dataGovernanceMapper.createDatabase(databaseName);
|
|
|
+ this.executeSql(this.getExecuteSql("createDatabase", databaseName));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
result.put("message", "创建失败!");
|
|
@@ -127,7 +126,7 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
try {
|
|
|
for (String databaseName : list) {
|
|
|
- dataGovernanceMapper.dropDatabase(databaseName);
|
|
|
+ this.executeSql(this.getExecuteSql("dropDatabase", databaseName));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -169,7 +168,8 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
Map<String, String> param = new HashMap<>();
|
|
|
param.put("databaseName", createTableDTO.getDatabaseName());
|
|
|
param.put("tableName", createTableDTO.getTableName());
|
|
|
- List<TableInfo> list = dataGovernanceMapper.getTableListByDatabaseName(param);
|
|
|
+
|
|
|
+ List<TableInfo> list = this.getDataList(TableInfo.class, this.getExecuteSql("getTableListByDatabaseName", param));
|
|
|
if (list.size() > 0) {
|
|
|
result.put("message", "数据表名称已存在!请更换名称!");
|
|
|
result.put("type", "error");
|
|
@@ -177,7 +177,7 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- dataGovernanceMapper.createTable(createTableDTO);
|
|
|
+ this.executeSql(this.getExecuteSql("createTable", createTableDTO));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
result.put("message", "创建失败!");
|
|
@@ -195,7 +195,7 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
try {
|
|
|
for (String tableName : list) {
|
|
|
- dataGovernanceMapper.dropTable(tableName);
|
|
|
+ this.executeSql(this.getExecuteSql("dropTable", tableName));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -214,7 +214,7 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
String[] strings = tableName.split("\\.");
|
|
|
param.put("dataBaseName", strings[0]);
|
|
|
param.put("tableName", strings[1]);
|
|
|
- return dataGovernanceMapper.getTableTitleListByTableName(param);
|
|
|
+ return this.getDataList(String.class, this.getExecuteSql("getTableTitleListByTableName", param));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -232,9 +232,9 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
queryParam.put("page", String.valueOf(page));
|
|
|
queryParam.put("pageSize", String.valueOf(pageParam.getPageSize()));
|
|
|
queryParam.put("tableName", param.getTableName());
|
|
|
- Integer count = dataGovernanceMapper.getDataCount(param.getTableName());
|
|
|
+ Integer count = this.getData(Integer.class, this.getExecuteSql("getDataCount", param.getTableName()));
|
|
|
result.setCount(count);
|
|
|
- List<Map> tableData = dataGovernanceMapper.getTableData(queryParam);
|
|
|
+ List<Map> tableData = this.getDataList(Map.class, this.getExecuteSql("getTableData", queryParam));
|
|
|
result.setTableData(tableData);
|
|
|
return result;
|
|
|
}
|
|
@@ -253,8 +253,8 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
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);
|
|
|
+ List<TableStructure> resourceTableStructure = this.getDataList(TableStructure.class, this.getExecuteSql("getTableStructure", resource));
|
|
|
+ List<TableStructure> targetTableStructure = this.getDataList(TableStructure.class, this.getExecuteSql("getTableStructure", target));
|
|
|
if (resourceTableStructure.size() != targetTableStructure.size()) {
|
|
|
result.put("message", "复制失败!选择的表结构不同");
|
|
|
result.put("type", "error");
|
|
@@ -267,7 +267,7 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
- dataGovernanceMapper.dataReplication(param);
|
|
|
+ this.executeSql(this.getExecuteSql("dataReplication", param));
|
|
|
result.put("message", "复制完成!");
|
|
|
result.put("type", "success");
|
|
|
return result;
|
|
@@ -288,9 +288,9 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
queryParam.put("page", String.valueOf(page));
|
|
|
queryParam.put("pageSize", String.valueOf(pageParam.getPageSize()));
|
|
|
queryParam.put("actionType", param.getActionType());
|
|
|
- Integer logCount = dataGovernanceMapper.getSysDataLogCount(queryParam);
|
|
|
+ Integer logCount = this.getData(Integer.class, this.getExecuteSql("getSysDataLogCount", queryParam));
|
|
|
result.setCount(logCount);
|
|
|
- List<SysDataLog> logList = dataGovernanceMapper.getSysDataLogList(queryParam);
|
|
|
+ List<SysDataLog> logList = this.getDataList(SysDataLog.class, this.getExecuteSql("getSysDataLogList", queryParam));
|
|
|
result.setTableData(logList);
|
|
|
return result;
|
|
|
}
|
|
@@ -337,13 +337,13 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
String table = databaseName + "." + tableName;
|
|
|
Map<String, String> createTableSql = dataGovernanceMapper.getCreateTableSql(table);
|
|
|
Map<String, String> param = new HashMap<>();
|
|
|
- param.put("tableName",table);
|
|
|
+ param.put("tableName", table);
|
|
|
List<Map> tableData = dataGovernanceMapper.getTableData(param);
|
|
|
- param=new HashMap<>();
|
|
|
+ param = new HashMap<>();
|
|
|
param.put("dataBaseName", databaseName);
|
|
|
param.put("tableName", tableName);
|
|
|
List<String> tableTitle = dataGovernanceMapper.getTableTitleListByTableName(param);
|
|
|
- String fileName = sqlFilePath +"/" + tableName + ".sql";
|
|
|
+ String fileName = sqlFilePath + "/" + tableName + ".sql";
|
|
|
File file = new File(fileName);
|
|
|
if (file.exists()) {
|
|
|
file.delete();
|
|
@@ -368,12 +368,12 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
valueBuilder.append("(");
|
|
|
for (String title : tableTitle) {
|
|
|
Object data = datum.get(title);
|
|
|
- if(data!=null){
|
|
|
+ if (data != null) {
|
|
|
valueBuilder.append("'");
|
|
|
valueBuilder.append(data);
|
|
|
valueBuilder.append("'");
|
|
|
valueBuilder.append(",");
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
valueBuilder.append(data);
|
|
|
valueBuilder.append(",");
|
|
|
}
|
|
@@ -394,13 +394,212 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
|
|
|
return file;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<DatabaseConfig> getDatabaseConfigList() {
|
|
|
+ return dataGovernanceMapper.getDatabaseConfigList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切换数据库连接
|
|
|
+ *
|
|
|
+ * @param id 数据库连接id
|
|
|
+ * @return 切换状态信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String, String> changeConnection(String id) {
|
|
|
+ //初始化结果
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+ //如果id为null或空,则初始化为1
|
|
|
+ if (id == null || id.isEmpty()) {
|
|
|
+ id = "1";
|
|
|
+ }
|
|
|
+ //获得连接id
|
|
|
+ this.connectionId = id;
|
|
|
+ //获取数据库连接配置信息
|
|
|
+ DatabaseConfig databaseConfig = dataGovernanceMapper.getDatabaseConfig(id);
|
|
|
+ try {
|
|
|
+ //加载驱动
|
|
|
+ Class.forName(driver.get(databaseConfig.getDatabaseType()));
|
|
|
+ //获取数据库连接
|
|
|
+ this.connection = DriverManager.getConnection(databaseConfig.getUrl(), databaseConfig.getUsername(), databaseConfig.getPassword());
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("message", "切换失败!");
|
|
|
+ result.put("type", "error");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ result.put("message", "切换成功!");
|
|
|
+ result.put("type", "success");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取当前数据库连接id
|
|
|
+ *
|
|
|
+ * @return 数据库连接id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getConnectionId() {
|
|
|
+ return this.connectionId;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验数据库命名
|
|
|
+ *
|
|
|
+ * @param text 数据库名/表名/字段
|
|
|
+ * @return true 不合规 /false 合规
|
|
|
+ */
|
|
|
private Boolean verifySQLName(String text) {
|
|
|
return text.isEmpty() || !text.matches("^[a-zA-Z_][a-zA-Z0-9_]{0,63}$");
|
|
|
}
|
|
|
|
|
|
- private void driverInit(){
|
|
|
- this.driver=new HashMap<>();
|
|
|
- this.driver.put("mysql","com.mysql.cj.jdbc.Driver");
|
|
|
+ /**
|
|
|
+ * 初始化数据库驱动字符串集
|
|
|
+ */
|
|
|
+ private void driverInit() {
|
|
|
+ this.driver = new HashMap<>();
|
|
|
+ this.driver.put("mysql", "com.mysql.cj.jdbc.Driver");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用于统一执行sql并返回结果
|
|
|
+ *
|
|
|
+ * @param beanClass 返回的列表类型的class,用于实例化
|
|
|
+ * @param sql 执行sql
|
|
|
+ * @param <E> 返回的列表类型
|
|
|
+ * @return 执行sql后返回的数据
|
|
|
+ */
|
|
|
+ private <E> List<E> getDataList(Class<E> beanClass, String sql) {
|
|
|
+ List<E> data = new ArrayList<>();
|
|
|
+ Statement statement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ //获取sql执行器
|
|
|
+ statement = this.connection.createStatement();
|
|
|
+ //获取sql执行结果
|
|
|
+ resultSet = statement.executeQuery(sql);
|
|
|
+ //遍历结果赋值
|
|
|
+ while (resultSet.next()) {
|
|
|
+ //实例化bean
|
|
|
+ E bean = null;
|
|
|
+ if (this.isJavaClass(beanClass) && beanClass!=Map.class) {
|
|
|
+ bean = resultSet.getObject(1, beanClass);
|
|
|
+ data.add(bean);
|
|
|
+ }else if (beanClass==Map.class){
|
|
|
+ Map<String,String> map=new HashMap<>();
|
|
|
+ ResultSetMetaData metaData = resultSet.getMetaData();
|
|
|
+ for (int i = 0; i< metaData.getColumnCount(); i++) {
|
|
|
+ map.put(metaData.getColumnName(i+1), resultSet.getString(i+1));
|
|
|
+ }
|
|
|
+ data.add((E)map);
|
|
|
+ }else {
|
|
|
+ bean = beanClass.newInstance();
|
|
|
+ Field[] fields = bean.getClass().getDeclaredFields();
|
|
|
+ int i = 1;
|
|
|
+ for (Field field : fields) {
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(bean, resultSet.getObject(i, field.getType()));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ data.add(bean);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ resultSet.close();
|
|
|
+ statement.close();
|
|
|
+ } catch (Exception throwable) {
|
|
|
+ throwable.printStackTrace();
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用于统一执行sql并返回结果
|
|
|
+ *
|
|
|
+ * @param beanClass 返回的类型的class,用于实例化
|
|
|
+ * @param sql 执行sql
|
|
|
+ * @param <T> 返回的列表类型
|
|
|
+ * @return 执行sql后返回的数据
|
|
|
+ */
|
|
|
+ private <T> T getData(Class<T> beanClass, String sql) {
|
|
|
+ Statement statement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ T bean = null;
|
|
|
+ try {
|
|
|
+ //获取sql执行器
|
|
|
+ statement = this.connection.createStatement();
|
|
|
+ //获取sql执行结果
|
|
|
+ resultSet = statement.executeQuery(sql);
|
|
|
+ //遍历结果赋值
|
|
|
+ while (resultSet.next()) {
|
|
|
+ //实例化bean
|
|
|
+ if (this.isJavaClass(beanClass) && beanClass!=Map.class) {
|
|
|
+ bean = resultSet.getObject(1, beanClass);
|
|
|
+ }else if (beanClass==Map.class){
|
|
|
+ Map<String,String> map=new HashMap<>();
|
|
|
+ ResultSetMetaData metaData = resultSet.getMetaData();
|
|
|
+ for (int i = 0; i< metaData.getColumnCount(); i++) {
|
|
|
+ map.put(metaData.getColumnName(i+1), resultSet.getString(i+1));
|
|
|
+ }
|
|
|
+ return (T)map;
|
|
|
+ } else {
|
|
|
+ bean = beanClass.newInstance();
|
|
|
+ Field[] fields = bean.getClass().getDeclaredFields();
|
|
|
+ int i = 1;
|
|
|
+ for (Field field : fields) {
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(bean, resultSet.getObject(i, field.getType()));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resultSet.close();
|
|
|
+ statement.close();
|
|
|
+ } catch (Exception throwable) {
|
|
|
+ throwable.printStackTrace();
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用于统一执行sql
|
|
|
+ *
|
|
|
+ * @param sql 执行的sql语句
|
|
|
+ */
|
|
|
+ private void executeSql(String sql) {
|
|
|
+ Statement st = null;
|
|
|
+ try {
|
|
|
+ //获取sql执行器
|
|
|
+ st = this.connection.createStatement();
|
|
|
+ //获取sql执行结果
|
|
|
+ st.execute(sql);
|
|
|
+ st.close();
|
|
|
+ } catch (Exception throwable) {
|
|
|
+ throwable.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取需要执行的sql
|
|
|
+ *
|
|
|
+ * @param statement sql语句对应mapper的id
|
|
|
+ * @param param 参数
|
|
|
+ * @return 需要执行的sql
|
|
|
+ */
|
|
|
+ private String getExecuteSql(String statement, Object param) {
|
|
|
+ return SqlHelper.getNamespaceSql(sqlSessionFactory, NAME_SPACE + "." + statement, param);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断类是否为java自有的类
|
|
|
+ *
|
|
|
+ * @param clz 类
|
|
|
+ * @return 是否为java自有的类
|
|
|
+ */
|
|
|
+ private boolean isJavaClass(Class<?> clz) {
|
|
|
+ return clz != null && clz.getClassLoader() == null;
|
|
|
}
|
|
|
}
|