Commit 7366a689 authored by Spencer Chang's avatar Spencer Chang

[fix] 优化返回信息

parent 4a8f7734
package com.hand.hls.dp.core;
import com.hand.hap.core.IRequest;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/8/13 09:50
* @since 1.0
*/
public class SubExecSqlContext {
private SysDpExecuteHistoryService service;
private IRequest requestContext;
private String sql;
private String ip;
private String execType;
private Integer seq;
public SysDpExecuteHistoryService getService() {
return service;
}
public void setService(SysDpExecuteHistoryService service) {
this.service = service;
}
public IRequest getRequestContext() {
return requestContext;
}
public void setRequestContext(IRequest requestContext) {
this.requestContext = requestContext;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getExecType() {
return execType;
}
public void setExecType(String execType) {
this.execType = execType;
}
public Integer getSeq() {
return seq;
}
public void setSeq(Integer seq) {
this.seq = seq;
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ import com.hand.hap.core.IRequest;
import com.hand.hap.generator.service.impl.DBUtil;
import com.hand.hap.system.dto.ResponseData;
import com.hand.hap.system.service.impl.BaseServiceImpl;
import com.hand.hls.dp.core.SubExecSqlContext;
import com.hand.hls.dp.dto.SysDpExecuteHistory;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
import com.hand.hls.dp.util.SqlConstantUtils;
......@@ -46,14 +47,13 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:07
* @since
* @since 1.0
*/
@Service
@Transactional(rollbackFor = Exception.class)
......@@ -183,7 +183,7 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
JSONArray coumnConfig = new JSONArray(DEFAULT_INITIAL_CAPACITY);
JSONArray columnConfig = new JSONArray(DEFAULT_INITIAL_CAPACITY);
List<Map<String, Object>> retList = new ArrayList<>(DEFAULT_INITIAL_CAPACITY);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
......@@ -228,9 +228,9 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
sysDpExecuteHistory.setDehExecuteTime(new Date());
ResultSetMetaData md = rs.getMetaData();
int columCount = md.getColumnCount();
int columnCount = md.getColumnCount();
// 组装excel列信息
for (int i = 1; i <= columCount; i++) {
for (int i = 1; i <= columnCount; i++) {
String columnName = md.getColumnName(i);
Map<String, Object> columnInfo = new HashMap<>(4);
columnInfo.put("width", 150);
......@@ -238,7 +238,7 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
columnInfo.put("align", "left");
columnInfo.put("prompt", columnName);
JSONObject columnJsonObject = new JSONObject(columnInfo);
coumnConfig.add(columnJsonObject);
columnConfig.add(columnJsonObject);
}
while (rs.next()) {
Map<String, Object> rowData = new HashMap<>(1);
......@@ -265,7 +265,7 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
self().insertSelective(requestContext, sysDpExecuteHistory);
// 查询结果导出excel
ExcelExportUtil excelExportUtil = new ExcelExportUtil();
excelExportUtil.createExcel(response, coumnConfig, retList);
excelExportUtil.createExcel(response, columnConfig, retList);
if (logger.isInfoEnabled()) {
logger.info("<== end execute query");
}
......@@ -495,7 +495,7 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:更新条目与查询条目不一致!本次更新前条件查询" + rowCount + "条数据。本次更新" + updateRowCount + "条数据。");
responseData.setMessage("错误消息:更新条目与查询条目不一致!本次更新前条件查询" + rowCount + "条数据。本次更新" + updateRowCount + "条数据。");
return responseData;
}
} catch (SQLException e) {
......@@ -583,11 +583,11 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
}
String sql = SqlConstantUtils.SELECT_COUNT;
// 默认截取from之后语句
// 默认截取from之后语句, 4 from的长度
if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_FROM)) {
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM));
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM) + 4);
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_FROM_UPPER)) {
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM_UPPER));
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM_UPPER) + 4);
}
// 先查询受where条件影响数据
......@@ -803,7 +803,7 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
}
/**
* rs结果转换jsoc
* rs结果转换json
*
* @param rs JDBC返回结果集
* @return 返回消息
......@@ -918,14 +918,18 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
// 支持多条语句执行,以“;“隔开
String[] undos = StringUtils.trimToEmpty(dpContext).split(";");
int length = undos.length;
ConcurrentHashMap<Integer, String> retMsg = null;
if (length >= 1) {
retMsg = new ConcurrentHashMap<>(1);
if (length <= 0) {
throw new HlsCusException("没有可执行语句!");
}
ConcurrentHashMap<Integer, String> retMsg = new ConcurrentHashMap<>(length);
List<String> result = new ArrayList<>(retMsg.size());
AtomicInteger atomicInteger = new AtomicInteger();
CountDownLatch downLatch = new CountDownLatch(length);
SubExecSqlContext subExecContext = new SubExecSqlContext();
subExecContext.setService(self());
subExecContext.setRequestContext(requestContext);
subExecContext.setIp(ip);
for (int i = 1; i <= length; i++) {
String undo = undos[i-1];
String undo = undos[i - 1];
if (StringUtils.isBlank(undo)) {
continue;
}
......@@ -946,6 +950,8 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
if (StringUtils.isNotEmpty(msg.toString())) {
throw new HlsCusException(msg.toString());
}
subExecContext.setSeq(i);
subExecContext.setSql(formatSql);
// 判断语句开始关键字
if (StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.SQL_EXEC_SELECT)) {
throw new HlsCusException("执行按钮不支持SELECT查询语句,请使用查询按钮");
......@@ -955,42 +961,24 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
if (StringUtils.isNotEmpty(msg.toString())) {
throw new HlsCusException(msg.toString());
}
Future<?> future = taskExecutor.submit(new SubThreadTask(self(), requestContext, ip, formatSql, SqlConstantUtils.SQL_EXEC_INSERT, length, atomicInteger, i, retMsg));
while (true) {
if (future.isDone()) {
retMsg.forEach((k, v) -> result.add("第" + k + "行语句," + v));
retMsg.clear();
break;
}
}
subExecContext.setExecType(SqlConstantUtils.SQL_EXEC_INSERT);
taskExecutor.execute(new SubThreadTask(subExecContext, retMsg, downLatch));
} else if (StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.SQL_EXEC_UPDATE)) {
String checkMsg = SqlParserCheckUtils.parserCheckUpdate(formatSql);
msg.append(checkMsg);
if (StringUtils.isNotEmpty(msg.toString())) {
throw new HlsCusException(msg.toString());
}
Future<?> future = taskExecutor.submit(new SubThreadTask(self(), requestContext, ip, formatSql, SqlConstantUtils.SQL_EXEC_UPDATE, length, atomicInteger, i, retMsg));
while (true) {
if (future.isDone()) {
retMsg.forEach((k, v) -> result.add("第" + k + "行语句," + v));
retMsg.clear();
break;
}
}
subExecContext.setExecType(SqlConstantUtils.SQL_EXEC_UPDATE);
taskExecutor.execute(new SubThreadTask(subExecContext, retMsg, downLatch));
} else if (StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.SQL_EXEC_DELETE)) {
String checkMsg = SqlParserCheckUtils.parserCheckDelete(formatSql);
msg.append(checkMsg);
if (StringUtils.isNotEmpty(msg.toString())) {
throw new HlsCusException(msg.toString());
}
Future<?> future = taskExecutor.submit(new SubThreadTask(self(), requestContext, ip, formatSql, SqlConstantUtils.SQL_EXEC_DELETE, length, atomicInteger, i, retMsg));
while (true) {
if (future.isDone()) {
retMsg.forEach((k, v) -> result.add("第" + k + "行语句," + v));
retMsg.clear();
break;
}
}
subExecContext.setExecType(SqlConstantUtils.SQL_EXEC_DELETE);
taskExecutor.execute(new SubThreadTask(subExecContext, retMsg, downLatch));
} else if (StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_CREATE_TABLE) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_ALTER_TABLE) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_COMMENT_TABLE) ||
......@@ -1004,75 +992,62 @@ public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecute
if (StringUtils.isNotEmpty(msg.toString())) {
throw new HlsCusException(msg.toString());
}
Future<?> future = taskExecutor.submit(new SubThreadTask(self(), requestContext, ip, formatSql, SqlConstantUtils.SQL_EXEC_DDL, length, atomicInteger, i, retMsg));
while (true) {
if (future.isDone()) {
retMsg.forEach((k, v) -> result.add("第" + k + "行语句," + v));
retMsg.clear();
break;
}
subExecContext.setExecType(SqlConstantUtils.SQL_EXEC_DDL);
taskExecutor.execute(new SubThreadTask(subExecContext, retMsg, downLatch));
}
}
try {
downLatch.await();
} catch (InterruptedException ie) {
logger.info("==> 错误信息:[{}]", ie.getMessage());
}
retMsg.forEach((k, v) -> result.add("第" + k + "行语句," + v));
if (!result.isEmpty()) {
responseData.setRows(result);
}
}
return responseData;
}
private static class SubThreadTask implements Runnable {
private final ReentrantLock mainLock = new ReentrantLock();
private final SysDpExecuteHistoryService service;
private final IRequest requestContext;
private final String sql;
private final String ip;
private final String execType;
private final Integer count;
private final AtomicInteger atomicInteger;
private final Integer seq;
private final SubExecSqlContext subExecContext;
private final ConcurrentHashMap<Integer, String> retMsg;
private final CountDownLatch downLatch;
private SubThreadTask(SysDpExecuteHistoryService service, IRequest requestContext, String ip, String sql,
String execType, Integer count, AtomicInteger atomicInteger, Integer seq, ConcurrentHashMap<Integer, String> retMsg) {
this.service = service;
this.requestContext = requestContext;
this.ip = ip;
this.sql = sql;
this.execType = execType;
this.count = count;
this.atomicInteger = atomicInteger;
this.seq = seq;
private SubThreadTask(SubExecSqlContext subExecContext, ConcurrentHashMap<Integer, String> retMsg, CountDownLatch downLatch) {
this.subExecContext = subExecContext;
this.retMsg = retMsg;
this.downLatch = downLatch;
}
@Override
public void run() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
final SysDpExecuteHistoryService service = subExecContext.getService();
final IRequest requestContext = subExecContext.getRequestContext();
final String sql = subExecContext.getSql();
final String ip = subExecContext.getIp();
final String execType = subExecContext.getExecType();
final Integer seq = subExecContext.getSeq();
try {
String threadName = Thread.currentThread().getName();
ResponseData responseData = new ResponseData();
int index = atomicInteger.getAndIncrement();
ResponseData subResponseData = new ResponseData();
if (StringUtils.equals(execType, SqlConstantUtils.SQL_EXEC_INSERT)) {
responseData = service.insert(requestContext, ip, sql);
subResponseData = service.insert(requestContext, ip, sql);
} else if (StringUtils.equals(execType, SqlConstantUtils.SQL_EXEC_UPDATE)) {
responseData = service.update(requestContext, ip, sql);
subResponseData = service.update(requestContext, ip, sql);
} else if (StringUtils.equals(execType, SqlConstantUtils.SQL_EXEC_DELETE)) {
responseData = service.delete(requestContext, ip, sql);
subResponseData = service.delete(requestContext, ip, sql);
} else if (StringUtils.equals(execType, SqlConstantUtils.SQL_EXEC_DDL)) {
responseData = service.ddl(requestContext, ip, sql);
}
logger.info("==> [{}]-->第[{}]次执行:[{}]", threadName, index, sql);
retMsg.put(seq, responseData.getMessage());
logger.info("==> 执行语句[{}],详细信息:[{}]", sql, responseData.getMessage());
if (index == count - 1 || count == 1) {
logger.info("<== ALL DONE");
subResponseData = service.ddl(requestContext, ip, sql);
}
retMsg.put(seq, subResponseData.getMessage());
logger.info("==> 执行语句[{}],详细信息:[{}]", sql, subResponseData.getMessage());
} catch (Exception e) {
retMsg.put(seq, e.getMessage());
logger.info("==> 执行语句[{}]出错,详细信息:[{}]", sql, e.getMessage());
} finally {
downLatch.countDown();
mainLock.unlock();
}
}
......
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