Commit f3846066 authored by Gwofoo's avatar Gwofoo

数据库删除功能

parent 869aa017
......@@ -22,7 +22,7 @@ import java.util.Map;
@RestController
@Api(tags = "机构管理Api")
@RequestMapping(value = "/agency")
public class RelationResource {
public class AgencyManagementResource {
@Autowired
private RelationService relationService;
@Autowired
......@@ -31,6 +31,7 @@ public class RelationResource {
private StudioResourceService studioResourceService;
/**
* (原)
* 根据机构ID和数据库sequenceNbr批量删除数据库
*
* @param agencyCode 机构ID
......@@ -44,7 +45,7 @@ public class RelationResource {
@PathVariable String agencyCode,
@PathVariable String sequenceNbrs) throws Exception {
relationService.deleteAgency(agencyCode, sequenceNbrs);
return ResponseHelper.buildResponse("The data has been deleted !");
return ResponseHelper.buildResponse("删除操作已触发!");
}
/**
......@@ -63,7 +64,7 @@ public class RelationResource {
HttpServletRequest request) throws Exception {
Map<String, Object> variables = new HashMap<>();
variables.put("agencyCode", agencyCode);
studioResourceService.generateSQLByDatabaseName("Agency","agencyCode",dbSeqs, ipSeq, variables, response);
studioResourceService.generateSQLByDatabaseName("Agency", "agencyCode", dbSeqs, ipSeq, variables, response);
}
......
......@@ -53,7 +53,7 @@ public class DatabaseConnectionService extends BaseService<DatabaseConnectionMod
*/
public DatabaseConnectionModel update(DatabaseConnectionModel model) throws Exception {
if (DatabaseUtils.isConnectionValid(model)) {
return this.updateWithModel(AESUtils.encryptPwd(model));
return this.updateWithModel(model);
} else {
return null;
}
......
......@@ -91,7 +91,7 @@ public class DatabaseNameListService extends BaseService<DataBaseNameListModel,
return null;
List<String> strings = new ArrayList<>();
Collection<DataBaseNameList> dataBaseNameLists = new ArrayList<>();
for (String dbName : DatabaseUtils.getDatabaseNames(connection)) {
for (String dbName : DatabaseUtils.getAllDatabaseNames(connection)) {
DataBaseNameListModel model = new DataBaseNameListModel(dbName, ipSeq);
strings.add(dbName);
dataBaseNameLists.add(this.prepareEntity(model));
......
package com.yeejoin.amos.api.tool.face.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yeejoin.amos.api.tool.face.model.DatabaseConnectionModel;
import com.yeejoin.amos.api.tool.face.model.DataBaseNameListModel;
import com.yeejoin.amos.api.tool.face.model.RelationModel;
import com.yeejoin.amos.api.tool.face.model.RelationTreeModel;
import com.yeejoin.amos.api.tool.face.orm.dao.RelationMapper;
......@@ -10,7 +10,6 @@ import com.yeejoin.amos.api.tool.face.orm.entity.Relation;
import com.yeejoin.amos.api.tool.face.orm.entity.TableColumn;
import com.yeejoin.amos.api.tool.utils.DatabaseUtils;
import com.yeejoin.amos.api.tool.utils.DateUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.BeanUtils;
......@@ -24,6 +23,7 @@ import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
......@@ -48,18 +48,81 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
private DatabaseNameListService dataBaseNameListService;
@Autowired
public EmqKeeper emqKeeper;
@Autowired
private DataSource dataSource; // amos_tool_library
/**
* 根据机构删除
*
*/
public void deleteAgency(String agencyCode, String sequenceNbrs) throws Exception {
for (Long seq : (Long[]) ConvertUtils.convert(StringUtil.string2Array(sequenceNbrs), Long.class)) {
DataBaseNameListModel nameListModel = dataBaseNameListService.queryBySeq(seq);
DatabaseConnectionModel model = databaseConnectionService.queryBySeq(nameListModel.getParentId());
model.setDbName(nameListModel.getDbName());
Connection connection = DatabaseUtils.getConnection(model);
this.deleteDataBase(
connection, agencyCode, model.getDbName());
public void deleteAgency(String agencyCode, String sequenceNbrsString) throws Exception {
String[] sequenceNbrs = StringUtil.string2Array(sequenceNbrsString);
Connection connection = dataSource.getConnection();
// 查询所有数据库,将sequenceNbrs转为具体的数据库名称databaseNames
List<String> allDbs = DatabaseUtils.getAllDatabaseNames(connection);
List<String> databaseNames = new ArrayList<>();
for (String sequenceNbr : sequenceNbrs) {
int idx = Integer.parseInt(sequenceNbr);
databaseNames.add(allDbs.get(idx - 1)); // sequenceNbrs序号从1开始
}
// 查询studio_resource表中的RESOURCE_CODE
String sql = "SELECT RESOURCE_CODE FROM studio_resource;";
List<String> fastDeleteDbs = DatabaseUtils.getList(sql, connection);
// 对于每一个待删除数据库,如果出现在resourceCodes中,则采用新的快速删除方式;否则采用旧的全表查找删除方式
for (String dbName : databaseNames) {
String fastDeleteDbResourceCode = "no";
for (String fastDeleteDb : fastDeleteDbs) {
if (dbName.contains(fastDeleteDb)) {
fastDeleteDbResourceCode = fastDeleteDb;
break;
}
}
// 连接到dbName
Connection conn = DatabaseUtils.getConnection(dbName);
if (!fastDeleteDbResourceCode.equals("no")) {
// 快速删除的逻辑
// 拿到快速删除对应的DELETE语句集
sql = "SELECT RESOURCE_DELETE_SQL FROM studio_resource WHERE RESOURCE_CODE = '" + fastDeleteDbResourceCode + "';";
String deleteSqlsStr = DatabaseUtils.getList(sql, connection).get(0);
JSONObject deleteSqlsJson = JSON.parseObject(deleteSqlsStr);
String deleteSqls = deleteSqlsJson.getString("agencyCode");
deleteSqls = deleteSqls.replace("#{agencyCode}", agencyCode);
System.out.println(deleteSqls);
// 检查删除是否成功?
List<Map<String, Object>> deleteRes = DatabaseUtils.getMaps(deleteSqls, conn);
System.out.println(deleteRes);
}
else {
// 全表遍历后删除的逻辑
// 获取dbName数据库下所有表名
List<String> tableNames = DatabaseUtils.getTableNames(dbName, conn);
for (String tableName : tableNames) {
sql = "SELECT 1 " +
"FROM information_schema.columns " +
"WHERE table_name = '" + tableName + "' AND column_name = 'AGENCY_CODE'";
List<String> list = getList(sql, conn);
// 仅在tableName有AGENCY_CODE字段时,删除tableName中的对应行数据
if (list.size() > 0) {
sql = "DELETE " + // TODO 改成DELETE即可
" FROM " + tableName +
" WHERE AGENCY_CODE = '" + agencyCode + "';";
List<Map<String, Object>> deleteRes = getMaps(sql, conn);
System.out.println(deleteRes);
}
}
}
conn.close();
}
connection.close(); // atl
}
/**
......
......@@ -54,22 +54,22 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
* 数据库备份文本前缀
*/
public static String ONESQL_PREFIX = "";
private static String SUFFIX = "sql";
private static String BR = "\r\n";
private static String SLASH = "/";
private static String BRANCH = ";";
private static String SPLIT = "`";
private static String SPACE = " ";
private static String INSERT_INTO = " INSERT INTO ";
private static String CREATE_INTO = " CREATE TABLE ";
private static String VALUES = "VALUES";
private static String LEFTBRACE = "(";
private static String RIGHTBRACE = ")";
private static String QUOTES = "'";
private static String COMMA = ",";
private static String DISABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 0;\r\n";
private static String ABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 1;\r\n";
private static String DELIMITER = "###################################";
private static final String SUFFIX = "sql";
private static final String BR = "\r\n";
private static final String SLASH = "/";
private static final String BRANCH = ";";
private static final String SPLIT = "`";
private static final String SPACE = " ";
private static final String INSERT_INTO = " INSERT INTO ";
private static final String CREATE_INTO = " CREATE TABLE ";
private static final String VALUES = "VALUES";
private static final String LEFTBRACE = "(";
private static final String RIGHTBRACE = ")";
private static final String QUOTES = "'";
private static final String COMMA = ",";
private static final String DISABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 0;\r\n";
private static final String ABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 1;\r\n";
private static final String DELIMITER = "###################################";
@Autowired
JdbcTemplate jdbcTemplate;
......@@ -177,14 +177,17 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
}
}
/**
* 导出机构
*/
public void generateSQLByDatabaseName(String resourceCode, String dimension, String dbSeqs, Long ipSeq, Map<String, Object> variables, HttpServletResponse response) {
try {
DatabaseConnectionModel dataBaseConnectionModel = databaseConnectionService.queryBySeq(ipSeq);
//根据连接查询到数据库表名
// 根据连接查询到数据库表名
Connection connection = DatabaseUtils.getConnection(dataBaseConnectionModel);
// 查询所有数据库
List<String> allDbs = DatabaseUtils.getList("show databases;", connection);
List<String> allDbs = DatabaseUtils.getAllDatabaseNames(connection);
List<String> databaseNames = new ArrayList<>();
String[] queryDbSeqs = dbSeqs.split(","); // 序号从1开始
......@@ -193,8 +196,9 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
databaseNames.add(allDbs.get(idx - 1));
}
// 查询到需要导出多个,在databaseNames中,循环,多次下载
// TODO 有bug,connection是同一个。。。
for (String databaseName : databaseNames) {
List<String> list = DatabaseUtils.getList("select table_name from information_schema.tables where table_schema='" + "" + databaseName + "';", connection);
List<String> tableNames = DatabaseUtils.getList("select table_name from information_schema.tables where table_schema='" + databaseName + "';", connection);
List<StudioResourceModel> resourceList = queryForStudioResourceList(resourceCode);
File directory = new File("");// 参数为空
String coursePath = directory.getCanonicalPath();
......@@ -214,7 +218,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
out = new FileOutputStream(file);
writer = new OutputStreamWriter(out, "utf8");
StringBuffer deleteSql = new StringBuffer();
for (String tableName : list) {
for (String tableName : tableNames) {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = '" + tableName + "' AND COLUMN_NAME = '" + "AGENCY_CODE" + "';");
......@@ -227,7 +231,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
}
StringBuffer insertSql = new StringBuffer();
for (StudioResourceModel model : resourceList) {
String sql = buildInsertSqlByDatabaseName(model, dimension, databaseName, list, ipSeq, variables);
String sql = buildInsertSqlByDatabaseName(model, dimension, databaseName, tableNames, ipSeq, variables);
if (!ObjectUtils.isEmpty(sql))
insertSql.append(sql);
}
......@@ -381,12 +385,12 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
"where AGENCY_CODE = " + "'" + agencyCode + "'";
// System.out.println(sqlStr);
ResultSet resultSet = sql.executeQuery(sqlStr);
ResultSetMetaData md = resultSet.getMetaData(); //获得结果集结构信息,元数据
List<ProjectModel> listPm = new ArrayList<>(); //实例化一个list作为容器
ResultSetMetaData md = resultSet.getMetaData(); // 获得结果集结构信息,元数据
List<ProjectModel> listPm = new ArrayList<>(); // 实例化一个list作为容器
try {
//循环赋值 添加ProjectMode到list
// 循环赋值 添加ProjectMode到list
while (resultSet.next()) {
//每次循环都实例化一个user 用来储存属性
// 每次循环都实例化一个user 用来储存属性
ProjectModel pm = new ProjectModel();
pm.setName(resultSet.getString("PROJECT_NAME"));
pm.setSequenceNbr(resultSet.getString("SEQUENCE_NBR"));
......@@ -396,7 +400,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//关闭资源
// 关闭资源
connection.close();
}
......@@ -413,12 +417,12 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
"where PROJECT_SEQ = " + "'" + projectNbr + "'";
// System.out.println(sqlStr);
ResultSet resultSet = sql.executeQuery(sqlStr);
ResultSetMetaData md = resultSet.getMetaData(); //获得结果集结构信息,元数据
List<ProjectModel> listPm = new ArrayList<>(); //实例化一个list作为容器
ResultSetMetaData md = resultSet.getMetaData(); // 获得结果集结构信息,元数据
List<ProjectModel> listPm = new ArrayList<>(); // 实例化一个list作为容器
try {
//循环赋值 添加ProjectMode到list
// 循环赋值 添加ProjectMode到list
while (resultSet.next()) {
//每次循环都实例化一个user 用来储存属性
// 每次循环都实例化一个user 用来储存属性
ProjectModel pm = new ProjectModel();
pm.setName(resultSet.getString("APP_NAME"));
pm.setSequenceNbr(resultSet.getString("SEQUENCE_NBR"));
......@@ -428,7 +432,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//关闭资源
// 关闭资源
connection.close();
}
......@@ -444,12 +448,12 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
"where agencyCode = '" + agencyCode + "' and type = 'project'";
// System.out.println(sqlStr);
ResultSet resultSet = sql.executeQuery(sqlStr);
ResultSetMetaData md = resultSet.getMetaData(); //获得结果集结构信息,元数据
List<ProjectModel> listPm = new ArrayList<>(); //实例化一个list作为容器
ResultSetMetaData md = resultSet.getMetaData(); // 获得结果集结构信息,元数据
List<ProjectModel> listPm = new ArrayList<>(); // 实例化一个list作为容器
try {
//循环赋值 添加ProjectMode到list
// 循环赋值 添加ProjectMode到list
while (resultSet.next()) {
//每次循环都实例化一个user 用来储存属性
// 每次循环都实例化一个user 用来储存属性
ProjectModel pm = new ProjectModel();
pm.setName(resultSet.getString("name"));
pm.setSequenceNbr(resultSet.getString("id"));
......@@ -460,7 +464,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//关闭资源
// 关闭资源
connection.close();
}
return listPm;
......
......@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.api.tool.face.model.DatabaseConnectionModel;
import com.yeejoin.amos.api.tool.face.orm.entity.TableColumn;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import java.sql.*;
......@@ -12,7 +13,6 @@ import java.util.*;
@Component
public class DatabaseUtils {
/**
* 数据库备份路径
*/
......@@ -40,6 +40,35 @@ public class DatabaseUtils {
public static String DELIMITER = "###################################";
private static String driverClassName;
private static String jdbcDefaultUsername;
private static String jdbcDefaultPassword;
private static String jdbcDefaultIp;
private static String jdbcDefaultPort;
@Value("${datasource.driver-class-name}")
public void setDriverClassName(String param) {
driverClassName = param;
}
@Value("${datasource.username}")
public void setJdbcDefaultUsername(String param) {
jdbcDefaultUsername = param;
}
@Value("${datasource.password}")
public void setJdbcDefaultPassword(String param) {
jdbcDefaultPassword = param;
}
@Value("${datasource.ip}")
public void setJdbcDefaultIp(String param) {
jdbcDefaultIp = param;
}
@Value("${datasource.port}")
public void setJdbcDefaultPort(String param) {
jdbcDefaultPort = param;
}
/**
* 连接数据库
* @param model DataBaseLinkModel
......@@ -48,7 +77,7 @@ public class DatabaseUtils {
public static Connection getConnection(DatabaseConnectionModel model) {
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName(driverClassName);
if (model.getPrefix() == null || model.getPrefix().isEmpty() || model.getPrefix().equals("null")) {
model.setPrefix("");
}
......@@ -76,6 +105,30 @@ public class DatabaseUtils {
}
/**
* 以默认方式 获取数据源
* @param dbName 数据库名称
* @return 数据库连接
*/
public static Connection getConnection(String dbName) throws Exception {
DatabaseConnectionModel model = new DatabaseConnectionModel();
model.setIp(jdbcDefaultIp);
model.setPort(jdbcDefaultPort);
model.setDbName(dbName);
model.setUserName(jdbcDefaultUsername);
model.setPassWord(AESUtils.encrypt(jdbcDefaultPassword));
return getConnection(model);
}
/**
* 获取数据库下的所有表名
*/
public static List<String> getTableNames(String dbName, Connection connection) throws SQLException {
String sql = "select table_name from information_schema.tables where table_schema='" + dbName + "';";
return getList(sql, connection);
}
/**
* 测试数据库连接是否成功
*/
public static boolean isConnectionValid(DatabaseConnectionModel model) throws Exception {
......@@ -90,7 +143,7 @@ public class DatabaseUtils {
/**
* 获取所有数据库名称,如果为空,则返回空的List<String>
*/
public static List<String> getDatabaseNames(Connection connection) throws SQLException {
public static List<String> getAllDatabaseNames(Connection connection) throws SQLException {
return new ArrayList<>(DatabaseUtils.getList("SHOW DATABASES;", connection));
}
......@@ -131,7 +184,7 @@ public class DatabaseUtils {
public static List<String> getList(String sql, Connection connection) throws SQLException {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();
try {
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
......
......@@ -71,7 +71,14 @@ management.security.enabled=false
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
# ??175?????
# login to 36.40.66.175
scene.loginUrl=http://36.40.66.175:8089/privilege/v1/auth/dual/idpassword
scene.loginId=yeejoin_xl
scene.plainPwd=a1234560
# default datasource info
datasource.ip=172.16.3.18
datasource.port=3306
datasource.username=root
datasource.password=Yeejoin@2020
datasource.driver-class-name=com.mysql.cj.jdbc.Driver
......@@ -70,7 +70,14 @@ management.security.enabled=false
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
# ??175?????
# login to 36.40.66.175
scene.loginUrl=http://36.40.66.175:8089/privilege/v1/auth/dual/idpassword
scene.loginId=yeejoin_xl
scene.plainPwd=a1234560
\ No newline at end of file
scene.plainPwd=a1234560
# default datasource info
datasource.ip=172.16.3.18
datasource.port=3306
datasource.username=root
datasource.password=Yeejoin@2020
datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment