Commit 45111d09 authored by Spencer Chang's avatar Spencer Chang

init

parent 94f03e01
# dp
# dp 数据处理中间件
## 实现DML DDL
## 数据修改
* 备份WHERE条件筛选的数据
* 检验WHERE条件筛选的数据条目与更新条目是否一致
* 禁止修改系统级别或业务系统基础数据
* DDL操作限制在内网环境使用
## v1-基础版本
### 直接判断sql语句关键字,校验sql有效性。
### 只支持单条语句执行并不需要结束符号。
## v2-多线程版本
### 使用Druid解析sql,校验sql有效性。
### 执行按钮支持以英文 **;** 分隔多条语句,使用多线程处理。
## 公共功能
### 查询只支持单条语句执行并不需要结束符号。
### 查询结果直接导出Excel。
### update & delete 语句默认会根据where条件先备份源数据。
### 每次执行的语句都会存储到数据库中(不管是否执行成功),并记录当前执行人ip、用户等信息。
## 支持版本
### Leaf4.1 & Leaf4.2
/* 数据处理(DP001) 注册脚本 */
resource_init(modules/DP/DP001/sys_dp.lview, 数据处理, HTML, Y, Y);
resource_init(sys/dp/query, 数据处理-查询);
resource_init(sys/dp/insert, 数据处理-查询);
resource_init(sys/dp/update, 数据处理-查询);
resource_init(sys/dp/delete, 数据处理-查询);
resource_init(sys/dp/ddl, 数据处理-ddl);
resource_init(sys/dp/download/log, 数据处理-日志下载);
/* 功能定义 */
function_init(DP001, 数据处理, Y, SYS, modules/DP/DP001/sys_dp.lview);
/* 功能资源分配 */
fun_resource(DP001, modules/DP/DP001/sys_dp.lview);
fun_resource(DP001, sys/dp/query);
fun_resource(DP001, sys/dp/insert);
fun_resource(DP001, sys/dp/update);
fun_resource(DP001, sys/dp/delete);
fun_resource(DP001, sys/dp/ddl);
fun_resource(DP001, sys/dp/download/log);
/* 角色功能分配 */
role_function(ADMIN, DP001);
\ No newline at end of file
create sequence sys_dp_execute_history_s;
\ No newline at end of file
sys_code_init(IP_ALLOW_LIST, IP允许访问列表, Y, USER, IP, "127.0.", Y);
\ No newline at end of file
create table sys_dp_execute_history
(
deh_id number not null,
deh_user_id number,
deh_ip varchar2(200),
deh_execute_time date,
deh_execute_type varchar2(10),
deh_row_count number,
deh_context clob,
deh_status varchar2(10),
deh_error varchar2(2000),
deh_bak_data clob,
OBJECT_VERSION_NUMBER number DEFAULT 1,
REQUEST_ID number DEFAULT -1,
PROGRAM_ID number DEFAULT -1,
CREATED_BY number DEFAULT -1,
CREATION_DATE date DEFAULT sysdate,
LAST_UPDATED_BY number DEFAULT -1,
LAST_UPDATE_DATE date DEFAULT sysdate,
LAST_UPDATE_LOGIN number DEFAULT -1
);
comment on table SYS_DP_EXECUTE_HISTORY
is '数据处理执行记录';
comment on column sys_dp_execute_history.deh_id
is 'PK';
comment on column sys_dp_execute_history.deh_user_id
is '执行人用户ID';
comment on column sys_dp_execute_history.deh_ip
is '执行IP';
comment on column sys_dp_execute_history.deh_execute_time
is '执行时间';
comment on column sys_dp_execute_history.deh_execute_type
is '执行类型 insert update delete query';
comment on column sys_dp_execute_history.deh_row_count
is '执行结果条数';
comment on column sys_dp_execute_history.deh_context
is '执行内容';
comment on column sys_dp_execute_history.deh_status
is '执行状态';
comment on column sys_dp_execute_history.deh_error
is '错误信息';
comment on column sys_dp_execute_history.deh_bak_data
is '备份源数据';
create index sys_dp_execute_history_n1 on sys_dp_execute_history(deh_user_id);
create index sys_dp_execute_history_n2 on sys_dp_execute_history(deh_ip);
alter table sys_dp_execute_history add constraint sys_dp_execute_history_pk primary key (deh_id);
\ No newline at end of file
package com.hand.hls.dp.controllers;
import com.hand.hap.core.IRequest;
import com.hand.hap.core.impl.RequestHelper;
import com.hand.hap.system.controllers.BaseController;
import com.hand.hap.system.dto.ResponseData;
import com.hand.hls.dp.service.SysDpDownloadLogService;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
import com.hand.hls.dp.util.IPUtils;
import leaf.bean.LeafRequestData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 15:31
* @since
*/
@RestController
public class SysDpExecuteHistoryController extends BaseController {
@Autowired
private SysDpExecuteHistoryService service;
@Autowired
private SysDpDownloadLogService downloadLogService;
@PostMapping(value = "/sys/dp/query")
public ResponseData query(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, @RequestParam("ck") int ck,
HttpServletRequest request, HttpServletResponse response) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
if (ck == 0){
service.query4ck(requestContext, ip, dpContext);
}else if(ck == 1){
request.setAttribute("_NO_PAGE_","Y");
final String context = new String(dpContext.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
service.query(requestContext, response, ip, context);
}
return new ResponseData();
}
@PostMapping(value = "/sys/dp/insert")
public ResponseData insert(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
return service.insert(requestContext, ip, dpContext);
}
@PostMapping(value = "/sys/dp/update")
public ResponseData update(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
return service.update(requestContext, ip, dpContext);
}
@PostMapping(value = "/sys/dp/delete")
public ResponseData delete(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
return service.delete(requestContext, ip, dpContext);
}
@PostMapping(value = "/sys/dp/download/log")
public void downloadLog(HttpServletRequest request, HttpServletResponse response) throws Exception{
IRequest requestContext = createRequestContext(request);
downloadLogService.download(requestContext, request, response);
}
@PostMapping(value = "/sys/dp/ddl")
public void ddl(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
service.ddl(requestContext, ip, dpContext);
}
}
package com.hand.hls.dp.dto;
import com.hand.hap.mybatis.annotation.ExtensionAttribute;
import com.hand.hap.system.dto.BaseDTO;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:01
* @since
*/
@ExtensionAttribute(disable = true)
@Table(name = "sys_dp_execute_history")
public class SysDpExecuteHistory extends BaseDTO {
@Id
@GeneratedValue
private Long dehId;
private Long dehUserId;
private String dehIp;
private Date dehExecuteTime;
private String dehExecuteType;
private Integer dehRowCount;
private String dehContext;
private String dehStatus;
private String dehError;
private String dehBakData;
public Long getDehId() {
return dehId;
}
public void setDehId(Long dehId) {
this.dehId = dehId;
}
public Long getDehUserId() {
return dehUserId;
}
public void setDehUserId(Long dehUserId) {
this.dehUserId = dehUserId;
}
public String getDehIp() {
return dehIp;
}
public void setDehIp(String dehIp) {
this.dehIp = dehIp;
}
public Date getDehExecuteTime() {
return dehExecuteTime;
}
public void setDehExecuteTime(Date dehExecuteTime) {
this.dehExecuteTime = dehExecuteTime;
}
public String getDehExecuteType() {
return dehExecuteType;
}
public void setDehExecuteType(String dehExecuteType) {
this.dehExecuteType = dehExecuteType;
}
public Integer getDehRowCount() {
return dehRowCount;
}
public void setDehRowCount(Integer dehRowCount) {
this.dehRowCount = dehRowCount;
}
public String getDehContext() {
return dehContext;
}
public void setDehContext(String dehContext) {
this.dehContext = dehContext;
}
public String getDehStatus() {
return dehStatus;
}
public void setDehStatus(String dehStatus) {
this.dehStatus = dehStatus;
}
public String getDehError() {
return dehError;
}
public void setDehError(String dehError) {
this.dehError = dehError;
}
public String getDehBakData() {
return dehBakData;
}
public void setDehBakData(String dehBakData) {
this.dehBakData = dehBakData;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SysDpExecuteHistory that = (SysDpExecuteHistory) o;
return Objects.equals(dehId, that.dehId) &&
Objects.equals(dehUserId, that.dehUserId) &&
Objects.equals(dehIp, that.dehIp) &&
Objects.equals(dehExecuteTime, that.dehExecuteTime) &&
Objects.equals(dehExecuteType, that.dehExecuteType) &&
Objects.equals(dehRowCount, that.dehRowCount) &&
Objects.equals(dehContext, that.dehContext) &&
Objects.equals(dehStatus, that.dehStatus) &&
Objects.equals(dehError, that.dehError) &&
Objects.equals(dehBakData, that.dehBakData);
}
@Override
public int hashCode() {
return Objects.hash(dehId, dehUserId, dehIp, dehExecuteTime, dehExecuteType, dehRowCount, dehContext, dehStatus, dehError, dehBakData);
}
@Override
public String toString() {
return "SysDpExecuteHistory{" +
"dehId=" + dehId +
", dehUserId=" + dehUserId +
", dehIp='" + dehIp + '\'' +
", dehExecuteTime=" + dehExecuteTime +
", dehExecuteType='" + dehExecuteType + '\'' +
", dehRowCount=" + dehRowCount +
", dehContext='" + dehContext + '\'' +
", dehStatus='" + dehStatus + '\'' +
", dehError='" + dehError + '\'' +
", dehBakData='" + dehBakData + '\'' +
'}';
}
}
package com.hand.hls.dp.mapper;
import com.hand.hap.mybatis.common.Mapper;
import com.hand.hls.dp.dto.SysDpExecuteHistory;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/5/12 19:17
* @since
*/
public interface SysDpExecuteHistoryMapper extends Mapper<SysDpExecuteHistory> {
}
package com.hand.hls.dp.service;
import com.hand.hap.core.IRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/20 10:02
* @since
*/
public interface SysDpDownloadLogService {
void download(IRequest requestContext, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
package com.hand.hls.dp.service;
import com.hand.hap.core.IRequest;
import com.hand.hap.core.ProxySelf;
import com.hand.hap.system.dto.ResponseData;
import com.hand.hap.system.service.IBaseService;
import com.hand.hls.dp.dto.SysDpExecuteHistory;
import javax.servlet.http.HttpServletResponse;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 15:57
* @since
*/
public interface SysDpExecuteHistoryService extends IBaseService<SysDpExecuteHistory>, ProxySelf<SysDpExecuteHistoryService> {
void query4ck(IRequest requestContext, final String ip, final String dpContext) throws Exception;
void query(IRequest requestContext, HttpServletResponse response, final String ip, final String dpContext) throws Exception;
ResponseData insert(IRequest requestContext, final String ip, final String dpContext);
ResponseData update(IRequest requestContext, final String ip, final String dpContext);
ResponseData delete(IRequest requestContext, final String ip, final String dpContext);
ResponseData ddl(IRequest requestContext, final String ip, final String dpContext);
void insertSysDpExecuteHistory(IRequest requestContext, SysDpExecuteHistory sysDpExecuteHistory);
}
package com.hand.hls.dp.service.impl;
import cn.hutool.core.date.DateUtil;
import com.hand.hap.core.IRequest;
import com.hand.hls.dp.service.SysDpDownloadLogService;
import com.hand.hls.dp.util.LogUtils;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/20 10:03
* @since
*/
@Service
public class SysDpDownloadLogServiceImpl implements SysDpDownloadLogService {
@Value("${log-path}")
private String logPath;
@Override
public void download(IRequest requestContext, HttpServletRequest request, HttpServletResponse response) throws Exception {
String zipFileName = DateUtil.format(new Date(),"yyyyMMddHHmmss") + "_logs.zip";
response.reset();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache,must-revalidate");
response.setHeader("Expires","0");
response.setHeader("Content-Disposition","attachment;filename=" + new String(zipFileName.getBytes(), StandardCharsets.ISO_8859_1));
response.setContentType("application/x-msdownload;");
ByteArrayOutputStream byteOutPut = new ByteArrayOutputStream();
CheckedOutputStream cos = new CheckedOutputStream(byteOutPut,new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
// 下载7天内的日志
String[] downloadFiles = new String[7];
int length = downloadFiles.length;
// 当前日志
downloadFiles[0] = logPath + "leaf.log";
for (int i = 1; i <= length - 1;i++){
LocalDate tmp = LocalDate.now().minusDays(i);
Date date = Date.from(tmp.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
downloadFiles[i] = logPath + "leaf-" + DateUtil.format(new Date(),"yyyy-MM-dd") + ".log";
}
// 查找日志目录下所有文件
List<String> files = new ArrayList<>(30);
LogUtils.findFiles(new File(logPath),files);
files.forEach(file -> {
for (int i = 0; i < length; i++) {
if (file.equals(downloadFiles[i])) {
// 创建ZipEntry
ZipEntry entry = new ZipEntry(file.substring(logPath.length()));
// 放入文件,防止文件名相同的附件报错
try {
zos.putNextEntry(entry);
} catch (Exception e) {
// ignore
}
// 写入文件
LogUtils.readFileToZip(file,zos);
}
}
});
zos.closeEntry();
zos.close();
response.setHeader("Content-Length", String.valueOf(byteOutPut.size()));
IOUtils.copy(new ByteArrayInputStream(byteOutPut.toByteArray()), response.getOutputStream());
cos.close();
}
}
package com.hand.hls.dp.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.dto.SysDpExecuteHistory;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
import com.hand.hls.dp.util.SqlConstantUtils;
import com.hand.hls.dp.util.SqlUtils;
import hls.core.sys.mapper.SysCodeValueMapper;
import leaf.plugin.export.components.ExcelExportUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:07
* @since
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecuteHistory> implements SysDpExecuteHistoryService {
private static final Logger logger = LoggerFactory.getLogger(SysDpExecuteHistoryServiceImpl.class);
/**
* Default initial capacity.
*/
private static final int DEFAULT_INITIAL_CAPACITY = 16;
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Autowired
private SysCodeValueMapper sysCodeValueMapper;
/**
* 查询结果导出excel之前先检验sql是否存在问题?
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
*/
@Override
public void query4ck(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute query4ck");
logger.info("ip:[{}] ==============>检验查询待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
verifySql4Query(msg, dpContext);
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlUtils.checkSqlIgnoreCase(dpContext, SqlConstantUtils.SQL_STAR)) {
msg.append("select语句不允许使用*查询所有字段,请重置语句!");
}
}
dpContext = StringUtils.trimToEmpty(StringUtils.replaceChars(StringUtils.replaceChars(dpContext, StringUtils.CR, StringUtils.SPACE), StringUtils.LF, StringUtils.SPACE));
if (StringUtils.isNotEmpty(msg.toString())) {
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_SELECT);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
sysDpExecuteHistory.setDehError(msg.toString());
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("ip:[{}] ==============>验证不通过:[{}]", ip, msg.toString());
}
throw new IllegalArgumentException(msg.toString());
}
SqlSession sqlSession = sqlSessionFactory.openSession();
try (Connection conn = DBUtil.getConnectionBySqlSession(sqlSession);
PreparedStatement preparedStatement = conn.prepareStatement(dpContext);
ResultSet rs = preparedStatement.executeQuery()) {
if (!rs.next()) {
sqlSession.close();
throw new IllegalArgumentException("未查询到任何数据!");
}
sqlSession.close();
} catch (Exception e) {
sqlSession.close();
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_SELECT);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============>出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(msg.toString());
}
}
if (logger.isInfoEnabled()) {
logger.info("<============== end execute query4ck");
}
}
/**
* 查询结果导出excel
*
* @param requestContext 请求上下文
* @param response 返回
* @param ip 请求ip
* @param dpContext 待执行内容
*/
@Override
public void query(IRequest requestContext, HttpServletResponse response, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute query");
logger.info("ip:[{}] ==============>查询待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_SELECT);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
JSONArray coumnConfig = new JSONArray(DEFAULT_INITIAL_CAPACITY);
List<Map<String, Object>> retList = new ArrayList<>(DEFAULT_INITIAL_CAPACITY);
if (StringUtils.isNotBlank(dpContext)) {
dpContext = StringUtils.trimToEmpty(StringUtils.replaceChars(StringUtils.replaceChars(dpContext, StringUtils.CR, StringUtils.SPACE), StringUtils.LF, StringUtils.SPACE));
StringBuilder msg = new StringBuilder();
verifySql4Query(msg, dpContext);
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlUtils.checkSqlIgnoreCase(dpContext, SqlConstantUtils.SQL_STAR)) {
msg.append("select语句不允许使用*查询所有字段,请重置语句!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(msg.toString());
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
throw new IllegalArgumentException(msg.toString());
}
SqlSession sqlSession = sqlSessionFactory.openSession();
try (Connection conn = DBUtil.getConnectionBySqlSession(sqlSession);
PreparedStatement preparedStatement = conn.prepareStatement(dpContext);
ResultSet rs = preparedStatement.executeQuery()) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
ResultSetMetaData md = rs.getMetaData();
int columCount = md.getColumnCount();
// 组装excel列信息
for (int i = 1; i <= columCount; i++) {
String columnName = md.getColumnName(i);
Map<String, Object> columnInfo = new HashMap<>(4);
columnInfo.put("width", 150);
columnInfo.put("name", columnName);
columnInfo.put("align", "left");
columnInfo.put("prompt", columnName);
JSONObject columnJsonObject = new JSONObject(columnInfo);
coumnConfig.add(columnJsonObject);
}
while (rs.next()) {
Map<String, Object> rowData = new HashMap<>(1);
try {
getType(rs, md, rowData);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage());
}
}
retList.add(rowData);
}
sysDpExecuteHistory.setDehRowCount(retList.size());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (Exception e) {
sqlSession.close();
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
// 查询结果导出excel
ExcelExportUtil excelExportUtil = new ExcelExportUtil();
excelExportUtil.createExcel(response, coumnConfig, retList);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute query");
}
}
/**
* 查询通用验证
*
* @param msg 消息内容
* @param dpContext 待验证内容
*/
private void verifySql4Query(StringBuilder msg, String dpContext) {
if (!SqlUtils.checkSqlIgnoreCase(dpContext, SqlConstantUtils.SQL_WHERE)) {
msg.append("传入的语句没有where条件,请检查!");
} else {
// 校验where 后有没有条件
if (SqlUtils.checkSqlEndsWithIgnoreCase(dpContext, SqlConstantUtils.SQL_WHERE)) {
msg.append("传入的语句where条件不成立,请检查!");
}
}
if (SqlUtils.checkSqlStartWithIgnoreCase(dpContext, SqlConstantUtils.SQL_SELECT)) {
msg.append("传入的语句不是select语句,请检查!");
}
if (SqlUtils.checkSql4WhereOneToOne(dpContext)) {
msg.append("传入的语句含有1=1,请检查!");
}
}
/**
* 新增
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData insert(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute insert");
logger.info("ip:[{}] ==============>新增待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_INSERT);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
// 支持多条语句执行,以“;“隔开
String[] undos = StringUtils.split(dpContext, ";");
SqlSession sqlSession = sqlSessionFactory.openSession();
try (Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
conn.setAutoCommit(false);
for (String undo : undos) {
if (StringUtils.isBlank(undo)) {
continue;
}
undo = StringUtils.trimToEmpty(StringUtils.trimToEmpty(StringUtils.replaceChars(StringUtils.replaceChars(undo, StringUtils.CR, StringUtils.SPACE), StringUtils.LF, StringUtils.SPACE)));
SysDpExecuteHistory insertDpExecuteHistory = new SysDpExecuteHistory();
insertDpExecuteHistory.setDehIp(ip);
insertDpExecuteHistory.setDehUserId(requestContext.getUserId());
insertDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_INSERT);
insertDpExecuteHistory.setDehContext(undo);
insertDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
StringBuilder msg = new StringBuilder();
if (SqlUtils.checkSqlStartWithIgnoreCase(undo, SqlConstantUtils.SQL_INSERT_INTO)) {
msg.append("传入的语句不是insert语句,请检查!");
}
if (SqlUtils.checkSql4WhereOneToOne(undo)) {
msg.append("传入的语句含有1=1,请检查!");
}
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlUtils.checkSqlFilterTableIgnoreCase(undo, SqlConstantUtils.SQL_FILTER_TABLE)) {
msg.append("当前网络环境不允许新增数据!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
insertDpExecuteHistory.setDehExecuteTime(new Date());
insertDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, insertDpExecuteHistory);
responseData.setMessage("错误:" + undo + ";之前语句均已执行成功。错误消息:" + msg.toString());
return responseData;
}
try (PreparedStatement preparedStatement = conn.prepareStatement(undo)) {
int rowCount = preparedStatement.executeUpdate();
insertDpExecuteHistory.setDehExecuteTime(new Date());
if (logger.isInfoEnabled()) {
logger.info("本次插入[{}]条数据。", rowCount);
}
responseData.setSuccess(rowCount > 0);
responseData.setMessage("本次插入" + rowCount + "条数据。");
insertDpExecuteHistory.setDehRowCount(rowCount);
insertDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
self().insertSysDpExecuteHistory(requestContext, insertDpExecuteHistory);
conn.commit();
} catch (SQLException e) {
sqlSession.close();
insertDpExecuteHistory.setDehExecuteTime(new Date());
insertDpExecuteHistory.setDehError(e.getMessage());
insertDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, insertDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + undo + ";之前语句均已执行成功。错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============>执行语句:[{}];出错:[{}]", ip, undo, e.getMessage());
}
return responseData;
}
}
responseData.setSuccess(true);
responseData.setMessage("执行成功!");
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("所有子句均已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (SQLException e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
responseData.setMessage(e.getMessage());
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============>出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute insert");
}
return responseData;
}
/**
* 更新
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData update(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute update");
logger.info("ip:[{}] ==============>更新待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_UPDATE);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
// 支持多条语句执行,以“;“隔开
String[] undos = StringUtils.split(dpContext, ";");
SqlSession sqlSession = sqlSessionFactory.openSession();
try (Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
conn.setAutoCommit(false);
for (String undo : undos) {
if (StringUtils.isBlank(undo)) {
continue;
}
undo = StringUtils.trimToEmpty(StringUtils.replaceChars(StringUtils.replaceChars(undo, StringUtils.CR, StringUtils.SPACE), StringUtils.LF, StringUtils.SPACE));
SysDpExecuteHistory updateDpExecuteHistory = new SysDpExecuteHistory();
updateDpExecuteHistory.setDehIp(ip);
updateDpExecuteHistory.setDehUserId(requestContext.getUserId());
updateDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_UPDATE);
updateDpExecuteHistory.setDehContext(undo);
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
StringBuilder msg = new StringBuilder();
if (SqlUtils.checkSqlStartWithIgnoreCase(undo, SqlConstantUtils.SQL_UDPATE)) {
msg.append("传入的语句不是update语句,请检查!");
}
if (!SqlUtils.checkSqlIgnoreCase(undo, SqlConstantUtils.SQL_SET)) {
msg.append("传入的语句没有set,请检查!");
}
if (!SqlUtils.checkSqlIgnoreCase(undo, SqlConstantUtils.SQL_WHERE)) {
msg.append("传入的语句没有where条件,请检查!");
} else {
// 校验where 后有没有条件
if (SqlUtils.checkSqlEndsWithIgnoreCase(undo, SqlConstantUtils.SQL_WHERE)) {
msg.append("传入的语句where条件不成立,请检查!");
}
}
if (SqlUtils.checkSql4WhereOneToOne(undo)) {
msg.append("传入的语句含有1=1,请检查!");
}
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlUtils.checkSqlFilterTableIgnoreCase(undo, SqlConstantUtils.SQL_FILTER_TABLE)) {
msg.append("当前网络环境不允许修改数据!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
updateDpExecuteHistory.setDehExecuteTime(new Date());
updateDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, updateDpExecuteHistory);
responseData.setMessage(msg.toString());
return responseData;
}
String sql = "SELECT COUNT(*) AS TOTALCOUNT FROM ";
// 默认截取update之后语句
if (StringUtils.contains(undo, SqlConstantUtils.SQL_SET)) {
sql = sql + StringUtils.substring(undo, 6, StringUtils.indexOf(undo, SqlConstantUtils.SQL_SET));
} else if (StringUtils.contains(undo, SqlConstantUtils.SQL_SET_UPPER)) {
sql = sql + StringUtils.substring(undo, 6, StringUtils.indexOf(undo, SqlConstantUtils.SQL_SET_UPPER));
}
if (StringUtils.contains(undo, SqlConstantUtils.SQL_WHERE)) {
sql = sql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_WHERE));
} else if (StringUtils.contains(undo, SqlConstantUtils.SQL_WHERE_UPPER)) {
sql = sql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_WHERE_UPPER));
}
// 先查询受where条件影响数据
int rowCount = 0;
try (PreparedStatement preparedStatementQuery = conn.prepareStatement(sql);
ResultSet rs = preparedStatementQuery.executeQuery()) {
while (rs.next()) {
rowCount = rs.getInt("TOTALCOUNT");
}
if (logger.isInfoEnabled()) {
logger.info("==============>执行sql[{}],本次更新条件查询:[{}]条数据。", sql, rowCount);
}
} catch (SQLException e) {
sqlSession.close();
updateDpExecuteHistory.setDehExecuteTime(new Date());
updateDpExecuteHistory.setDehContext(sql);
updateDpExecuteHistory.setDehError(e.getMessage());
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + sql + "。错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============>执行语句:[{}];出错:[{}]", ip, sql, e.getMessage());
}
return responseData;
}
// 备份数据
String bakSql = "SELECT * FROM ";
// 默认截取update之后语句
if (StringUtils.contains(undo, SqlConstantUtils.SQL_SET)) {
bakSql = bakSql + StringUtils.substring(undo, 6, StringUtils.indexOf(undo, SqlConstantUtils.SQL_SET));
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_SET_UPPER)) {
bakSql = bakSql + StringUtils.substring(undo, 6, StringUtils.indexOf(undo, SqlConstantUtils.SQL_SET_UPPER));
}
if (StringUtils.contains(undo, SqlConstantUtils.SQL_WHERE)) {
bakSql = bakSql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_WHERE));
} else if (StringUtils.contains(undo, SqlConstantUtils.SQL_WHERE_UPPER)) {
bakSql = bakSql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_WHERE_UPPER));
}
String bakData = bakData(conn, bakSql);
// 执行传入语句
try (PreparedStatement preparedStatement = conn.prepareStatement(undo)) {
int updateRowCount = preparedStatement.executeUpdate();
if (logger.isInfoEnabled()) {
logger.info("==============>执行sql[{}],本次更新:[{}]条数据。", undo, updateRowCount);
}
updateDpExecuteHistory.setDehExecuteTime(new Date());
// 查询结果==更新结果
if (updateRowCount == rowCount) {
conn.commit();
if (logger.isInfoEnabled()) {
logger.info("本次更新[{}]条数据。", updateRowCount);
}
responseData.setSuccess(updateRowCount > 0);
responseData.setMessage("本次更新" + updateRowCount + "条数据。");
responseData.setTotal((long) updateRowCount);
updateDpExecuteHistory.setDehRowCount(updateRowCount);
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
updateDpExecuteHistory.setDehBakData(bakData);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
} else {
conn.rollback();
sqlSession.close();
updateDpExecuteHistory.setDehError("更新条目与查询条目不一致!本次更新前条件查询" + rowCount + "条数据。本次更新" + updateRowCount + "条数据。");
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + undo + "之前语句均已执行成功!错误消息::更新条目与查询条目不一致!本次更新前条件查询" + rowCount + "条数据。本次更新" + updateRowCount + "条数据。");
return responseData;
}
} catch (SQLException e) {
sqlSession.close();
updateDpExecuteHistory.setDehExecuteTime(new Date());
updateDpExecuteHistory.setDehError(e.getMessage());
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + undo + ";之前语句均已执行成功。错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============>执行语句:[{}];出错:[{}]", ip, undo, e.getMessage());
}
return responseData;
}
}
responseData.setSuccess(true);
responseData.setMessage("执行成功!");
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("所有子句均已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (SQLException e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
responseData.setMessage(e.getMessage());
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============>出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute update");
}
return responseData;
}
/**
* 删除
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData delete(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute delete");
logger.info("ip:[{}] ==============>删除待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DELETE);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
// 支持多条语句执行,以“;“隔开
String[] undos = StringUtils.split(dpContext, ";");
SqlSession sqlSession = sqlSessionFactory.openSession();
try (Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
conn.setAutoCommit(false);
for (String undo : undos) {
if (StringUtils.isBlank(undo)) {
continue;
}
undo = StringUtils.trimToEmpty(StringUtils.replaceChars(StringUtils.replaceChars(undo, StringUtils.CR, StringUtils.SPACE), StringUtils.LF, StringUtils.SPACE));
StringBuilder msg = new StringBuilder();
SysDpExecuteHistory delDpExecuteHistory = new SysDpExecuteHistory();
delDpExecuteHistory.setDehIp(ip);
delDpExecuteHistory.setDehUserId(requestContext.getUserId());
delDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DELETE);
delDpExecuteHistory.setDehContext(undo);
delDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (SqlUtils.checkSqlStartWithIgnoreCase(undo, SqlConstantUtils.SQL_DELETE)) {
msg.append("传入的语句不是delete语句,请检查!");
}
if (!SqlUtils.checkSqlIgnoreCase(undo, SqlConstantUtils.SQL_WHERE)) {
msg.append("传入的语句没有where条件,请检查!");
} else {
// 校验where 后有没有条件
if (SqlUtils.checkSqlEndsWithIgnoreCase(undo, SqlConstantUtils.SQL_WHERE)) {
msg.append("传入的语句where条件不成立,请检查!");
}
}
if (SqlUtils.checkSql4WhereOneToOne(undo)) {
msg.append("传入的语句含有1=1,请检查!");
}
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlUtils.checkSqlFilterTableIgnoreCase(undo, SqlConstantUtils.SQL_FILTER_TABLE)) {
msg.append("当前网络环境不允许删除数据!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
delDpExecuteHistory.setDehExecuteTime(new Date());
delDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, delDpExecuteHistory);
responseData.setMessage(msg.toString());
return responseData;
}
String sql = "SELECT COUNT(*) AS TOTALCOUNT FROM ";
// 默认截取from之后语句
if (StringUtils.contains(undo, SqlConstantUtils.SQL_FROM)) {
sql = sql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_FROM));
} else if (StringUtils.contains(undo, SqlConstantUtils.SQL_FROM_UPPER)) {
sql = sql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_FROM_UPPER));
}
// 先查询受where条件影响数据
int rowCount = 0;
try (PreparedStatement preparedStatementQuery = conn.prepareStatement(sql);
ResultSet rs = preparedStatementQuery.executeQuery()) {
while (rs.next()) {
rowCount = rs.getInt("TOTALCOUNT");
}
if (logger.isInfoEnabled()) {
logger.info("==============>执行sql[{}],本次删除前条件查询:[{}]条数据。", sql, rowCount);
}
} catch (SQLException e) {
sqlSession.close();
sysDpExecuteHistory.setDehExecuteTime(new Date());
delDpExecuteHistory.setDehContext(sql);
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + sql + "。错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============>执行语句:[{}];出错:[{}]", ip, sql, e.getMessage());
}
return responseData;
}
// 备份数据
String bakSql = "SELECT * ";
// 默认截取from之后语句
if (StringUtils.contains(undo, SqlConstantUtils.SQL_FROM)) {
bakSql = bakSql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_FROM));
} else if (StringUtils.contains(undo, SqlConstantUtils.SQL_FROM_UPPER)) {
bakSql = bakSql + StringUtils.substring(undo, StringUtils.indexOf(undo, SqlConstantUtils.SQL_FROM_UPPER));
}
String bakData = bakData(conn, bakSql);
// 执行传入语句
try (PreparedStatement preparedStatement = conn.prepareStatement(undo)) {
int deleteRowCount = preparedStatement.executeUpdate();
if (logger.isInfoEnabled()) {
logger.info("==============>执行sql[{}],本次删除:[{}]条数据。", undo, deleteRowCount);
}
delDpExecuteHistory.setDehExecuteTime(new Date());
// 查询结果==删除结果
if (deleteRowCount == rowCount) {
conn.commit();
if (logger.isInfoEnabled()) {
logger.info("本次删除[{}]条数据。", rowCount);
}
responseData.setSuccess(deleteRowCount > 0);
responseData.setMessage("本次删除" + deleteRowCount + "条数据。");
responseData.setTotal((long) deleteRowCount);
delDpExecuteHistory.setDehRowCount(deleteRowCount);
delDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
delDpExecuteHistory.setDehBakData(bakData);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
} else {
conn.rollback();
delDpExecuteHistory.setDehError("删除条目与查询条目不一致!本次删除前条件查询" + rowCount + "条数据。本次删除" + deleteRowCount + "条数据。");
delDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + undo + "之前语句均已执行成功!错误消息:删除条目与查询条目不一致!本次删除前条件查询" + rowCount + "条数据。本次删除" + deleteRowCount + "条数据。");
return responseData;
}
} catch (SQLException e) {
sqlSession.close();
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + undo + ";之前语句均已执行成功。错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============>执行语句:[{}];出错:[{}]", ip, undo, e.getMessage());
}
return responseData;
}
}
responseData.setSuccess(true);
responseData.setMessage("执行成功!");
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("所有子句均已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (Exception e) {
sqlSession.close();
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============>出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute delete");
}
return responseData;
}
/**
* ddl
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData ddl(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute ddl");
logger.info("ip:[{}] ==============>待执行DDL语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DDL);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
msg.append("当前网络环境不允许执行语句!");
}
if (StringUtils.isNotEmpty(msg.toString())) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, sysDpExecuteHistory);
responseData.setMessage(msg.toString());
return responseData;
}
SqlSession sqlSession = sqlSessionFactory.openSession();
try (Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
// 支持多条语句执行,以“;“隔开
String[] undos = StringUtils.split(dpContext, ";");
for (String undo : undos) {
if (StringUtils.isBlank(undo)) {
continue;
}
undo = StringUtils.trimToEmpty(StringUtils.replaceChars(StringUtils.replaceChars(undo, StringUtils.CR, StringUtils.SPACE), StringUtils.LF, StringUtils.SPACE));
SysDpExecuteHistory ddlDpExecuteHistory = new SysDpExecuteHistory();
ddlDpExecuteHistory.setDehIp(ip);
ddlDpExecuteHistory.setDehUserId(requestContext.getUserId());
ddlDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DDL);
ddlDpExecuteHistory.setDehContext(undo);
// 不允许drop&truncate
if (SqlUtils.checkSqlStartWith4DdlIgnoreCase(undo, SqlConstantUtils.SQL_DROP)) {
msg.append("传入的语句含有drop语句,请检查!");
}
if (SqlUtils.checkSqlStartWith4DdlIgnoreCase(undo, SqlConstantUtils.SQL_TRUNCATE)) {
msg.append("传入的语句含有truncate语句,请检查!");
}
// 不允许有dml语句
if (SqlUtils.checkSqlStartWith4DdlIgnoreCase(undo, SqlConstantUtils.SQL_SELECT)) {
msg.append("传入的语句含有select语句,请检查!");
}
if (SqlUtils.checkSqlStartWith4DdlIgnoreCase(undo, SqlConstantUtils.SQL_INSERT_INTO)) {
msg.append("传入的语句含有insert语句,请检查!");
}
if (SqlUtils.checkSqlStartWith4DdlIgnoreCase(undo, SqlConstantUtils.SQL_UDPATE)) {
msg.append("传入的语句含有update语句,请检查!");
}
if (SqlUtils.checkSqlStartWith4DdlIgnoreCase(undo, SqlConstantUtils.SQL_DELETE)) {
msg.append("传入的语句含有delete语句,请检查!");
}
if (StringUtils.isNotEmpty(msg.toString())) {
ddlDpExecuteHistory.setDehExecuteTime(new Date());
ddlDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, ddlDpExecuteHistory);
responseData.setMessage("错误:" + undo + ";" + msg.toString());
return responseData;
}
try (PreparedStatement ddl = conn.prepareStatement(undo)) {
ddl.executeUpdate();
ddlDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
ddlDpExecuteHistory.setDehExecuteTime(new Date());
self().insertSysDpExecuteHistory(requestContext, ddlDpExecuteHistory);
} catch (SQLException e) {
sqlSession.close();
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, ddlDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误:" + undo + ";之前语句均已执行成功。错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============>执行语句:[{}];出错:[{}]", ip, undo, e.getMessage());
}
return responseData;
}
}
responseData.setSuccess(true);
responseData.setMessage("执行成功!");
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("所有子句均已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (Exception e) {
sqlSession.close();
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============>出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute ddl");
}
return responseData;
}
/**
* 备份数据
*
* @param conn 链接
* @param bakSql 备份待执行sql
* @return 返回消息
* @throws SQLException sql例外
*/
private String bakData(Connection conn, String bakSql) throws SQLException {
PreparedStatement bakQuery = conn.prepareStatement(bakSql);
ResultSet bakRs = bakQuery.executeQuery();
String bakData = null;
try {
bakData = convertToJson(bakRs);
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("==============>执行备份语句:[{}];出错:[{}]", bakSql, e.getMessage());
}
}
bakQuery.close();
bakRs.close();
return bakData;
}
/**
* rs结果转换jsoc
*
* @param rs JDBC返回结果集
* @return 返回消息
* @throws Exception 例外
*/
private static String convertToJson(ResultSet rs) throws Exception {
JSONArray jsonArray = new JSONArray(DEFAULT_INITIAL_CAPACITY);
ResultSetMetaData metaData = rs.getMetaData();
while (rs.next()) {
JSONObject jsonObject = new JSONObject(DEFAULT_INITIAL_CAPACITY);
getType(rs, metaData, jsonObject);
jsonArray.add(jsonObject);
}
return jsonArray.toJSONString();
}
/**
* 根据类型转换数据
*
* @param rs JDBC返回结果集
* @param md 元数据
* @param jsonObject json对象
* @throws Exception 例外
*/
private static void getType(ResultSet rs, ResultSetMetaData md, Map<String, Object> jsonObject) throws Exception {
int columnCount = md.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String columnName = md.getColumnName(i);
int columnType = md.getColumnType(i);
switch (columnType) {
case Types.ARRAY:
jsonObject.put(columnName, rs.getArray(columnName));
break;
case Types.BIGINT:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.BOOLEAN:
jsonObject.put(columnName, rs.getBoolean(columnName));
break;
case Types.BLOB:
Blob blob = rs.getBlob(columnName);
byte[] bytes = new byte[(int) blob.length()];
try (InputStream in = blob.getBinaryStream()) {
in.read(bytes);
String tmp = new String(bytes);
jsonObject.put(columnName, tmp);
} catch (IOException e) {
// ignore
}
break;
case Types.DOUBLE:
jsonObject.put(columnName, rs.getDouble(columnName));
break;
case Types.FLOAT:
jsonObject.put(columnName, rs.getFloat(columnName));
break;
case Types.INTEGER:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.NVARCHAR:
jsonObject.put(columnName, rs.getNString(columnName));
break;
case Types.VARCHAR:
jsonObject.put(columnName, rs.getString(columnName));
break;
case Types.TINYINT:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.SMALLINT:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.DATE:
jsonObject.put(columnName, rs.getDate(columnName));
break;
case Types.TIMESTAMP:
jsonObject.put(columnName, rs.getTimestamp(columnName));
break;
case Types.CLOB:
jsonObject.put(columnName, rs.getString(columnName));
break;
case Types.DECIMAL:
jsonObject.put(columnName, rs.getBigDecimal(columnName));
break;
case Types.NUMERIC:
jsonObject.put(columnName, rs.getBigDecimal(columnName));
break;
default:
jsonObject.put(columnName, rs.getObject(columnName));
break;
}
}
}
/**
* 自定义事务,保存查询预校验执行记录
*
* @param requestContext 请求上下文
* @param sysDpExecuteHistory 传入的参数
*/
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
@Override
public void insertSysDpExecuteHistory(IRequest requestContext, SysDpExecuteHistory sysDpExecuteHistory) {
self().insertSelective(requestContext, sysDpExecuteHistory);
}
}
package com.hand.hls.dp.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static java.util.Collections.emptyList;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/6/4 12:30
* @since
*/
public class ArrayUtils {
/**
* Indicates whether the given object is not {@code null} and is an array.
*
* @param o the given object.
* @return {@code true} if the given object is not {@code null} and is an array, otherwise {@code false}.
*/
public static boolean isArray(Object o) {
return o != null && o.getClass().isArray();
}
/**
* Indicates whether the given array is {@code null} or empty.
*
* @param <T> the type of elements of the array.
* @param array the array to check.
* @return {@code true} if the given array is {@code null} or empty, otherwise {@code false}.
*/
public static <T> boolean isNullOrEmpty(T[] array) {
return array == null || isEmpty(array);
}
/**
* Returns an array containing the given arguments.
*
* @param <T> the type of the array to return.
* @param values the values to store in the array.
* @return an array containing the given arguments.
*/
@SafeVarargs
public static <T> T[] array(T... values) {
return values;
}
/**
* Returns all the non-{@code null} elements in the given array.
*
* @param <T> the type of elements of the array.
* @param array the given array.
* @return all the non-{@code null} elements in the given array. An empty list is returned if the given array is
* {@code null}.
*/
public static <T> List<T> nonNullElementsIn(T[] array) {
if (array == null) {
return emptyList();
}
List<T> nonNullElements = new ArrayList<>();
for (T o : array) {
if (o != null) {
nonNullElements.add(o);
}
}
return nonNullElements;
}
/**
* Returns {@code true} if the given array has only {@code null} elements, {@code false} otherwise. If given array is
* empty, this method returns {@code true}.
*
* @param <T> the type of elements of the array.
* @param array the given array. <b>It must not be null</b>.
* @return {@code true} if the given array has only {@code null} elements or is empty, {@code false} otherwise.
* @throws NullPointerException if the given array is {@code null}.
*/
public static <T> boolean hasOnlyNullElements(T[] array) {
Objects.requireNonNull(array);
if (isEmpty(array)) {
return false;
}
for (T o : array) {
if (o != null) {
return false;
}
}
return true;
}
private static <T> boolean isEmpty(T[] array) {
return array.length == 0;
}
public static boolean isObjectArray(Object o) {
return isArray(o) && !isArrayTypePrimitive(o);
}
public static boolean isArrayTypePrimitive(Object o) {
return o != null && o.getClass().getComponentType().isPrimitive();
}
}
package com.hand.hls.dp.util;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.nio.Buffer;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 15:41
* @since
*/
public class IPUtils {
private static final String IPV4_LOCAL_HOST = "127.0.0.1";
private static final String IPV6_LOCAL_HOST = "0:0:0:0:0:0:0:1";
public static String getIpAddress(HttpServletRequest request) {
if (Objects.isNull(request)) {
return "";
}
// X-Forwarded-For:Squid 服务代理
String ip = request.getHeader("x-forwarded-for");
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// Proxy-Client-IP:apache 服务代理
ip = request.getHeader("Proxy-Client-IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// WL-Proxy-Client-IP:weblogic 服务代理
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// X-Real-IP:nginx服务代理
ip = request.getHeader("X-Real-IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
// 本地局域网放行
if (Objects.equals(IPV4_LOCAL_HOST,ip) || Objects.equals(IPV6_LOCAL_HOST,ip)) {
// 根据网卡获取本机配置的ip
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
} catch (Exception e) {
// 默认
ip = IPV4_LOCAL_HOST;
}
byte[] address = inetAddress.getAddress();
if (address.length == 16) {
// IPV6
ip = inetAddress.getHostAddress();
} else if (address.length == 4) {
// IPV4
ip = inetAddress.getHostAddress();
} else {
//throw new Exception("Invalid IPv6 address: '" + ip + '\'');
// 默认
ip = IPV6_LOCAL_HOST;
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
// "***.***.***.***".length() = 15
if (Objects.nonNull(ip) && ip.length() > 15) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return extractString(ip);
}
/**
* 获取访问主机的网段(4位)
*/
private static String extractString(String s) {
if (StringUtils.isEmpty(s)) {
return "";
}
StringBuilder retVal = new StringBuilder();
String[] split = s.split("\\.");
for (int i = 0; i < 4; i++) {
retVal.append(split[i]);
if (i < 3) {
retVal.append(".");
}
}
return retVal.toString();
}
// private static final String inet6AddressToAscii(byte[] address) {
// int longestRunOffset = -1;
// int longestRunLength = 0;
// boolean var3 = false;
// boolean var4 = false;
// int var5 = false;
//
// for(int i = 0; i < address.length; i += 2) {
// int currentRunOffset;
// for(currentRunOffset = i; i < 16 && address[i] == 0 && address[i + 1] == 0; i += 2) {
// }
//
// int currentRunLength = i - currentRunOffset;
// if (currentRunLength > longestRunLength && currentRunLength >= 4) {
// longestRunOffset = currentRunOffset;
// longestRunLength = currentRunLength;
// }
// }
//
// Buffer result = new Buffer();
// int i = 0;
//
// while(i < address.length) {
// if (i == longestRunOffset) {
// result.writeByte(58);
// i += longestRunLength;
// if (i == 16) {
// result.writeByte(58);
// }
// } else {
// if (i > 0) {
// result.writeByte(58);
// }
//
// int group = Util.and(address[i], 255) << 8 | Util.and(address[i + 1], 255);
// result.writeHexadecimalUnsignedLong((long)group);
// i += 2;
// }
// }
//
// return result.readUtf8();
// }
}
package com.hand.hls.dp.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipOutputStream;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/20 10:19
* @since
*/
public class LogUtils {
private static final Logger logger = LoggerFactory.getLogger(LogUtils.class);
/**
* 兼容不同操作系统间区别
*/
private static final String separator = File.separator;
/** 默认缓存大小 */
public static final int DEFAULT_BUFFER_SIZE = 256;
/** 默认大缓存大小 */
public static final int DEFAULT_LARGE_BUFFER_SIZE = 1024;
/** 数据流末尾 */
public static final int EOF = -1;
/**
* 通过目录查找所属文件
* @param dir 待查询目录
* @param fileNames 查询结果
*/
public static void findFiles(File dir, List<String> fileNames){
Objects.requireNonNull(dir,"file dir must not be null");
if (!dir.exists() || !dir.isDirectory()) {
return;
}
String[] files = dir.list();
for (String s : Objects.requireNonNull(files)) {
File file = new File(dir, s);
if (file.isFile()) {
fileNames.add(dir + separator + file.getName());
} else {
findFiles(file, fileNames);
}
}
}
/**
* 读取文件内容写入ZipOutputStream
* @param filePath 待读取文件全路径
* @param zos 写入内容的ZipOutputStream
*/
public static void readFileToZip(final String filePath, ZipOutputStream zos){
Objects.requireNonNull(filePath,"filePath must not be null");
Objects.requireNonNull(zos,"ZipOutputStream must not be null");
try (RandomAccessFile raf = new RandomAccessFile(filePath,"r");
FileChannel fileChannel = raf.getChannel()){
ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_LARGE_BUFFER_SIZE);
int n = 0;
while (EOF != (n = fileChannel.read(buffer))) {
// 切换buffer读模式
buffer.flip();
while (buffer.hasRemaining()) {
int remain = buffer.remaining();
byte[] bytes = new byte[remain];
// 从buffer读取数据到bytes
buffer.get(bytes);
// 内容写入ZipOutputStream
zos.write(bytes,0,n);
}
// 切换buffer写模式
buffer.clear();
}
} catch (FileNotFoundException fe){
// 文件没找到
if (logger.isErrorEnabled()) {
logger.error("{} file not found!", filePath);
}
} catch (IOException e){
// io例外
if (logger.isWarnEnabled()) {
logger.warn("{} file IOException!Message:[{}]", filePath, e.getMessage());
}
}
}
}
package com.hand.hls.dp.util;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:25
* @since
*/
public interface SqlConstantUtils {
String SQL_STAR = "*";
String SQL_SELECT = "select";
String SQL_FROM = "from";
String SQL_FROM_UPPER = "FROM";
String SQL_WHERE = "where";
String SQL_WHERE_UPPER = "WHERE";
String SQL_WHERE_ADD = "and";
String SQL_WHERE_ADD_UPPER = "AND";
String SQL_UDPATE = "update";
String SQL_DELETE = "delete";
String SQL_DROP = "drop";
String SQL_TRUNCATE = "truncate";
String SQL_SET = "set";
String SQL_SET_UPPER = "SET";
String SQL_INSERT_INTO = "insert into";
String DDL_CREATE_TABLE = "create table";
String DDL_ALTER_TABLE = "alter table";
String DDL_COMMENT_TABLE = "comment on table";
String DDL_COMMENT_ON_COLUMN = "comment on column";
String DDL_CREATE_INDEX = "create index";
String DDL_CREATE_SEQUENCE = "create sequence";
String SQL_EXEC_SELECT = "SELECT";
String SQL_EXEC_INSERT = "INSERT";
String SQL_EXEC_UPDATE = "UPDATE";
String SQL_EXEC_DELETE = "DELETE";
String SQL_EXEC_DDL = "DDL";
String SQL_EXEC_DONE = "DONE";
String SQL_EXEC_DE = "DE";
String SQL_EXEC_UNDO = "UNDO";
String SQL_WHERE_1T1_EQ = "=";
String[] SQL_FILTER_TABLE = new String[]{"sys_dp_execute_history","QRTZ_","ACT_","SYS_USER","SYS_ROLE_B","SYS_FUNCTION_B","SYS_ROLE_FUNCTION",
"SYS_RESOURCE_B","SYS_JOB_RUNNING_INFO","SYS_MODULE","SYS_WIDGETS","USER_","DBA_",};
}
package com.hand.hls.dp.util;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:37
* @since
*/
public class SqlUtils {
public static boolean checkSql(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
return false;
}
for (String condition : conditions) {
if (StringUtils.contains(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSql4WhereOneToOne(String sql) {
int lastIndex = StringUtils.lastIndexOfIgnoreCase(sql, SqlConstantUtils.SQL_WHERE);
String condition = StringUtils.substringAfterLast(sql, StringUtils.substring(sql, lastIndex, lastIndex + 5));
String[] conditions = StringUtils.split(StringUtils.trimToEmpty(condition), SqlConstantUtils.SQL_WHERE_ADD);
if (ArrayUtils.isNullOrEmpty(conditions)) {
conditions = StringUtils.split(condition, SqlConstantUtils.SQL_WHERE_ADD_UPPER);
}
for (String c : conditions) {
c = StringUtils.deleteWhitespace(StringUtils.normalizeSpace(c));
String[] tmpCons = StringUtils.split(c, SqlConstantUtils.SQL_WHERE_1T1_EQ);
if (!ArrayUtils.isNullOrEmpty(tmpCons)) {
List<String> cons = ArrayUtils.nonNullElementsIn(tmpCons);
cons.remove(SqlConstantUtils.SQL_WHERE_1T1_EQ);
for (int i = 0; i < cons.size(); i++) {
if (i == 1) {
String startCon = cons.get(i - 1);
String endCon = cons.get(i);
if (StringUtils.equals(startCon, endCon)) {
return true;
}
}
}
}
}
return false;
}
public static boolean checkSqlIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (StringUtils.containsIgnoreCase(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlFilterTableIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (StringUtils.containsIgnoreCase(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlStartWithIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (!StringUtils.startsWith(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlStartWith4DdlIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_CREATE_TABLE)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_ALTER_TABLE)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_COMMENT_TABLE)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_COMMENT_ON_COLUMN)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_CREATE_INDEX)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_CREATE_SEQUENCE)) {
return false;
}
for (String condition : conditions) {
if (StringUtils.startsWith(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlEndsWithIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (StringUtils.endsWithIgnoreCase(sql, condition)) {
return true;
}
}
return false;
}
}
package hls.core.sys.mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* 此mapper只用于查询syscode相关的值,暂无其他方法
* Created by fjm on 2017/7/31.
*/
public interface SysCodeValueMapper {
String queryCodeValueMeaning(@Param("code") String code, @Param("code_value") String codeValue);
List<Map> queryCodeDetails(String code);
}
<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
<a:init-procedure/>
<a:view>
<script><![CDATA[
function dp001_query() {
$('dp001_query_btn').disable();
let dpContext = document.getElementById('context_tta').value;
Leaf.showConfirm('${l:HLS.PROMPT}', '确认执行当前语句吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在执行...');
Leaf.request({
url: '${/request/@context_path}/sys/dp/query?ck=0',
para: dpContext,
success: function () {
let form = document.createElement("form");
form.target = "_export_dp_query_window";
form.method = "post";
let url = '${/request/@context_path}/sys/dp/query?ck=1';
let token = $jq('meta[name=_csrf]').attr('content');
form.action = url + (url.indexOf('?') == -1 ? '?' : '&') + 'r=' + Math.random() + '&_csrf=' + token;
let iframe = Ext.get('_export_dp_query_window') || new Ext.Template('<iframe id ="_export_dp_query_window" name="_export_dp_query_window" style="position:absolute;left:-1000px;top:-1000px;width:1px;height:1px;display:none"></iframe>').insertFirst(document.body, {}, true)
var s = document.createElement("input");
s.id = "_request_data";
s.type = 'hidden';
s.name = '_request_data';
let p = {"parameter": dpContext};
s.value = Ext.encode(p);
form.appendChild(s);
document.body.appendChild(form);
form.submit();
Ext.fly(form).remove();
setTimeout(function () {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 10000);
},
failure: function () {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
error: function () {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
scope: this
});
}, function cancel() {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function dp001_insert() {
debugger
$('dp001_insert_btn').disable();
let dpContext = document.getElementById('context_tta').value;
Leaf.showConfirm('${l:HLS.PROMPT}', '确认执行当前语句吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在执行...');
Leaf.request({
url: '${/request/@context_path}/sys/dp/insert',
para: dpContext,
success: function (res) {
setTimeout(function () {
$('dp001_insert_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
if (res.success) {
Leaf.showMessage('提示', '新增成功!');
document.getElementById('result_tta').value = res.error.message;
}
}, 10000);
},
failure: function () {
$('dp001_insert_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
error: function () {
$('dp001_insert_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
scope: this
});
}, function cancel() {
$('dp001_insert_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function dp001_update() {
$('dp001_update_btn').disable();
let dpContext = document.getElementById('context_tta').value;
Leaf.showConfirm('${l:HLS.PROMPT}', '确认执行当前语句吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在执行...');
Leaf.request({
url: '${/request/@context_path}/sys/dp/update',
para: dpContext,
success: function (res) {
setTimeout(function () {
$('dp001_update_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
if (res.success) {
Leaf.showMessage('提示', '更新成功!');
document.getElementById('result_tta').value = res.error.message;
}
}, 10000);
},
failure: function () {
$('dp001_update_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
error: function () {
$('dp001_update_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
scope: this
});
}, function cancel() {
$('dp001_update_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function dp001_delete() {
$('dp001_delete_btn').disable();
let dpContext = document.getElementById('context_tta').value;
Leaf.showConfirm('${l:HLS.PROMPT}', '确认执行当前语句吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在执行...');
Leaf.request({
url: '${/request/@context_path}/sys/dp/delete',
para: dpContext,
success: function (res) {
setTimeout(function () {
$('dp001_delete_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
if (res.success) {
Leaf.showMessage('提示', '删除成功!');
document.getElementById('result_tta').value = res.error.message;
}
}, 10000);
},
failure: function () {
$('dp001_delete_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
error: function () {
$('dp001_delete_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
scope: this
});
}, function cancel() {
$('dp001_delete_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function dp001_ddl() {
$('dp001_ddl_btn').disable();
let dpContext = document.getElementById('context_tta').value;
Leaf.showConfirm('${l:HLS.PROMPT}', '确认执行当前语句吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在执行...');
Leaf.request({
url: '${/request/@context_path}/sys/dp/ddl',
para: dpContext,
success: function (res) {
setTimeout(function () {
$('dp001_ddl_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
if (res.success) {
Leaf.showMessage('提示', '执行成功!');
document.getElementById('result_tta').value = res.error.message;
}
}, 10000);
},
failure: function () {
$('dp001_ddl_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
error: function () {
$('dp001_ddl_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
scope: this
});
}, function cancel() {
$('dp001_ddl_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function download_log() {
$('dp001_download_log_btn').disable();
Leaf.showConfirm('${l:HLS.PROMPT}', '确认下载日志吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在下载中...');
let form = document.createElement("form");
form.target = "_export_dp_log_window";
form.method = "post";
let url = '${/request/@context_path}/sys/dp/download/log';
let token = $jq('meta[name=_csrf]').attr('content');
form.action = url + (url.indexOf('?') == -1 ? '?' : '&') + 'r=' + Math.random() + '&_csrf=' + token;
let iframe = Ext.get('_export_dp_log_window') || new Ext.Template('<iframe id ="_export_dp_log_window" name="_export_dp_log_window" style="position:absolute;left:-1000px;top:-1000px;width:1px;height:1px;display:none"></iframe>').insertFirst(document.body, {}, true)
document.body.appendChild(form);
form.submit();
Ext.fly(form).remove();
setTimeout(function () {
$('dp001_download_log_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 10000);
}, function cancel() {
$('dp001_download_log_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
]]></script>
<a:screenBody>
<a:screenTopToolbar>
<a:gridButton id="dp001_query_btn" click="dp001_query" text="查询"/>
<a:gridButton id="dp001_insert_btn" click="dp001_insert" text="新增"/>
<a:gridButton id="dp001_update_btn" click="dp001_update" text="更新"/>
<a:gridButton id="dp001_delete_btn" click="dp001_delete" text="删除"/>
<a:gridButton id="dp001_ddl_btn" click="dp001_ddl" text="ddl"/>
<a:gridButton id="dp001_download_log_btn" click="download_log" text="日志下载"/>
</a:screenTopToolbar>
<a:fieldSet title="待执行">
<a:textArea id="context_tta" name="context" marginWidth="50" marginHeight="450"/>
</a:fieldSet>
<a:fieldSet title="结果">
<a:textArea id="result_tta" readOnly="true" name="result" marginWidth="50" marginHeight="560"/>
</a:fieldSet>
</a:screenBody>
</a:view>
</a:screen>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="hls.core.sys.mapper.SysCodeValueMapper">
<!--
可以根据以下sql片段查询出syscode相关的值
使用方法如下:
<select id="demo">
select
view.code_id
from
<include refid="hls.core.sys.mapper.SysCodeValueMapper.queryCodeDetail" /> view
</select>
-->
<sql id="queryCodeDetail">
(SELECT
a.CODE_ID AS code_id ,
a.CODE AS code ,
a.DESCRIPTION AS code_name ,
b.CODE_VALUE_ID AS code_value_id ,
b.VALUE AS code_value ,
b.DESCRIPTION AS code_value_name ,
b.MEANING AS meaning ,
b.ORDER_SEQ AS order_seq
FROM
sys_code_b a
LEFT JOIN sys_code_value_b b
ON
a.CODE_ID = b.CODE_ID)
</sql>
<resultMap id="codeDetailsResultMap" type="map">
<result column="code_id" property="code_id"/>
<result column="code" property="code"/>
<result column="code_name" property="code_name"/>
<result column="code_value_id" property="code_value_id"/>
<result column="code_value" property="code_value"/>
<result column="code_value_name" property="code_value_name"/>
<result column="meaning" property="meaning"/>
<result column="order_seq" property="order_seq"/>
</resultMap>
<select id="queryCodeDetails" parameterType="java.lang.String" resultMap="codeDetailsResultMap">
SELECT
a.CODE_ID AS code_id ,
a.CODE AS code ,
a.DESCRIPTION AS code_name ,
b.CODE_VALUE_ID AS code_value_id ,
b.VALUE AS code_value ,
b.DESCRIPTION AS code_value_name ,
b.MEANING AS meaning ,
b.ORDER_SEQ AS order_seq
FROM
sys_code_b a
LEFT JOIN sys_code_value_b b
ON
a.CODE_ID = b.CODE_ID
where a.CODE = #{code}
</select>
<select id="queryCodeValueMeaning" parameterType="java.lang.String" resultType="java.lang.String">
SELECT b.MEANING AS meaning
FROM
sys_code_b a
LEFT JOIN sys_code_value_b b
ON
a.CODE_ID = b.CODE_ID
where b.enabled_flag = 'Y'
and b.value = #{code_value}
and a.CODE = #{code}
</select>
<sql id="queryFndEmployeeAssignsDetail" >
(
SELECT
t1.EMPLOYEE_ASSIGN_ID AS EMPLOYEE_ASSIGN_ID,
t1.ENABLED_FLAG AS enabled_flag,
t1.PRIMARY_POSITION_FLAG AS primary_position_flag,
t1.EMPLOYEE_ID AS employee_id,
e.ENABLED_FLAG AS employee_enabled,
e.EMPLOYEE_CODE AS EMPLOYEE_CODE,
e.NAME AS employee_name,
t1.COMPANY_ID AS company_id,
c.enabled_flag AS company_enabled,
c.company_code AS company_code,
c.company_short_name AS company_short_name,
t1.POSITION_ID AS position_id,
p.enabled_flag AS position_enabled,
p.position_code AS position_code,
p.position_type AS position_type,
p.position_name AS POSITION_NAME,
p.unit_id AS unit_id,
u.ENABLED_FLAG AS unit_enabled,
u.UNIT_CODE AS unit_code,
u.UNIT_NAME AS UNIT_NAME
FROM
fnd_employee_assigns t1,fnd_employee e, fnd_company c , fnd_org_position p , fnd_org_unit u
WHERE
e.EMPLOYEE_ID = t1.EMPLOYEE_ID
AND c.company_id = t1.COMPANY_ID
AND p.position_id = t1.POSITION_ID
AND u.UNIT_ID = p.unit_id
)
</sql>
<sql id="queryFndOrgUnitMgrDetail">
SELECT
u.UNIT_ID AS unit_id,
u.UNIT_CODE AS unit_code,
u.UNIT_NAME AS unit_name,
u.CHIEF_POSITION_ID AS mgr_position_id,
mgr_a.position_code AS mgr_position_code,
mgr_a.POSITION_NAME AS mgr_position_name,
mgr_a.employee_id AS mgr_employee_id,
mgr_a.EMPLOYEE_CODE AS mgr_employee_code,
mgr_a.employee_name AS mgr_employee_name,
u.PARENT_UNIT_ID AS parent_unit_id,
u.COMPANY_ID AS company_id,
u.ENABLED_FLAG AS unit_enabled,
mgr_a.position_enabled AS mgr_position_enabled,
mgr_a.enabled_flag AS mgr_assign_enabled
FROM
( fnd_org_unit u JOIN
<include refid="hls.core.sys.mapper.SysCodeValueMapper.queryFndEmployeeAssignsDetail"/>
mgr_a )
WHERE
( mgr_a.position_id = u.CHIEF_POSITION_ID )
</sql>
<sql id="queryHlsBpMasterDetail">
SELECT
hbm.BP_ID AS BP_ID,
hbm.OWNER_USER_ID AS OWNER_USER_ID,
hbm.BP_CODE AS BP_CODE,
hbm.BP_NAME AS BP_NAME,
hbm.EXTRA_NAM AS EXTRA_NAM,
hbm.BP_CLASS AS BP_CLASS,
hbm.BP_CATEGORY AS BP_CATEGORY,
hbm.BP_TYPE AS BP_TYPE,
hbm.BP_TITLE AS BP_TITLE,
hbm.SEARCH_TERM_1 AS SEARCH_TERM_1,
hbm.SEARCH_TERM_2 AS SEARCH_TERM_2,
hbm.EXTERNAL_BP_CODE AS EXTERNAL_BP_CODE,
hbm.ADDRESS_ID AS ADDRESS_ID,
hbm.ENABLED_FLAG AS ENABLED_FLAG,
hbm.FIRST_NAME AS FIRST_NAME,
hbm.MIDDLE_NAME AS MIDDLE_NAME,
hbm.LAST_NAME AS LAST_NAME,
hbm.GENDER AS GENDER,
hbm.NATIONALITY AS NATIONALITY,
hbm.DATE_OF_BIRTH AS DATE_OF_BIRTH,
hbm.PLACE_OF_BIRTH AS PLACE_OF_BIRTH,
hbm.NAME_AT_BIRTH AS NAME_AT_BIRTH,
hbm.MARITAL_STATUS AS MARITAL_STATUS,
hbm.NUMBER_OF_CHILDREN AS NUMBER_OF_CHILDREN,
hbm.ACADEMIC_BACKGROUND AS ACADEMIC_BACKGROUND,
hbm.AGE AS AGE,
hbm.ID_TYPE AS ID_TYPE,
hbm.ID_CARD_NO AS ID_CARD_NO,
hbm.ANNUAL_INCOME AS ANNUAL_INCOME,
hbm.CURRENCY AS CURRENCY,
hbm.CAPITAL_OF_FAMILY AS CAPITAL_OF_FAMILY,
hbm.LIABILITY_OF_FAMILY AS LIABILITY_OF_FAMILY,
hbm.LEGAL_FORM AS LEGAL_FORM,
hbm.INDUSTRY AS INDUSTRY,
hbm.BUSINESS_LICENSE_NUM AS BUSINESS_LICENSE_NUM,
hbm.CORPORATE_CODE AS CORPORATE_CODE,
hbm.ORGANIZATION_CODE AS ORGANIZATION_CODE,
hbm.TAX_REGISTRY_NUM AS TAX_REGISTRY_NUM,
hbm.REGISTERED_PLACE AS REGISTERED_PLACE,
hbm.FOUNDED_DATE AS FOUNDED_DATE,
hbm.REGISTERED_CAPITAL AS REGISTERED_CAPITAL,
hbm.BALANCE_SHEET_CURRENCY AS BALANCE_SHEET_CURRENCY,
hbm.TAXPAYER_TYPE AS TAXPAYER_TYPE,
hbm.INVOICE_TITLE AS INVOICE_TITLE,
hbm.INVOICE_BP_ADDRESS_PHONE_NUM AS INVOICE_BP_ADDRESS_PHONE_NUM,
hbm.INVOICE_BP_BANK_ACCOUNT AS INVOICE_BP_BANK_ACCOUNT,
hbm.LOAN_CARD_NUM AS LOAN_CARD_NUM,
hbm.PAID_UP_CAPITAL AS PAID_UP_CAPITAL,
hbm.COMPANY_NATURE AS COMPANY_NATURE,
hbm.PRIMARY_BUSINESS AS PRIMARY_BUSINESS,
hbm.MAIN_PRODUCTS AS MAIN_PRODUCTS,
hbm.BP_NAME_SP AS BP_NAME_SP,
hbm.GENDER_SP AS GENDER_SP,
hbm.DATE_OF_BIRTH_SP AS DATE_OF_BIRTH_SP,
hbm.ACADEMIC_BACKGROUND_SP AS ACADEMIC_BACKGROUND_SP,
hbm.ID_TYPE_SP AS ID_TYPE_SP,
hbm.ID_CARD_NO_SP AS ID_CARD_NO_SP,
hbm.COUNTRY_SP AS COUNTRY_SP,
hbm.PROVINCE_SP AS PROVINCE_SP,
hbm.CITY_SP AS CITY_SP,
hbm.DISTRICT_SP AS DISTRICT_SP,
hbm.ADDRESS_SP AS ADDRESS_SP,
hbm.CREATED_BY AS CREATED_BY,
hbm.CREATION_DATE AS CREATION_DATE,
hbm.LAST_UPDATED_BY AS LAST_UPDATED_BY,
hbm.LAST_UPDATETIME_DATE AS LAST_UPDATETIME_DATE,
hbm.REF_V01 AS REF_V01,
hbm.REF_V02 AS REF_V02,
hbm.REF_V03 AS REF_V03,
hbm.REF_V04 AS REF_V04,
hbm.REF_V05 AS REF_V05,
hbm.REF_V06 AS REF_V06,
hbm.REF_V07 AS REF_V07,
hbm.REF_V08 AS REF_V08,
hbm.REF_V09 AS REF_V09,
hbm.REF_V10 AS REF_V10,
hbm.REF_V11 AS REF_V11,
hbm.REF_V12 AS REF_V12,
hbm.REF_V13 AS REF_V13,
hbm.REF_V14 AS REF_V14,
hbm.REF_V15 AS REF_V15,
hbm.REF_N01 AS REF_N01,
hbm.REF_N02 AS REF_N02,
hbm.REF_N03 AS REF_N03,
hbm.REF_N04 AS REF_N04,
hbm.REF_N05 AS REF_N05,
hbm.REF_N06 AS REF_N06,
hbm.REF_N07 AS REF_N07,
hbm.REF_N08 AS REF_N08,
hbm.REF_N09 AS REF_N09,
hbm.REF_N10 AS REF_N10,
hbm.REF_D01 AS REF_D01,
hbm.REF_D02 AS REF_D02,
hbm.REF_D03 AS REF_D03,
hbm.REF_D04 AS REF_D04,
hbm.REF_D05 AS REF_D05,
hbm.ENTERPRISE_SCALE AS ENTERPRISE_SCALE,
hbm.LEGAL_PERSON AS LEGAL_PERSON,
hbm.SPOUSE_PHONE AS SPOUSE_PHONE,
hbm.LICENSE_TERMS AS LICENSE_TERMS,
hbm.PORPORTION_OF_GUARANTEE AS PORPORTION_OF_GUARANTEE,
hbm.PERIOD_IN_JOB AS PERIOD_IN_JOB,
hbm.NET_MONTHLY_INCOME AS NET_MONTHLY_INCOME,
hbm.LEADER_FLAG AS LEADER_FLAG,
hbm.MORTGAGE_FLAG AS MORTGAGE_FLAG,
hbm.LOCAL_PERSON_FLAG AS LOCAL_PERSON_FLAG,
hbm.HIGH_MENTAL_FLAG AS HIGH_MENTAL_FLAG,
hbm.HAS_HOUSE_FLAG AS HAS_HOUSE_FLAG,
hbm.HAS_CAR_FLAG AS HAS_CAR_FLAG,
hbm.COMMUNITY_LEADER_FLAG AS COMMUNITY_LEADER_FLAG,
hbm.BANK_MATCH_FLAG AS BANK_MATCH_FLAG,
hbm.HOUSE_PROPERTY_VALUE AS HOUSE_PROPERTY_VALUE,
hbm.HOUSE_LOAN_BALANCE AS HOUSE_LOAN_BALANCE,
hbm.DEPOSIT_CERTIFICATE AS DEPOSIT_CERTIFICATE,
hbm.CREDIT_CARD_LIMIT AS CREDIT_CARD_LIMIT,
hbm.ADDRESS_ON_ID AS ADDRESS_ON_ID,
hbm.ADDRESS_ON_RESIDENT_BOOKLIT AS ADDRESS_ON_RESIDENT_BOOKLIT,
hbm.LIVING_ADDRESS AS LIVING_ADDRESS,
hbm.WORKING_PLACE AS WORKING_PLACE,
hbm.WORKING_DURATION AS WORKING_DURATION,
hbm.WORKING_ADDRESS AS WORKING_ADDRESS,
hbm.OPERATION_YEAR AS OPERATION_YEAR,
hbm.POSITION AS POSITION,
hbm.CELL_PHONE AS CELL_PHONE,
hbm.PHONE_EXTRA AS PHONE_EXTRA,
hbm.PHONE AS PHONE,
hbm.CONTACT_PERSON AS CONTACT_PERSON,
hbm.FAX_NUMBER AS FAX_NUMBER,
hbm.DEPARTMENT AS DEPARTMENT,
hbm.CREDIT_FLAG AS CREDIT_FLAG,
hbm.CREDIT_AMOUNT AS CREDIT_AMOUNT,
hbm.CREDIT_ALT AS CREDIT_ALT,
hbm.CREDIT_FORBID AS CREDIT_FORBID,
hbm.EMAIL AS EMAIL,
hbm.NC_STATUS AS NC_STATUS,
hbm.CELL_PHONE_2 AS CELL_PHONE_2,
hbm.EMPLOYEE_AMOUNT AS EMPLOYEE_AMOUNT,
hbm.BILLING_STATUS AS BILLING_STATUS,
hbm.FIN_NET_CASH_INFLOW AS FIN_NET_CASH_INFLOW,
hbm.FIN_MONTHLY_PAYMENT AS FIN_MONTHLY_PAYMENT,
hbm.FIN_MONTHS AS FIN_MONTHS,
hbm.FIN_LIQUIDITY_RATIO AS FIN_LIQUIDITY_RATIO,
hbm.FIN_LEVERAGE AS FIN_LEVERAGE,
hbm.FIN_DATA AS FIN_DATA,
hbm.FIN_EVALUATION AS FIN_EVALUATION,
hbm.CDD_LIST_ID AS CDD_LIST_ID,
hbm.PB_NUMBER AS PB_NUMBER,
hbm.REPO_NUMBER AS REPO_NUMBER,
hbm.FIN_NOTE1 AS FIN_NOTE1,
hbm.FIN_NOTE AS FIN_NOTE,
hbm.BLACK_FLAG AS BLACK_FLAG,
hbm.LOCK_FLAG AS LOCK_FLAG,
hbm.OLD_FLAG AS OLD_FLAG,
hbm.LIMIT_FROM AS LIMIT_FROM,
hbm.LIMIT_TO AS LIMIT_TO,
hbm.LAW_BP_FLAG AS LAW_BP_FLAG,
hbm.FIN_TURNOVER_1 AS FIN_TURNOVER_1,
hbm.FIN_TURNOVER AS FIN_TURNOVER,
hbm.IF_TO_ZX_FLAG AS IF_TO_ZX_FLAG,
hbm.ZX_LAST_UPDATE_DATE AS ZX_LAST_UPDATE_DATE,
hbm.ZX_LAST_UPDATED_BY AS ZX_LAST_UPDATED_BY,
hbm.CREDIT_AMOUNT_USED_FIX AS CREDIT_AMOUNT_USED_FIX,
hbm.CREDIT_AMOUNT_USED_CYCLE AS CREDIT_AMOUNT_USED_CYCLE,
hbm.CREDIT_AMOUNT_FROZEN_FIX AS CREDIT_AMOUNT_FROZEN_FIX,
hbm.CREDIT_AMOUNT_FROZEN_CYCLE AS CREDIT_AMOUNT_FROZEN_CYCLE,
hbm.POLLING_TIMES AS POLLING_TIMES,
hbm.SALE_TYPE AS SALE_TYPE,
hbm.ASSET_TYPE AS ASSET_TYPE,
hbm.AREA AS AREA,
hbm.LISTED_COMPANY_FLAG AS LISTED_COMPANY_FLAG,
hbm.MARKET_LOCATION AS MARKET_LOCATION,
hbm.CREDIT_RATING AS CREDIT_RATING,
hbm.RATING_DATE AS RATING_DATE,
hbm.INVOICE_KIND AS INVOICE_KIND,
hbm.LICENSE_TERMS_IF_LONG AS LICENSE_TERMS_IF_LONG,
hbm.LISTED_SUBJECT AS LISTED_SUBJECT,
hbm.LISTED_SUBJECT_ORG_CODE AS LISTED_SUBJECT_ORG_CODE,
hbm.ACTUAL_CONTROLLER_ID_TYPE AS ACTUAL_CONTROLLER_ID_TYPE,
hbm.ACTUAL_CONTROLLER_ID AS ACTUAL_CONTROLLER_ID,
hbm.ACTUAL_CONTROLLER_ORG_CODE AS ACTUAL_CONTROLLER_ORG_CODE,
hbm.BP_NAME_SP_CLASS AS BP_NAME_SP_CLASS
FROM
hls_bp_master hbm
</sql>
<sql id="sys_function_vl">
SELECT
t1.FUNCTION_ID AS function_id,
t1.FUNCTION_CODE AS function_code,
t1.FUNCTION_NAME_ID AS function_name_id,
t1.FUNCTION_NAME AS function_name,
t1.FUNCTION_TYPE AS function_type,
t1.PARENT_FUNCTION_ID AS parent_function_id,
t1.MODULE_CODE AS module_CODE,
t1.ICON AS icon,
t1.SEQUENCE AS sequence,
t1.SERVICE_ID AS service_id,
t1.CREATION_DATE AS creation_date,
t1.CREATED_BY AS created_by,
t1.LAST_UPDATE_DATE AS last_update_date,
t1.LAST_UPDATED_BY AS last_updated_by
FROM
sys_function_b t1
</sql>
<sql id="sys_role_function_item_v">
SELECT
e.role_function_group_id AS role_function_group_id,
e.role_id AS role_id,
e.function_group_id AS function_group_id,
r.function_id AS function_id,
f.function_code AS function_code,
f.function_name AS function_name,
f.service_id AS main_service_id,
(
SELECT
s.service_name
FROM
sys_service s
WHERE
(
s.service_id = f.service_id
)
) AS main_service_name,
r.layout_sequence AS layout_sequence,
r.enabled_flag AS enabled_flag,
e.parent_role_function_group_id AS parent_role_function_group_id,
r.creation_date AS creation_date,
r.created_by AS created_by,
r.last_update_date AS last_update_date,
r.last_updated_by AS last_updated_by
FROM
(
(
sys_role_function_group e
JOIN sys_function_group_item r
)
JOIN <include refid="hls.core.sys.mapper.SysCodeValueMapper.sys_function_vl"/> f
)
WHERE
(
(
e.function_group_id = r.function_group_id
)
AND (
r.function_id = f.function_id
)
AND (
NOT (
EXISTS (
SELECT
1
FROM
sys_role_function_exclude_item i
WHERE
(
(
i.role_function_group_id = e.role_function_group_id
)
AND (
i.function_id = r.function_id
)
)
)
)
)
AND (r.enabled_flag = 'Y')
)
</sql>
<sql id="sys_service_lookup_v">
SELECT
a.code_id AS code_id,
a.code AS code,
v.code_value_id AS code_value_id,
v.code_value AS code_value,
v.code_value_name_id AS code_value_name_id,
b.description_text AS code_value_name,
b.language AS language,
v.enabled_flag AS enabled_flag,
v.order_seq AS order_seq
FROM
sys_codes a,
sys_code_values v
LEFT JOIN fnd_descriptions b ON (
(
v.code_value_name_id = b.description_id
)
)
WHERE
(
(
a.code_id = v.code_id
)
AND (
b.ref_table = 'SYS_CODE_VALUES'
)
AND (
b.ref_field = 'CODE_VALUE_NAME_ID'
)
)
ORDER BY
v.order_seq
</sql>
</mapper>
\ No newline at end of file
package com.hand.hls.dp.controllers;
import com.hand.hap.core.IRequest;
import com.hand.hap.core.impl.RequestHelper;
import com.hand.hap.system.controllers.BaseController;
import com.hand.hap.system.dto.ResponseData;
import com.hand.hls.dp.service.SysDpDownloadLogService;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
import com.hand.hls.dp.util.IPUtils;
import leaf.bean.LeafRequestData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 15:31
* @since
*/
@RestController
public class SysDpExecuteHistoryController extends BaseController {
@Autowired
private SysDpExecuteHistoryService service;
@Autowired
private SysDpDownloadLogService downloadLogService;
@PostMapping(value = "/sys/dp/query")
public ResponseData query(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, @RequestParam("ck") int ck,
HttpServletRequest request, HttpServletResponse response) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
if (ck == 0){
service.query4ck(requestContext, ip, dpContext);
}else if(ck == 1){
request.setAttribute("_NO_PAGE_","Y");
final String context = new String(dpContext.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
service.query(requestContext, response, ip, context);
}
return new ResponseData();
}
@PostMapping(value = "/sys/dp/insert")
public ResponseData insert(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
return service.insert(requestContext, ip, dpContext);
}
@PostMapping(value = "/sys/dp/update")
public ResponseData update(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
return service.update(requestContext, ip, dpContext);
}
@PostMapping(value = "/sys/dp/delete")
public ResponseData delete(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
return service.delete(requestContext, ip, dpContext);
}
@PostMapping(value = "/sys/dp/download/log")
public void downloadLog(HttpServletRequest request, HttpServletResponse response) throws Exception{
IRequest requestContext = createRequestContext(request);
downloadLogService.download(requestContext, request, response);
}
@PostMapping(value = "/sys/dp/ddl")
public void ddl(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData, HttpServletRequest request) throws Exception{
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
service.ddl(requestContext, ip, dpContext);
}
}
package com.hand.hls.dp.controllers.v2;
import com.hand.hap.core.IRequest;
import com.hand.hap.core.impl.RequestHelper;
import com.hand.hap.system.controllers.BaseController;
import com.hand.hap.system.dto.ResponseData;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
import com.hand.hls.dp.util.IPUtils;
import leaf.bean.LeafRequestData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 15:31
* @since
*/
@RestController
public class SysDpExecuteHistoryV2Controller extends BaseController {
@Autowired
private SysDpExecuteHistoryService service;
@PostMapping(value = "/sys/dp/v2/execute")
public ResponseData query(@ModelAttribute(LEAF_PARAM_NAME) LeafRequestData requestData,
HttpServletRequest request) throws Exception {
IRequest requestContext = createRequestContext(request);
RequestHelper.setCurrentRequest(requestContext);
final String ip = IPUtils.getIpAddress(request);
final String dpContext = (String) requestData.get("parameter");
return service.execute(requestContext, ip, dpContext);
}
}
package com.hand.hls.dp.dto;
import com.hand.hap.mybatis.annotation.ExtensionAttribute;
import com.hand.hap.system.dto.BaseDTO;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:01
* @since
*/
@ExtensionAttribute(disable = true)
@Table(name = "sys_dp_execute_history")
public class SysDpExecuteHistory extends BaseDTO {
@Id
@GeneratedValue
private Long dehId;
private Long dehUserId;
private String dehIp;
private Date dehExecuteTime;
private String dehExecuteType;
private Integer dehRowCount;
private String dehContext;
private String dehStatus;
private String dehError;
private String dehBakData;
public Long getDehId() {
return dehId;
}
public void setDehId(Long dehId) {
this.dehId = dehId;
}
public Long getDehUserId() {
return dehUserId;
}
public void setDehUserId(Long dehUserId) {
this.dehUserId = dehUserId;
}
public String getDehIp() {
return dehIp;
}
public void setDehIp(String dehIp) {
this.dehIp = dehIp;
}
public Date getDehExecuteTime() {
return dehExecuteTime;
}
public void setDehExecuteTime(Date dehExecuteTime) {
this.dehExecuteTime = dehExecuteTime;
}
public String getDehExecuteType() {
return dehExecuteType;
}
public void setDehExecuteType(String dehExecuteType) {
this.dehExecuteType = dehExecuteType;
}
public Integer getDehRowCount() {
return dehRowCount;
}
public void setDehRowCount(Integer dehRowCount) {
this.dehRowCount = dehRowCount;
}
public String getDehContext() {
return dehContext;
}
public void setDehContext(String dehContext) {
this.dehContext = dehContext;
}
public String getDehStatus() {
return dehStatus;
}
public void setDehStatus(String dehStatus) {
this.dehStatus = dehStatus;
}
public String getDehError() {
return dehError;
}
public void setDehError(String dehError) {
this.dehError = dehError;
}
public String getDehBakData() {
return dehBakData;
}
public void setDehBakData(String dehBakData) {
this.dehBakData = dehBakData;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SysDpExecuteHistory that = (SysDpExecuteHistory) o;
return Objects.equals(dehId, that.dehId) &&
Objects.equals(dehUserId, that.dehUserId) &&
Objects.equals(dehIp, that.dehIp) &&
Objects.equals(dehExecuteTime, that.dehExecuteTime) &&
Objects.equals(dehExecuteType, that.dehExecuteType) &&
Objects.equals(dehRowCount, that.dehRowCount) &&
Objects.equals(dehContext, that.dehContext) &&
Objects.equals(dehStatus, that.dehStatus) &&
Objects.equals(dehError, that.dehError) &&
Objects.equals(dehBakData, that.dehBakData);
}
@Override
public int hashCode() {
return Objects.hash(dehId, dehUserId, dehIp, dehExecuteTime, dehExecuteType, dehRowCount, dehContext, dehStatus, dehError, dehBakData);
}
@Override
public String toString() {
return "SysDpExecuteHistory{" +
"dehId=" + dehId +
", dehUserId=" + dehUserId +
", dehIp='" + dehIp + '\'' +
", dehExecuteTime=" + dehExecuteTime +
", dehExecuteType='" + dehExecuteType + '\'' +
", dehRowCount=" + dehRowCount +
", dehContext='" + dehContext + '\'' +
", dehStatus='" + dehStatus + '\'' +
", dehError='" + dehError + '\'' +
", dehBakData='" + dehBakData + '\'' +
'}';
}
}
package com.hand.hls.dp.mapper;
import com.hand.hap.mybatis.common.Mapper;
import com.hand.hls.dp.dto.SysDpExecuteHistory;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/5/12 19:17
* @since
*/
public interface SysDpExecuteHistoryMapper extends Mapper<SysDpExecuteHistory> {
}
package com.hand.hls.dp.service;
import com.hand.hap.core.IRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/20 10:02
* @since
*/
public interface SysDpDownloadLogService {
void download(IRequest requestContext, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
package com.hand.hls.dp.service;
import com.hand.hap.core.IRequest;
import com.hand.hap.core.ProxySelf;
import com.hand.hap.system.dto.ResponseData;
import com.hand.hap.system.service.IBaseService;
import com.hand.hls.dp.dto.SysDpExecuteHistory;
import javax.servlet.http.HttpServletResponse;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 15:57
* @since
*/
public interface SysDpExecuteHistoryService extends IBaseService<SysDpExecuteHistory>, ProxySelf<SysDpExecuteHistoryService> {
void query4ck(IRequest requestContext, final String ip, final String dpContext) throws Exception;
void query(IRequest requestContext, HttpServletResponse response, final String ip, final String dpContext) throws Exception;
ResponseData insert(IRequest requestContext, final String ip, final String dpContext);
ResponseData update(IRequest requestContext, final String ip, final String dpContext);
ResponseData delete(IRequest requestContext, final String ip, final String dpContext);
ResponseData ddl(IRequest requestContext, final String ip, final String dpContext);
void insertSysDpExecuteHistory(IRequest requestContext, SysDpExecuteHistory sysDpExecuteHistory);
ResponseData execute(IRequest requestContext, final String ip, final String dpContext) throws Exception;
}
package com.hand.hls.dp.service.impl;
import cn.hutool.core.date.DateUtil;
import com.hand.hap.core.IRequest;
import com.hand.hls.dp.service.SysDpDownloadLogService;
import com.hand.hls.dp.util.LogUtils;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/20 10:03
* @since
*/
@Service
public class SysDpDownloadLogServiceImpl implements SysDpDownloadLogService {
@Value("${log-path}")
private String logPath;
@Override
public void download(IRequest requestContext, HttpServletRequest request, HttpServletResponse response) throws Exception {
String zipFileName = DateUtil.format(new Date(),"yyyyMMddHHmmss") + "_logs.zip";
response.reset();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache,must-revalidate");
response.setHeader("Expires","0");
response.setHeader("Content-Disposition","attachment;filename=" + new String(zipFileName.getBytes(), StandardCharsets.ISO_8859_1));
response.setContentType("application/x-msdownload;");
ByteArrayOutputStream byteOutPut = new ByteArrayOutputStream();
CheckedOutputStream cos = new CheckedOutputStream(byteOutPut,new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
// 下载7天内的日志
String[] downloadFiles = new String[7];
int length = downloadFiles.length;
// 当前日志
downloadFiles[0] = logPath + "leaf.log";
for (int i = 1; i <= length - 1;i++){
LocalDate tmp = LocalDate.now().minusDays(i);
Date date = Date.from(tmp.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
downloadFiles[i] = logPath + "leaf-" + DateUtil.format(new Date(),"yyyy-MM-dd") + ".log";
}
// 查找日志目录下所有文件
List<String> files = new ArrayList<>(30);
LogUtils.findFiles(new File(logPath),files);
files.forEach(file -> {
for (int i = 0; i < length; i++) {
if (file.equals(downloadFiles[i])) {
// 创建ZipEntry
ZipEntry entry = new ZipEntry(file.substring(logPath.length()));
// 放入文件,防止文件名相同的附件报错
try {
zos.putNextEntry(entry);
} catch (Exception e) {
// ignore
}
// 写入文件
LogUtils.readFileToZip(file,zos);
}
}
});
zos.closeEntry();
zos.close();
response.setHeader("Content-Length", String.valueOf(byteOutPut.size()));
IOUtils.copy(new ByteArrayInputStream(byteOutPut.toByteArray()), response.getOutputStream());
cos.close();
}
}
package com.hand.hls.dp.service.impl;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.dto.SysDpExecuteHistory;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
import com.hand.hls.dp.util.SqlConstantUtils;
import com.hand.hls.dp.util.SqlParserCheckUtils;
import com.hand.hls.dp.util.SqlCheckUtils;
import com.hand.hls.exception.HlsCusException;
import hls.core.sys.mapper.SysCodeValueMapper;
import leaf.plugin.export.components.ExcelExportUtil;
import leaf.utils.ConfigUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
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.locks.ReentrantLock;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:07
* @since
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDpExecuteHistoryServiceImpl extends BaseServiceImpl<SysDpExecuteHistory> implements SysDpExecuteHistoryService {
private static final Logger logger = LoggerFactory.getLogger(SysDpExecuteHistoryServiceImpl.class);
/**
* Default initial capacity.
*/
private static final int DEFAULT_INITIAL_CAPACITY = 16;
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Autowired
private SysCodeValueMapper sysCodeValueMapper;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
/**
* 查询结果导出excel之前先检验sql是否存在问题?
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
*/
@Override
public void query4ck(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute query4ck");
logger.info("ip:[{}] ==============> 检验查询待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
String formatSql = dpContext;
if (ConfigUtils.isOracle()) {
try {
formatSql = SQLUtils.format(formatSql, JdbcConstants.ORACLE);
} catch (ParserException ex) {
msg.append("语句不正确!错误信息:").append(ex.getMessage());
}
} else if (ConfigUtils.isMySQL()) {
try {
formatSql = SQLUtils.format(formatSql, JdbcConstants.MYSQL);
} catch (ParserException ex) {
msg.append("语句不正确!错误信息:").append(ex.getMessage());
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
throw new IllegalArgumentException(msg.toString());
}
String checkMsg = SqlParserCheckUtils.parserCheckSelect(formatSql);
msg.append(checkMsg);
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlCheckUtils.checkSqlIgnoreCase(formatSql, SqlConstantUtils.SQL_STAR)) {
msg.append("select语句不允许使用*查询所有字段,请重置语句!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_SELECT);
sysDpExecuteHistory.setDehContext(formatSql);
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
sysDpExecuteHistory.setDehError(msg.toString());
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("ip:[{}] ==============> 验证不通过:[{}]", ip, msg.toString());
}
throw new IllegalArgumentException(msg.toString());
}
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = DBUtil.getConnectionBySqlSession(sqlSession);
PreparedStatement preparedStatement = conn.prepareStatement(formatSql);
ResultSet rs = preparedStatement.executeQuery()) {
if (!rs.next()) {
throw new IllegalArgumentException("未查询到任何数据!");
}
} catch (Exception e) {
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_SELECT);
sysDpExecuteHistory.setDehContext(formatSql);
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============> 出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(msg.toString());
}
}
if (logger.isInfoEnabled()) {
logger.info("<============== end execute query4ck");
}
}
/**
* 查询结果导出excel
*
* @param requestContext 请求上下文
* @param response 返回
* @param ip 请求ip
* @param dpContext 待执行内容
*/
@Override
public void query(IRequest requestContext, HttpServletResponse response, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute query");
logger.info("ip:[{}] ==============> 查询待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_SELECT);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
JSONArray coumnConfig = new JSONArray(DEFAULT_INITIAL_CAPACITY);
List<Map<String, Object>> retList = new ArrayList<>(DEFAULT_INITIAL_CAPACITY);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
String formatSql = dpContext;
if (ConfigUtils.isOracle()) {
try {
formatSql = SQLUtils.format(formatSql, JdbcConstants.ORACLE);
} catch (ParserException ex) {
msg.append("语句不正确!错误信息:").append(ex.getMessage());
}
} else if (ConfigUtils.isMySQL()) {
try {
formatSql = SQLUtils.format(formatSql, JdbcConstants.MYSQL);
} catch (ParserException ex) {
msg.append("语句不正确!错误信息:").append(ex.getMessage());
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
throw new IllegalArgumentException(msg.toString());
}
String checkMsg = SqlParserCheckUtils.parserCheckSelect(formatSql);
msg.append(checkMsg);
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlCheckUtils.checkSqlIgnoreCase(formatSql, SqlConstantUtils.SQL_STAR)) {
msg.append("select语句不允许使用*查询所有字段,请重置语句!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(msg.toString());
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
throw new IllegalArgumentException(msg.toString());
}
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = DBUtil.getConnectionBySqlSession(sqlSession);
PreparedStatement preparedStatement = conn.prepareStatement(formatSql);
ResultSet rs = preparedStatement.executeQuery()) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
ResultSetMetaData md = rs.getMetaData();
int columCount = md.getColumnCount();
// 组装excel列信息
for (int i = 1; i <= columCount; i++) {
String columnName = md.getColumnName(i);
Map<String, Object> columnInfo = new HashMap<>(4);
columnInfo.put("width", 150);
columnInfo.put("name", columnName);
columnInfo.put("align", "left");
columnInfo.put("prompt", columnName);
JSONObject columnJsonObject = new JSONObject(columnInfo);
coumnConfig.add(columnJsonObject);
}
while (rs.next()) {
Map<String, Object> rowData = new HashMap<>(1);
try {
getType(rs, md, rowData);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage());
}
}
retList.add(rowData);
}
sysDpExecuteHistory.setDehRowCount(retList.size());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (Exception e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
// 查询结果导出excel
ExcelExportUtil excelExportUtil = new ExcelExportUtil();
excelExportUtil.createExcel(response, coumnConfig, retList);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute query");
}
}
/**
* 新增
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData insert(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute insert");
logger.info("ip:[{}] ==============> 新增待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_INSERT);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
conn.setAutoCommit(false);
SysDpExecuteHistory insertDpExecuteHistory = new SysDpExecuteHistory();
insertDpExecuteHistory.setDehIp(ip);
insertDpExecuteHistory.setDehUserId(requestContext.getUserId());
insertDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_INSERT);
insertDpExecuteHistory.setDehContext(dpContext);
insertDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlCheckUtils.checkSqlFilterTableIgnoreCase(dpContext, SqlConstantUtils.SQL_FILTER_TABLE)) {
msg.append("当前网络环境不允许新增数据!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
insertDpExecuteHistory.setDehExecuteTime(new Date());
insertDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, insertDpExecuteHistory);
responseData.setMessage("错误消息:" + msg.toString());
return responseData;
}
try (PreparedStatement preparedStatement = conn.prepareStatement(dpContext)) {
int rowCount = preparedStatement.executeUpdate();
insertDpExecuteHistory.setDehExecuteTime(new Date());
if (logger.isInfoEnabled()) {
logger.info("本次插入[{}]条数据。", rowCount);
}
responseData.setSuccess(rowCount > 0);
responseData.setMessage("执行成功,本次插入" + rowCount + "条数据。");
insertDpExecuteHistory.setDehRowCount(rowCount);
insertDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
self().insertSysDpExecuteHistory(requestContext, insertDpExecuteHistory);
conn.commit();
} catch (SQLException e) {
insertDpExecuteHistory.setDehExecuteTime(new Date());
insertDpExecuteHistory.setDehError(e.getMessage());
insertDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, insertDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============> 执行语句:[{}];出错:[{}]", ip, dpContext, e.getMessage());
}
return responseData;
}
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (SQLException e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
responseData.setMessage(e.getMessage());
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============> 出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute insert");
}
return responseData;
}
/**
* 更新
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData update(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute update");
logger.info("ip:[{}] ==============> 更新待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_UPDATE);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
conn.setAutoCommit(false);
SysDpExecuteHistory updateDpExecuteHistory = new SysDpExecuteHistory();
updateDpExecuteHistory.setDehIp(ip);
updateDpExecuteHistory.setDehUserId(requestContext.getUserId());
updateDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_UPDATE);
updateDpExecuteHistory.setDehContext(dpContext);
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlCheckUtils.checkSqlFilterTableIgnoreCase(dpContext, SqlConstantUtils.SQL_FILTER_TABLE)) {
msg.append("当前网络环境不允许修改数据!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
updateDpExecuteHistory.setDehExecuteTime(new Date());
updateDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, updateDpExecuteHistory);
responseData.setMessage("错误消息:" + msg.toString());
return responseData;
}
String sql = SqlConstantUtils.SELECT_COUNT;
// 默认截取update之后语句
if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_SET)) {
sql = sql + StringUtils.substring(dpContext, 6, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_SET));
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_SET_UPPER)) {
sql = sql + StringUtils.substring(dpContext, 6, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_SET_UPPER));
}
if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_WHERE)) {
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_WHERE));
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_WHERE_UPPER)) {
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_WHERE_UPPER));
}
// 先查询受where条件影响数据
int rowCount = 0;
try (PreparedStatement preparedStatementQuery = conn.prepareStatement(sql);
ResultSet rs = preparedStatementQuery.executeQuery()) {
while (rs.next()) {
rowCount = rs.getInt("TOTALCOUNT");
}
if (logger.isInfoEnabled()) {
logger.info("==============> 执行sql[{}],本次更新条件查询:[{}]条数据。", sql, rowCount);
}
} catch (SQLException e) {
updateDpExecuteHistory.setDehExecuteTime(new Date());
updateDpExecuteHistory.setDehContext(sql);
updateDpExecuteHistory.setDehError(e.getMessage());
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============> 执行语句:[{}];出错:[{}]", ip, sql, e.getMessage());
}
return responseData;
}
// 备份数据
String bakSql = SqlConstantUtils.SELECT_FROM;
// 默认截取update之后语句
if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_SET)) {
bakSql = bakSql + StringUtils.substring(dpContext, 6, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_SET));
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_SET_UPPER)) {
bakSql = bakSql + StringUtils.substring(dpContext, 6, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_SET_UPPER));
}
if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_WHERE)) {
bakSql = bakSql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_WHERE));
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_WHERE_UPPER)) {
bakSql = bakSql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_WHERE_UPPER));
}
String bakData = bakData(conn, bakSql);
// 执行传入语句
try (PreparedStatement preparedStatement = conn.prepareStatement(dpContext)) {
int updateRowCount = preparedStatement.executeUpdate();
if (logger.isInfoEnabled()) {
logger.info("==============> 执行sql[{}],本次更新:[{}]条数据。", dpContext, updateRowCount);
}
updateDpExecuteHistory.setDehExecuteTime(new Date());
// 查询结果==更新结果
if (updateRowCount == rowCount) {
conn.commit();
if (logger.isInfoEnabled()) {
logger.info("本次更新[{}]条数据。", updateRowCount);
}
responseData.setSuccess(updateRowCount > 0);
responseData.setMessage("执行成功,本次更新" + updateRowCount + "条数据。");
responseData.setTotal((long) updateRowCount);
updateDpExecuteHistory.setDehRowCount(updateRowCount);
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
updateDpExecuteHistory.setDehBakData(bakData);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
} else {
conn.rollback();
updateDpExecuteHistory.setDehError("更新条目与查询条目不一致!本次更新前条件查询" + rowCount + "条数据。本次更新" + updateRowCount + "条数据。");
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息::更新条目与查询条目不一致!本次更新前条件查询" + rowCount + "条数据。本次更新" + updateRowCount + "条数据。");
return responseData;
}
} catch (SQLException e) {
updateDpExecuteHistory.setDehExecuteTime(new Date());
updateDpExecuteHistory.setDehError(e.getMessage());
updateDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, updateDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============> 执行语句:[{}];出错:[{}]", ip, dpContext, e.getMessage());
}
return responseData;
}
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (SQLException e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
responseData.setMessage(e.getMessage());
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============> 出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute update");
}
return responseData;
}
/**
* 删除
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData delete(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute delete");
logger.info("ip:[{}] ==============> 删除待执行语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DELETE);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
conn.setAutoCommit(false);
SysDpExecuteHistory delDpExecuteHistory = new SysDpExecuteHistory();
delDpExecuteHistory.setDehIp(ip);
delDpExecuteHistory.setDehUserId(requestContext.getUserId());
delDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DELETE);
delDpExecuteHistory.setDehContext(dpContext);
delDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
if (SqlCheckUtils.checkSqlFilterTableIgnoreCase(dpContext, SqlConstantUtils.SQL_FILTER_TABLE)) {
msg.append("当前网络环境不允许删除数据!");
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
delDpExecuteHistory.setDehExecuteTime(new Date());
delDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, delDpExecuteHistory);
responseData.setMessage("错误消息:" + msg.toString());
return responseData;
}
String sql = SqlConstantUtils.SELECT_COUNT;
// 默认截取from之后语句
if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_FROM)) {
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM));
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_FROM_UPPER)) {
sql = sql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM_UPPER));
}
// 先查询受where条件影响数据
int rowCount = 0;
try (PreparedStatement preparedStatementQuery = conn.prepareStatement(sql);
ResultSet rs = preparedStatementQuery.executeQuery()) {
while (rs.next()) {
rowCount = rs.getInt("TOTALCOUNT");
}
if (logger.isInfoEnabled()) {
logger.info("==============> 执行sql[{}],本次删除前条件查询:[{}]条数据。", sql, rowCount);
}
} catch (SQLException e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
delDpExecuteHistory.setDehContext(sql);
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============> 执行语句:[{}];出错:[{}]", ip, sql, e.getMessage());
}
return responseData;
}
// 备份数据
String bakSql = "SELECT * ";
// 默认截取from之后语句
if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_FROM)) {
bakSql = bakSql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM));
} else if (StringUtils.contains(dpContext, SqlConstantUtils.SQL_FROM_UPPER)) {
bakSql = bakSql + StringUtils.substring(dpContext, StringUtils.indexOf(dpContext, SqlConstantUtils.SQL_FROM_UPPER));
}
String bakData = bakData(conn, bakSql);
// 执行传入语句
try (PreparedStatement preparedStatement = conn.prepareStatement(dpContext)) {
int deleteRowCount = preparedStatement.executeUpdate();
if (logger.isInfoEnabled()) {
logger.info("==============> 执行sql[{}],本次删除:[{}]条数据。", dpContext, deleteRowCount);
}
delDpExecuteHistory.setDehExecuteTime(new Date());
// 查询结果==删除结果
if (deleteRowCount == rowCount) {
conn.commit();
if (logger.isInfoEnabled()) {
logger.info("本次删除[{}]条数据。", rowCount);
}
responseData.setSuccess(deleteRowCount > 0);
responseData.setMessage("执行成功,本次删除" + deleteRowCount + "条数据。");
responseData.setTotal((long) deleteRowCount);
delDpExecuteHistory.setDehRowCount(deleteRowCount);
delDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
delDpExecuteHistory.setDehBakData(bakData);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
} else {
conn.rollback();
delDpExecuteHistory.setDehError("删除条目与查询条目不一致!本次删除前条件查询" + rowCount + "条数据。本次删除" + deleteRowCount + "条数据。");
delDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:删除条目与查询条目不一致!本次删除前条件查询" + rowCount + "条数据。本次删除" + deleteRowCount + "条数据。");
return responseData;
}
} catch (SQLException e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, delDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============> 执行语句:[{}];出错:[{}]", ip, dpContext, e.getMessage());
}
return responseData;
}
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (Exception e) {
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============> 出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute delete");
}
return responseData;
}
/**
* ddl
*
* @param requestContext 请求上下文
* @param ip 请求ip
* @param dpContext 待执行内容
* @return 返回消息
*/
@Override
public ResponseData ddl(IRequest requestContext, String ip, String dpContext) {
Objects.requireNonNull(dpContext, "传入的语句为空!");
if (logger.isInfoEnabled()) {
logger.info("==============> start execute ddl");
logger.info("ip:[{}] ==============> 待执行DDL语句:[{}]", ip, dpContext);
}
final String ipWhiteCheck = sysCodeValueMapper.queryCodeValueMeaning("IP_ALLOW_LIST", "IP");
ResponseData responseData = new ResponseData(false);
SysDpExecuteHistory sysDpExecuteHistory = new SysDpExecuteHistory();
sysDpExecuteHistory.setDehIp(ip);
sysDpExecuteHistory.setDehUserId(requestContext.getUserId());
sysDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DDL);
sysDpExecuteHistory.setDehContext(dpContext);
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_UNDO);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
// 非内部限制
if (!StringUtils.startsWith(ip, ipWhiteCheck)) {
msg.append("当前网络环境不允许执行语句!");
}
if (StringUtils.isNotEmpty(msg.toString())) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, sysDpExecuteHistory);
responseData.setMessage(msg.toString());
return responseData;
}
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = DBUtil.getConnectionBySqlSession(sqlSession)) {
SysDpExecuteHistory ddlDpExecuteHistory = new SysDpExecuteHistory();
ddlDpExecuteHistory.setDehIp(ip);
ddlDpExecuteHistory.setDehUserId(requestContext.getUserId());
ddlDpExecuteHistory.setDehExecuteType(SqlConstantUtils.SQL_EXEC_DDL);
ddlDpExecuteHistory.setDehContext(dpContext);
if (StringUtils.isNotEmpty(msg.toString())) {
ddlDpExecuteHistory.setDehExecuteTime(new Date());
ddlDpExecuteHistory.setDehError(msg.toString());
self().insertSelective(requestContext, ddlDpExecuteHistory);
responseData.setMessage("错误消息:" + msg.toString());
return responseData;
}
try (PreparedStatement ddl = conn.prepareStatement(dpContext)) {
ddl.executeUpdate();
ddlDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
ddlDpExecuteHistory.setDehExecuteTime(new Date());
self().insertSysDpExecuteHistory(requestContext, ddlDpExecuteHistory);
} catch (SQLException e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, ddlDpExecuteHistory);
responseData.setSuccess(true);
responseData.setMessage("错误消息:" + e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug("ip:[{}] ==============> 执行语句:[{}];出错:[{}]", ip, dpContext, e.getMessage());
}
return responseData;
}
responseData.setSuccess(true);
responseData.setMessage("执行成功!");
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError("已执行成功!");
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DONE);
} catch (Exception e) {
sysDpExecuteHistory.setDehExecuteTime(new Date());
sysDpExecuteHistory.setDehError(e.getMessage());
sysDpExecuteHistory.setDehStatus(SqlConstantUtils.SQL_EXEC_DE);
self().insertSysDpExecuteHistory(requestContext, sysDpExecuteHistory);
if (logger.isWarnEnabled()) {
logger.warn("ip:[{}] ==============> 出错:[{}]", ip, e.getMessage());
}
throw new IllegalArgumentException(e.getMessage());
}
}
self().insertSelective(requestContext, sysDpExecuteHistory);
if (logger.isInfoEnabled()) {
logger.info("<============== end execute ddl");
}
return responseData;
}
/**
* 备份数据
*
* @param conn 链接
* @param bakSql 备份待执行sql
* @return 返回消息
* @throws SQLException sql例外
*/
private String bakData(Connection conn, String bakSql) throws SQLException {
PreparedStatement bakQuery = conn.prepareStatement(bakSql);
ResultSet bakRs = bakQuery.executeQuery();
String bakData = null;
try {
bakData = convertToJson(bakRs);
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("==============> 执行备份语句:[{}];出错:[{}]", bakSql, e.getMessage());
}
}
bakQuery.close();
bakRs.close();
return bakData;
}
/**
* rs结果转换jsoc
*
* @param rs JDBC返回结果集
* @return 返回消息
* @throws Exception 例外
*/
private static String convertToJson(ResultSet rs) throws Exception {
JSONArray jsonArray = new JSONArray(DEFAULT_INITIAL_CAPACITY);
ResultSetMetaData metaData = rs.getMetaData();
while (rs.next()) {
JSONObject jsonObject = new JSONObject(DEFAULT_INITIAL_CAPACITY);
getType(rs, metaData, jsonObject);
jsonArray.add(jsonObject);
}
return jsonArray.toJSONString();
}
/**
* 根据类型转换数据
*
* @param rs JDBC返回结果集
* @param md 元数据
* @param jsonObject json对象
* @throws Exception 例外
*/
private static void getType(ResultSet rs, ResultSetMetaData md, Map<String, Object> jsonObject) throws Exception {
int columnCount = md.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String columnName = md.getColumnName(i);
int columnType = md.getColumnType(i);
switch (columnType) {
case Types.ARRAY:
jsonObject.put(columnName, rs.getArray(columnName));
break;
case Types.BIGINT:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.BOOLEAN:
jsonObject.put(columnName, rs.getBoolean(columnName));
break;
case Types.BLOB:
Blob blob = rs.getBlob(columnName);
byte[] bytes = new byte[(int) blob.length()];
try (InputStream in = blob.getBinaryStream()) {
in.read(bytes);
String tmp = new String(bytes);
jsonObject.put(columnName, tmp);
} catch (IOException e) {
// ignore
}
break;
case Types.DOUBLE:
jsonObject.put(columnName, rs.getDouble(columnName));
break;
case Types.FLOAT:
jsonObject.put(columnName, rs.getFloat(columnName));
break;
case Types.INTEGER:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.NVARCHAR:
jsonObject.put(columnName, rs.getNString(columnName));
break;
case Types.VARCHAR:
jsonObject.put(columnName, rs.getString(columnName));
break;
case Types.TINYINT:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.SMALLINT:
jsonObject.put(columnName, rs.getInt(columnName));
break;
case Types.DATE:
jsonObject.put(columnName, rs.getDate(columnName));
break;
case Types.TIMESTAMP:
jsonObject.put(columnName, rs.getTimestamp(columnName));
break;
case Types.CLOB:
jsonObject.put(columnName, rs.getString(columnName));
break;
case Types.DECIMAL:
jsonObject.put(columnName, rs.getBigDecimal(columnName));
break;
case Types.NUMERIC:
jsonObject.put(columnName, rs.getBigDecimal(columnName));
break;
default:
jsonObject.put(columnName, rs.getObject(columnName));
break;
}
}
}
/**
* 自定义事务,保存查询预校验执行记录
*
* @param requestContext 请求上下文
* @param sysDpExecuteHistory 传入的参数
*/
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
@Override
public void insertSysDpExecuteHistory(IRequest requestContext, SysDpExecuteHistory sysDpExecuteHistory) {
self().insertSelective(requestContext, sysDpExecuteHistory);
}
@Override
public ResponseData execute(IRequest requestContext, String ip, String dpContext) throws Exception {
ResponseData responseData = new ResponseData(true);
if (StringUtils.isNotBlank(dpContext)) {
StringBuilder msg = new StringBuilder();
// 支持多条语句执行,以“;“隔开
String[] undos = StringUtils.trimToEmpty(dpContext).split(";");
int length = undos.length;
ConcurrentHashMap<Integer, String> retMsg = null;
if (length >= 1) {
retMsg = new ConcurrentHashMap<>(1);
}
List<String> result = new ArrayList<>(retMsg.size());
AtomicInteger atomicInteger = new AtomicInteger();
for (int i = 1; i <= length; i++) {
String undo = undos[i-1];
if (StringUtils.isBlank(undo)) {
continue;
}
String formatSql = undo;
if (ConfigUtils.isOracle()) {
try {
formatSql = SQLUtils.format(formatSql, JdbcConstants.ORACLE);
} catch (ParserException ex) {
msg.append("语句不正确!错误信息:").append(ex.getMessage());
}
} else if (ConfigUtils.isMySQL()) {
try {
formatSql = SQLUtils.format(formatSql, JdbcConstants.MYSQL);
} catch (ParserException ex) {
msg.append("语句不正确!错误信息:").append(ex.getMessage());
}
}
if (StringUtils.isNotEmpty(msg.toString())) {
throw new HlsCusException(msg.toString());
}
// 判断语句开始关键字
if (StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.SQL_EXEC_SELECT)) {
throw new HlsCusException("执行按钮不支持SELECT查询语句,请使用查询按钮");
} else if (StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.SQL_EXEC_INSERT)) {
String checkMsg = SqlParserCheckUtils.parserCheckInsert(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_INSERT, length, atomicInteger, i, retMsg));
while (true) {
if (future.isDone()) {
retMsg.forEach((k, v) -> {
result.add("第" + k + "行语句," + v);
});
retMsg.clear();
break;
}
}
} 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;
}
}
} 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;
}
}
} else if (StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_CREATE_TABLE) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_ALTER_TABLE) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_COMMENT_TABLE) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_COMMENT_ON_COLUMN) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_CREATE_INDEX) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_CREATE_SEQUENCE) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_CREATE_SEQUENCE) ||
StringUtils.startsWithIgnoreCase(formatSql, SqlConstantUtils.DDL_ALTER_SYSTEM_KILL_SESSION)) {
String checkMsg = SqlParserCheckUtils.parserCheckDDL(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_DDL, length, atomicInteger, i, retMsg));
while (true) {
if (future.isDone()) {
retMsg.forEach((k, v) -> {
result.add("第" + k + "行语句," + v);
});
retMsg.clear();
break;
}
}
}
}
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 ConcurrentHashMap<Integer, String> retMsg;
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;
this.retMsg = retMsg;
}
@Override
public void run() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
String threadName = Thread.currentThread().getName();
ResponseData responseData = new ResponseData();
int index = atomicInteger.getAndIncrement();
if (StringUtils.equals(execType, SqlConstantUtils.SQL_EXEC_INSERT)) {
responseData = service.insert(requestContext, ip, sql);
} else if (StringUtils.equals(execType, SqlConstantUtils.SQL_EXEC_UPDATE)) {
responseData = service.update(requestContext, ip, sql);
} else if (StringUtils.equals(execType, SqlConstantUtils.SQL_EXEC_DELETE)) {
responseData = 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");
}
} catch (Exception e) {
retMsg.put(seq, e.getMessage());
logger.info("=========> 执行语句[{}]出错,详细信息:[{}]", sql, e.getMessage());
} finally {
mainLock.unlock();
}
}
}
}
\ No newline at end of file
package com.hand.hls.dp.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static java.util.Collections.emptyList;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/6/4 12:30
* @since
*/
public class ArrayUtils {
/**
* Indicates whether the given object is not {@code null} and is an array.
*
* @param o the given object.
* @return {@code true} if the given object is not {@code null} and is an array, otherwise {@code false}.
*/
public static boolean isArray(Object o) {
return o != null && o.getClass().isArray();
}
/**
* Indicates whether the given array is {@code null} or empty.
*
* @param <T> the type of elements of the array.
* @param array the array to check.
* @return {@code true} if the given array is {@code null} or empty, otherwise {@code false}.
*/
public static <T> boolean isNullOrEmpty(T[] array) {
return array == null || isEmpty(array);
}
/**
* Returns an array containing the given arguments.
*
* @param <T> the type of the array to return.
* @param values the values to store in the array.
* @return an array containing the given arguments.
*/
@SafeVarargs
public static <T> T[] array(T... values) {
return values;
}
/**
* Returns all the non-{@code null} elements in the given array.
*
* @param <T> the type of elements of the array.
* @param array the given array.
* @return all the non-{@code null} elements in the given array. An empty list is returned if the given array is
* {@code null}.
*/
public static <T> List<T> nonNullElementsIn(T[] array) {
if (array == null) {
return emptyList();
}
List<T> nonNullElements = new ArrayList<>();
for (T o : array) {
if (o != null) {
nonNullElements.add(o);
}
}
return nonNullElements;
}
/**
* Returns {@code true} if the given array has only {@code null} elements, {@code false} otherwise. If given array is
* empty, this method returns {@code true}.
*
* @param <T> the type of elements of the array.
* @param array the given array. <b>It must not be null</b>.
* @return {@code true} if the given array has only {@code null} elements or is empty, {@code false} otherwise.
* @throws NullPointerException if the given array is {@code null}.
*/
public static <T> boolean hasOnlyNullElements(T[] array) {
Objects.requireNonNull(array);
if (isEmpty(array)) {
return false;
}
for (T o : array) {
if (o != null) {
return false;
}
}
return true;
}
private static <T> boolean isEmpty(T[] array) {
return array.length == 0;
}
public static boolean isObjectArray(Object o) {
return isArray(o) && !isArrayTypePrimitive(o);
}
public static boolean isArrayTypePrimitive(Object o) {
return o != null && o.getClass().getComponentType().isPrimitive();
}
}
package com.hand.hls.dp.util;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.nio.Buffer;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 15:41
* @since
*/
public class IPUtils {
private static final String IPV4_LOCAL_HOST = "127.0.0.1";
private static final String IPV6_LOCAL_HOST = "0:0:0:0:0:0:0:1";
public static String getIpAddress(HttpServletRequest request) {
boolean ipv6 = false;
if (Objects.isNull(request)) {
return "";
}
// X-Forwarded-For:Squid 服务代理
String ip = request.getHeader("x-forwarded-for");
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// Proxy-Client-IP:apache 服务代理
ip = request.getHeader("Proxy-Client-IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// WL-Proxy-Client-IP:weblogic 服务代理
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// X-Real-IP:nginx服务代理
ip = request.getHeader("X-Real-IP");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
// 本地局域网放行
if (Objects.equals(IPV4_LOCAL_HOST,ip) || Objects.equals(IPV6_LOCAL_HOST,ip)) {
// 根据网卡获取本机配置的ip
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
} catch (Exception e) {
// 默认
ip = IPV4_LOCAL_HOST;
}
byte[] address = inetAddress.getAddress();
if (address.length == 16) {
// IPV6
ip = inetAddress.getHostAddress();
ipv6 = true;
} else if (address.length == 4) {
// IPV4
ip = inetAddress.getHostAddress();
} else {
//throw new Exception("Invalid IPv6 address: '" + ip + '\'');
// 默认
ip = IPV6_LOCAL_HOST;
ipv6 = true;
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
// "***.***.***.***".length() = 15
if (Objects.nonNull(ip) && ip.length() > 15 && !ipv6) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return extractString(ip);
}
/**
* 获取访问主机的网段(4位)
*/
private static String extractString(String s) {
if (StringUtils.isEmpty(s)) {
return "";
}
StringBuilder retVal = new StringBuilder();
String[] split = s.split("\\.");
for (int i = 0; i < 4; i++) {
retVal.append(split[i]);
if (i < 3) {
retVal.append(".");
}
}
return retVal.toString();
}
// private static final String inet6AddressToAscii(byte[] address) {
// int longestRunOffset = -1;
// int longestRunLength = 0;
// boolean var3 = false;
// boolean var4 = false;
// int var5 = false;
//
// for(int i = 0; i < address.length; i += 2) {
// int currentRunOffset;
// for(currentRunOffset = i; i < 16 && address[i] == 0 && address[i + 1] == 0; i += 2) {
// }
//
// int currentRunLength = i - currentRunOffset;
// if (currentRunLength > longestRunLength && currentRunLength >= 4) {
// longestRunOffset = currentRunOffset;
// longestRunLength = currentRunLength;
// }
// }
//
// Buffer result = new Buffer();
// int i = 0;
//
// while(i < address.length) {
// if (i == longestRunOffset) {
// result.writeByte(58);
// i += longestRunLength;
// if (i == 16) {
// result.writeByte(58);
// }
// } else {
// if (i > 0) {
// result.writeByte(58);
// }
//
// int group = Util.and(address[i], 255) << 8 | Util.and(address[i + 1], 255);
// result.writeHexadecimalUnsignedLong((long)group);
// i += 2;
// }
// }
//
// return result.readUtf8();
// }
}
package com.hand.hls.dp.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipOutputStream;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/20 10:19
* @since
*/
public class LogUtils {
private static final Logger logger = LoggerFactory.getLogger(LogUtils.class);
/**
* 兼容不同操作系统间区别
*/
private static final String separator = File.separator;
/** 默认缓存大小 */
public static final int DEFAULT_BUFFER_SIZE = 256;
/** 默认大缓存大小 */
public static final int DEFAULT_LARGE_BUFFER_SIZE = 1024;
/** 数据流末尾 */
public static final int EOF = -1;
/**
* 通过目录查找所属文件
* @param dir 待查询目录
* @param fileNames 查询结果
*/
public static void findFiles(File dir, List<String> fileNames){
Objects.requireNonNull(dir,"file dir must not be null");
if (!dir.exists() || !dir.isDirectory()) {
return;
}
String[] files = dir.list();
for (String s : Objects.requireNonNull(files)) {
File file = new File(dir, s);
if (file.isFile()) {
fileNames.add(dir + separator + file.getName());
} else {
findFiles(file, fileNames);
}
}
}
/**
* 读取文件内容写入ZipOutputStream
* @param filePath 待读取文件全路径
* @param zos 写入内容的ZipOutputStream
*/
public static void readFileToZip(final String filePath, ZipOutputStream zos){
Objects.requireNonNull(filePath,"filePath must not be null");
Objects.requireNonNull(zos,"ZipOutputStream must not be null");
try (RandomAccessFile raf = new RandomAccessFile(filePath,"r");
FileChannel fileChannel = raf.getChannel()){
ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_LARGE_BUFFER_SIZE);
int n = 0;
while (EOF != (n = fileChannel.read(buffer))) {
// 切换buffer读模式
buffer.flip();
while (buffer.hasRemaining()) {
int remain = buffer.remaining();
byte[] bytes = new byte[remain];
// 从buffer读取数据到bytes
buffer.get(bytes);
// 内容写入ZipOutputStream
zos.write(bytes,0,n);
}
// 切换buffer写模式
buffer.clear();
}
} catch (FileNotFoundException fe){
// 文件没找到
if (logger.isErrorEnabled()) {
logger.error("{} file not found!", filePath);
}
} catch (IOException e){
// io例外
if (logger.isWarnEnabled()) {
logger.warn("{} file IOException!Message:[{}]", filePath, e.getMessage());
}
}
}
}
package com.hand.hls.dp.util;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:37
* @since
*/
public class SqlCheckUtils {
private static final Logger logger = LoggerFactory.getLogger(SqlCheckUtils.class);
public static boolean checkSql(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
return false;
}
for (String condition : conditions) {
if (StringUtils.contains(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSql4WhereOneToOne(String sql) {
int lastIndex = StringUtils.lastIndexOfIgnoreCase(sql, SqlConstantUtils.SQL_WHERE);
String condition = StringUtils.substringAfterLast(sql, StringUtils.substring(sql, lastIndex, lastIndex + 5));
String[] conditions = StringUtils.split(StringUtils.trimToEmpty(condition), SqlConstantUtils.SQL_WHERE_ADD);
if (ArrayUtils.isNullOrEmpty(conditions)) {
conditions = StringUtils.split(condition, SqlConstantUtils.SQL_WHERE_ADD_UPPER);
}
for (String c : conditions) {
c = StringUtils.deleteWhitespace(StringUtils.normalizeSpace(c));
String[] tmpCons = StringUtils.split(c, SqlConstantUtils.SQL_WHERE_1T1_EQ);
if (!ArrayUtils.isNullOrEmpty(tmpCons)) {
List<String> cons = ArrayUtils.nonNullElementsIn(tmpCons);
cons.remove(SqlConstantUtils.SQL_WHERE_1T1_EQ);
for (int i = 0; i < cons.size(); i++) {
if (i == 1) {
String startCon = cons.get(i - 1);
String endCon = cons.get(i);
if (StringUtils.equals(startCon, endCon)) {
return true;
}
}
}
}
}
return false;
}
public static boolean checkSqlIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (StringUtils.containsIgnoreCase(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlFilterTableIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (StringUtils.containsIgnoreCase(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlStartWithIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (!StringUtils.startsWith(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlStartWith4DdlIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_CREATE_TABLE)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_ALTER_TABLE)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_COMMENT_TABLE)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_COMMENT_ON_COLUMN)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_CREATE_INDEX)) {
return false;
} else if (StringUtils.startsWith(sql, SqlConstantUtils.DDL_CREATE_SEQUENCE)) {
return false;
}
for (String condition : conditions) {
if (StringUtils.startsWith(sql, condition)) {
return true;
}
}
return false;
}
public static boolean checkSqlEndsWithIgnoreCase(String sql, String... conditions) {
Objects.requireNonNull(conditions, "conditions 条件不能为空");
if (conditions.length < 1) {
throw new IllegalArgumentException("conditions 条件不能为空");
}
for (String condition : conditions) {
if (StringUtils.endsWithIgnoreCase(sql, condition)) {
return true;
}
}
return false;
}
private final static SQLParserFeature[] FORMAT_DEFAULT_FEATURES = {
SQLParserFeature.KeepComments,
SQLParserFeature.EnableSQLBinaryOpExprGroup
};
public static String format(String sql, String dbType) {
return format(sql, dbType, null, null);
}
public static String format(String sql, String dbType, List<Object> parameters, SQLUtils.FormatOption option) throws ClassCastException, ParserException{
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, FORMAT_DEFAULT_FEATURES);
List<SQLStatement> statementList = parser.parseStatementList();
return SQLUtils.toSQLString(statementList, dbType, parameters, option);
}
}
package com.hand.hls.dp.util;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/4/18 16:25
* @since
*/
public interface SqlConstantUtils {
String SELECT_COUNT = "SELECT COUNT(*) AS TOTALCOUNT FROM ";
String SELECT_FROM = "SELECT * FROM ";
String SQL_STAR = "*";
String SQL_SELECT = "select";
String SQL_FROM = "from";
String SQL_FROM_UPPER = "FROM";
String SQL_WHERE = "where";
String SQL_WHERE_UPPER = "WHERE";
String SQL_WHERE_ADD = "and";
String SQL_WHERE_ADD_UPPER = "AND";
String SQL_UDPATE = "update";
String SQL_DELETE = "delete";
String SQL_DROP = "drop";
String SQL_TRUNCATE = "truncate";
String SQL_SET = "set";
String SQL_SET_UPPER = "SET";
String SQL_INSERT_INTO = "insert into";
String DDL_CREATE_TABLE = "create table";
String DDL_ALTER_TABLE = "alter table";
String DDL_COMMENT_TABLE = "comment on table";
String DDL_COMMENT_ON_COLUMN = "comment on column";
String DDL_CREATE_INDEX = "create index";
String DDL_CREATE_SEQUENCE = "create sequence";
String DDL_ALTER_SEQUENCE = "alter sequence";
String DDL_ALTER_SYSTEM_KILL_SESSION = "alter system kill session";
String SQL_EXEC_SELECT = "SELECT";
String SQL_EXEC_INSERT = "INSERT";
String SQL_EXEC_UPDATE = "UPDATE";
String SQL_EXEC_DELETE = "DELETE";
String SQL_EXEC_DDL = "DDL";
String SQL_EXEC_DONE = "DONE";
String SQL_EXEC_DE = "DE";
String SQL_EXEC_UNDO = "UNDO";
String SQL_WHERE_1T1_EQ = "=";
String[] SQL_FILTER_TABLE = new String[]{"sys_dp_execute_history","QRTZ_","ACT_","SYS_USER","SYS_ROLE_B","SYS_FUNCTION_B","SYS_ROLE_FUNCTION",
"SYS_RESOURCE_B","SYS_JOB_RUNNING_INFO","SYS_MODULE","SYS_WIDGETS","USER_","DBA_",};
}
package com.hand.hls.dp.util;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import org.apache.commons.collections.CollectionUtils;
import java.util.List;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/6/10 15:09
* @since 1.0
*/
public final class SqlParserCheckUtils {
private static final int STMTLISTSIZE = 2;
/**
* 校验查询语句
*
* @param sql 待校验语句
* @return 返回信息
*/
public static String parserCheckSelect(String sql) {
StringBuilder ret = new StringBuilder();
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
if (Objects.isNull(stmtList) || CollectionUtils.isEmpty(stmtList) || CollectionUtils.size(stmtList) >= STMTLISTSIZE) {
ret.append("语句不正确!");
return ret.toString();
}
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLSelectStatement) {
SQLSelectStatement selectStatement = (SQLSelectStatement) stmt;
SQLSelect sqlSelect = selectStatement.getSelect();
SQLSelectQueryBlock queryBlock = sqlSelect.getQueryBlock();
String selectRet = parseSelectBlock(ret, queryBlock);
if (selectRet != null) {return selectRet;}
// 校验查询字段里的SUB SELECT
List<SQLSelectItem> sqlSelectItemList = queryBlock.getSelectList();
for (SQLSelectItem selectItem : sqlSelectItemList) {
SQLExpr itemExpr = selectItem.getExpr();
if (itemExpr instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) itemExpr;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {return subSelectRet;}
}
}
} else {
ret.append("不是SELECT语句!");
}
}
return ret.toString();
}
private static String parseSelectBlock(StringBuilder ret, SQLSelectQueryBlock queryBlock) {
SQLExpr where = queryBlock.getWhere();
if (Objects.isNull(where)) {
return ret.append("SELECT语句没有WHERE条件!").toString();
}
if (where instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr sqlBinaryOpExpr = (SQLBinaryOpExpr) where;
SQLBinaryOperator sqlBinaryOperator = sqlBinaryOpExpr.getOperator();
if (Objects.equals(SQLBinaryOperator.Equality, sqlBinaryOperator)) {
SQLExpr right = sqlBinaryOpExpr.getRight();
if (right instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) right;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
parseSelectBlock(ret, subQueryBlock);
if (ret != null) {
return ret.toString();
}
}
SQLExpr left = sqlBinaryOpExpr.getLeft();
if (left instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) left;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
if (Objects.equals(right, left)) {
assert ret != null;
return ret.append("SELECT语句WHERE条件含有1 = 1!").toString();
}
} else {
// WHERE条件集合
List<SQLObject> whereList = where.getChildren();
for (SQLObject so : whereList) {
if (so instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr sbo = (SQLBinaryOpExpr) so;
SQLExpr right = sbo.getRight();
if (right instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) right;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
SQLExpr left = sbo.getLeft();
if (left instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) left;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
if (Objects.equals(right, left)) {
assert ret != null;
return ret.append("SELECT语句WHERE条件含有1 = 1!").toString();
}
}
}
}
}
return null;
}
/**
* 校验新增语句
*
* @param sql 待校验语句
* @return 返回信息
*/
public static String parserCheckInsert(String sql) {
StringBuilder ret = new StringBuilder();
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
if (Objects.isNull(stmtList) || CollectionUtils.isEmpty(stmtList) || CollectionUtils.size(stmtList) >= STMTLISTSIZE) {
ret.append("语句不正确!");
return ret.toString();
}
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLInsertStatement) {
SQLInsertStatement insertStatement = (SQLInsertStatement) stmt;
// 如果含有SUB SELECT语句
SQLSelect sqlSelect = insertStatement.getQuery();
if (Objects.nonNull(sqlSelect)) {
SQLSelectQueryBlock queryBlock = sqlSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, queryBlock);
if (subSelectRet != null) {return subSelectRet;}
}
} else {
ret.append("不是INSERT语句!");
}
}
return ret.toString();
}
/**
* 校验更新语句
*
* @param sql 待校验语句
* @return 返回信息
*/
public static String parserCheckUpdate(String sql) {
StringBuilder ret = new StringBuilder();
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
if (Objects.isNull(stmtList) || CollectionUtils.isEmpty(stmtList) || CollectionUtils.size(stmtList) >= STMTLISTSIZE) {
ret.append("语句不正确!");
return ret.toString();
}
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLUpdateStatement) {
SQLUpdateStatement updateStatement = (SQLUpdateStatement) stmt;
List<SQLUpdateSetItem> sqlUpdateSetItemList = updateStatement.getItems();
if (Objects.isNull(sqlUpdateSetItemList) || CollectionUtils.isEmpty(sqlUpdateSetItemList)) {
ret.append("UPDATE语句没有SET!");
}
// SET 集合
for (SQLUpdateSetItem sqlUpdateSetItem : sqlUpdateSetItemList) {
SQLExpr sqlUpdateExpr = sqlUpdateSetItem.getValue();
// SUB SELECT 语句
if (sqlUpdateExpr instanceof SQLQueryExpr) {
SQLQueryExpr sqlQueryExpr = (SQLQueryExpr) sqlUpdateExpr;
SQLSelect subSqlSelect = sqlQueryExpr.getSubQuery();
if (Objects.nonNull(subSqlSelect)) {
SQLSelectQueryBlock queryBlock = subSqlSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, queryBlock);
if (subSelectRet != null) {return subSelectRet;}
}
}
}
SQLExpr where = updateStatement.getWhere();
if (Objects.isNull(where)) {
return ret.append("UPDATE语句没有WHERE条件!").toString();
}
if (where instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr sqlBinaryOpExpr = (SQLBinaryOpExpr) where;
SQLBinaryOperator sqlBinaryOperator = sqlBinaryOpExpr.getOperator();
if (Objects.equals(SQLBinaryOperator.Equality, sqlBinaryOperator)) {
SQLExpr right = sqlBinaryOpExpr.getRight();
if (right instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) right;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {return subSelectRet;}
}
SQLExpr left = sqlBinaryOpExpr.getLeft();
if (left instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) left;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {return subSelectRet;}
}
if (Objects.equals(right, left)) {
return ret.append("UPDATE语句WHERE条件含有1 = 1!").toString();
}
} else {
// WHERE条件集合
List<SQLObject> whereList = where.getChildren();
for (SQLObject so : whereList) {
if (so instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr sbo = (SQLBinaryOpExpr) so;
SQLExpr right = sbo.getRight();
if (right instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) right;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
SQLExpr left = sbo.getLeft();
if (left instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) left;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
if (Objects.equals(right, left)) {
return ret.append("UPDATE语句WHERE条件含有1 = 1!").toString();
}
}
}
}
}
} else {
ret.append("不是UPDATE语句!");
}
}
return ret.toString();
}
/**
* 校验删除语句
*
* @param sql 待校验语句
* @return 返回信息
*/
public static String parserCheckDelete(String sql) {
StringBuilder ret = new StringBuilder();
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
if (Objects.isNull(stmtList) || CollectionUtils.isEmpty(stmtList) || CollectionUtils.size(stmtList) >= STMTLISTSIZE) {
ret.append("语句不正确!");
return ret.toString();
}
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLDeleteStatement) {
SQLDeleteStatement deleteStatement = (SQLDeleteStatement) stmt;
SQLExpr where = deleteStatement.getWhere();
if (Objects.isNull(where)) {
return ret.append("DELETE语句没有WHERE条件!").toString();
}
if (where instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr sqlBinaryOpExpr = (SQLBinaryOpExpr) where;
SQLBinaryOperator sqlBinaryOperator = sqlBinaryOpExpr.getOperator();
if (Objects.equals(SQLBinaryOperator.Equality, sqlBinaryOperator)) {
SQLExpr right = sqlBinaryOpExpr.getRight();
if (right instanceof SQLQueryExpr) {
SQLQueryExpr sqlWhereSubQueryExpr = (SQLQueryExpr) right;
SQLSelect sqlWhereSubSelect = sqlWhereSubQueryExpr.getSubQuery();
if (Objects.nonNull(sqlWhereSubSelect)) {
SQLSelectQueryBlock queryBlock = sqlWhereSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, queryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
}
SQLExpr left = sqlBinaryOpExpr.getLeft();
if (Objects.equals(right, left)) {
return ret.append("SELECT语句WHERE条件含有1 = 1!").toString();
}
} else {
// WHERE条件集合
List<SQLObject> whereList = where.getChildren();
for (SQLObject so : whereList) {
if (so instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr sbo = (SQLBinaryOpExpr) so;
SQLExpr right = sbo.getRight();
if (right instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) right;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
SQLExpr left = sbo.getLeft();
if (left instanceof SQLQueryExpr) {
SQLQueryExpr itemQueryExpr = (SQLQueryExpr) left;
SQLSelect itemSubSelect = itemQueryExpr.getSubQuery();
SQLSelectQueryBlock subQueryBlock = itemSubSelect.getQueryBlock();
String subSelectRet = parseSelectBlock(ret, subQueryBlock);
if (subSelectRet != null) {
return subSelectRet;
}
}
if (Objects.equals(right, left)) {
return ret.append("SELECT语句WHERE条件含有1 = 1!").toString();
}
}
}
}
}
} else {
ret.append("不是DELETE语句!");
}
}
return ret.toString();
}
/**
* 校验DDL语句
*
* @param sql 待校验语句
* @return 返回信息
*/
public static String parserCheckDDL(String sql) {
StringBuilder ret = new StringBuilder();
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
if (Objects.isNull(stmtList) || CollectionUtils.isEmpty(stmtList) || CollectionUtils.size(stmtList) >= STMTLISTSIZE) {
ret.append("语句不正确!");
return ret.toString();
}
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLDDLStatement) {
if (stmt instanceof SQLAlterTableStatement) {
// ignore
} else if (stmt instanceof SQLCreateTableStatement) {
// ignore
} else if (stmt instanceof SQLCreateSequenceStatement) {
// ignore
} else if (stmt instanceof SQLCreateViewStatement) {
// ignore
} else if (stmt instanceof SQLCreateIndexStatement) {
// ignore
} else if (stmt instanceof SQLAlterTableAddColumn) {
// ignore
} else if (stmt instanceof SQLAlterSequenceStatement) {
// ignore
} else {
ret.append("不支持的语句!");
}
} else {
ret.append("不支持的语句!");
}
}
return ret.toString();
}
}
package hls.core.sys.mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* 此mapper只用于查询syscode相关的值,暂无其他方法
* Created by fjm on 2017/7/31.
*/
public interface SysCodeValueMapper {
String queryCodeValueMeaning(@Param("code") String code, @Param("code_value") String codeValue);
List<Map> queryCodeDetails(String code);
}
<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
<a:init-procedure/>
<a:view>
<script><![CDATA[
function dp001_query() {
$('dp001_query_btn').disable();
let dpContext = document.getElementById('context_tta').value;
Leaf.showConfirm('${l:HLS.PROMPT}', '确认执行当前语句吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在执行...');
Leaf.request({
url: '${/request/@context_path}/sys/dp/query?ck=0',
para: dpContext,
success: function () {
let form = document.createElement("form");
form.target = "_export_dp_query_window";
form.method = "post";
let url = '${/request/@context_path}/sys/dp/query?ck=1';
let token = $jq('meta[name=_csrf]').attr('content');
form.action = url + (url.indexOf('?') == -1 ? '?' : '&') + 'r=' + Math.random() + '&_csrf=' + token;
let iframe = Ext.get('_export_dp_query_window') || new Ext.Template('<iframe id ="_export_dp_query_window" name="_export_dp_query_window" style="position:absolute;left:-1000px;top:-1000px;width:1px;height:1px;display:none"></iframe>').insertFirst(document.body, {}, true)
var s = document.createElement("input");
s.id = "_request_data";
s.type = 'hidden';
s.name = '_request_data';
let p = {"parameter": dpContext};
s.value = Ext.encode(p);
form.appendChild(s);
document.body.appendChild(form);
form.submit();
Ext.fly(form).remove();
setTimeout(function () {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 10000);
},
failure: function () {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
error: function () {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
scope: this
});
}, function cancel() {
$('dp001_query_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function dp001_exec() {
$('dp001_exec_btn').disable();
let dpContext = document.getElementById('context_tta').value;
Leaf.showConfirm('${l:HLS.PROMPT}', '确认执行当前语句吗?', function okFun() {
document.getElementById('result_tta').value = null;
Leaf.Masker.mask(Ext.getBody(), '正在执行...');
Leaf.request({
url: '${/request/@context_path}/sys/dp/v2/execute',
para: dpContext,
success: function (res) {
setTimeout(function () {
$('dp001_exec_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
if (res.success) {
Leaf.showMessage('提示', '执行完成!');
// document.getElementById('result_tta').value = res.error.message;
document.getElementById('result_tta').value = res.result.record;
}
}, 10000);
},
failure: function () {
$('dp001_exec_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
error: function () {
$('dp001_exec_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
},
scope: this
});
}, function cancel() {
$('dp001_exec_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function download_log() {
$('dp001_download_log_btn').disable();
Leaf.showConfirm('${l:HLS.PROMPT}', '确认下载日志吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在下载中...');
let form = document.createElement("form");
form.target = "_export_dp_log_window";
form.method = "post";
let url = '${/request/@context_path}/sys/dp/download/log';
let token = $jq('meta[name=_csrf]').attr('content');
form.action = url + (url.indexOf('?') == -1 ? '?' : '&') + 'r=' + Math.random() + '&_csrf=' + token;
let iframe = Ext.get('_export_dp_log_window') || new Ext.Template('<iframe id ="_export_dp_log_window" name="_export_dp_log_window" style="position:absolute;left:-1000px;top:-1000px;width:1px;height:1px;display:none"></iframe>').insertFirst(document.body, {}, true)
document.body.appendChild(form);
form.submit();
Ext.fly(form).remove();
setTimeout(function () {
$('dp001_download_log_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 10000);
}, function cancel() {
$('dp001_download_log_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
]]></script>
<a:screenBody>
<a:screenTopToolbar>
<a:gridButton id="dp001_query_btn" click="dp001_query" text="查询"/>
<a:gridButton id="dp001_exec_btn" click="dp001_exec" text="执行"/>
<a:gridButton id="dp001_download_log_btn" click="download_log" text="日志下载"/>
</a:screenTopToolbar>
<a:fieldSet title="待执行">
<a:textArea id="context_tta" name="context" marginWidth="50" marginHeight="450"/>
</a:fieldSet>
<a:fieldSet title="结果">
<a:textArea id="result_tta" readOnly="true" name="result" marginWidth="50" marginHeight="560"/>
</a:fieldSet>
</a:screenBody>
</a:view>
</a:screen>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="hls.core.sys.mapper.SysCodeValueMapper">
<!--
可以根据以下sql片段查询出syscode相关的值
使用方法如下:
<select id="demo">
select
view.code_id
from
<include refid="hls.core.sys.mapper.SysCodeValueMapper.queryCodeDetail" /> view
</select>
-->
<sql id="queryCodeDetail">
(SELECT
a.CODE_ID AS code_id ,
a.CODE AS code ,
a.DESCRIPTION AS code_name ,
b.CODE_VALUE_ID AS code_value_id ,
b.VALUE AS code_value ,
b.DESCRIPTION AS code_value_name ,
b.MEANING AS meaning ,
b.ORDER_SEQ AS order_seq
FROM
sys_code_b a
LEFT JOIN sys_code_value_b b
ON
a.CODE_ID = b.CODE_ID)
</sql>
<resultMap id="codeDetailsResultMap" type="map">
<result column="code_id" property="code_id"/>
<result column="code" property="code"/>
<result column="code_name" property="code_name"/>
<result column="code_value_id" property="code_value_id"/>
<result column="code_value" property="code_value"/>
<result column="code_value_name" property="code_value_name"/>
<result column="meaning" property="meaning"/>
<result column="order_seq" property="order_seq"/>
</resultMap>
<select id="queryCodeDetails" parameterType="java.lang.String" resultMap="codeDetailsResultMap">
SELECT
a.CODE_ID AS code_id ,
a.CODE AS code ,
a.DESCRIPTION AS code_name ,
b.CODE_VALUE_ID AS code_value_id ,
b.VALUE AS code_value ,
b.DESCRIPTION AS code_value_name ,
b.MEANING AS meaning ,
b.ORDER_SEQ AS order_seq
FROM
sys_code_b a
LEFT JOIN sys_code_value_b b
ON
a.CODE_ID = b.CODE_ID
where a.CODE = #{code}
</select>
<select id="queryCodeValueMeaning" parameterType="java.lang.String" resultType="java.lang.String">
SELECT b.MEANING AS meaning
FROM
sys_code_b a
LEFT JOIN sys_code_value_b b
ON
a.CODE_ID = b.CODE_ID
where b.enabled_flag = 'Y'
and b.value = #{code_value}
and a.CODE = #{code}
</select>
<sql id="queryFndEmployeeAssignsDetail" >
(
SELECT
t1.EMPLOYEE_ASSIGN_ID AS EMPLOYEE_ASSIGN_ID,
t1.ENABLED_FLAG AS enabled_flag,
t1.PRIMARY_POSITION_FLAG AS primary_position_flag,
t1.EMPLOYEE_ID AS employee_id,
e.ENABLED_FLAG AS employee_enabled,
e.EMPLOYEE_CODE AS EMPLOYEE_CODE,
e.NAME AS employee_name,
t1.COMPANY_ID AS company_id,
c.enabled_flag AS company_enabled,
c.company_code AS company_code,
c.company_short_name AS company_short_name,
t1.POSITION_ID AS position_id,
p.enabled_flag AS position_enabled,
p.position_code AS position_code,
p.position_type AS position_type,
p.position_name AS POSITION_NAME,
p.unit_id AS unit_id,
u.ENABLED_FLAG AS unit_enabled,
u.UNIT_CODE AS unit_code,
u.UNIT_NAME AS UNIT_NAME
FROM
fnd_employee_assigns t1,fnd_employee e, fnd_company c , fnd_org_position p , fnd_org_unit u
WHERE
e.EMPLOYEE_ID = t1.EMPLOYEE_ID
AND c.company_id = t1.COMPANY_ID
AND p.position_id = t1.POSITION_ID
AND u.UNIT_ID = p.unit_id
)
</sql>
<sql id="queryFndOrgUnitMgrDetail">
SELECT
u.UNIT_ID AS unit_id,
u.UNIT_CODE AS unit_code,
u.UNIT_NAME AS unit_name,
u.CHIEF_POSITION_ID AS mgr_position_id,
mgr_a.position_code AS mgr_position_code,
mgr_a.POSITION_NAME AS mgr_position_name,
mgr_a.employee_id AS mgr_employee_id,
mgr_a.EMPLOYEE_CODE AS mgr_employee_code,
mgr_a.employee_name AS mgr_employee_name,
u.PARENT_UNIT_ID AS parent_unit_id,
u.COMPANY_ID AS company_id,
u.ENABLED_FLAG AS unit_enabled,
mgr_a.position_enabled AS mgr_position_enabled,
mgr_a.enabled_flag AS mgr_assign_enabled
FROM
( fnd_org_unit u JOIN
<include refid="hls.core.sys.mapper.SysCodeValueMapper.queryFndEmployeeAssignsDetail"/>
mgr_a )
WHERE
( mgr_a.position_id = u.CHIEF_POSITION_ID )
</sql>
<sql id="queryHlsBpMasterDetail">
SELECT
hbm.BP_ID AS BP_ID,
hbm.OWNER_USER_ID AS OWNER_USER_ID,
hbm.BP_CODE AS BP_CODE,
hbm.BP_NAME AS BP_NAME,
hbm.EXTRA_NAM AS EXTRA_NAM,
hbm.BP_CLASS AS BP_CLASS,
hbm.BP_CATEGORY AS BP_CATEGORY,
hbm.BP_TYPE AS BP_TYPE,
hbm.BP_TITLE AS BP_TITLE,
hbm.SEARCH_TERM_1 AS SEARCH_TERM_1,
hbm.SEARCH_TERM_2 AS SEARCH_TERM_2,
hbm.EXTERNAL_BP_CODE AS EXTERNAL_BP_CODE,
hbm.ADDRESS_ID AS ADDRESS_ID,
hbm.ENABLED_FLAG AS ENABLED_FLAG,
hbm.FIRST_NAME AS FIRST_NAME,
hbm.MIDDLE_NAME AS MIDDLE_NAME,
hbm.LAST_NAME AS LAST_NAME,
hbm.GENDER AS GENDER,
hbm.NATIONALITY AS NATIONALITY,
hbm.DATE_OF_BIRTH AS DATE_OF_BIRTH,
hbm.PLACE_OF_BIRTH AS PLACE_OF_BIRTH,
hbm.NAME_AT_BIRTH AS NAME_AT_BIRTH,
hbm.MARITAL_STATUS AS MARITAL_STATUS,
hbm.NUMBER_OF_CHILDREN AS NUMBER_OF_CHILDREN,
hbm.ACADEMIC_BACKGROUND AS ACADEMIC_BACKGROUND,
hbm.AGE AS AGE,
hbm.ID_TYPE AS ID_TYPE,
hbm.ID_CARD_NO AS ID_CARD_NO,
hbm.ANNUAL_INCOME AS ANNUAL_INCOME,
hbm.CURRENCY AS CURRENCY,
hbm.CAPITAL_OF_FAMILY AS CAPITAL_OF_FAMILY,
hbm.LIABILITY_OF_FAMILY AS LIABILITY_OF_FAMILY,
hbm.LEGAL_FORM AS LEGAL_FORM,
hbm.INDUSTRY AS INDUSTRY,
hbm.BUSINESS_LICENSE_NUM AS BUSINESS_LICENSE_NUM,
hbm.CORPORATE_CODE AS CORPORATE_CODE,
hbm.ORGANIZATION_CODE AS ORGANIZATION_CODE,
hbm.TAX_REGISTRY_NUM AS TAX_REGISTRY_NUM,
hbm.REGISTERED_PLACE AS REGISTERED_PLACE,
hbm.FOUNDED_DATE AS FOUNDED_DATE,
hbm.REGISTERED_CAPITAL AS REGISTERED_CAPITAL,
hbm.BALANCE_SHEET_CURRENCY AS BALANCE_SHEET_CURRENCY,
hbm.TAXPAYER_TYPE AS TAXPAYER_TYPE,
hbm.INVOICE_TITLE AS INVOICE_TITLE,
hbm.INVOICE_BP_ADDRESS_PHONE_NUM AS INVOICE_BP_ADDRESS_PHONE_NUM,
hbm.INVOICE_BP_BANK_ACCOUNT AS INVOICE_BP_BANK_ACCOUNT,
hbm.LOAN_CARD_NUM AS LOAN_CARD_NUM,
hbm.PAID_UP_CAPITAL AS PAID_UP_CAPITAL,
hbm.COMPANY_NATURE AS COMPANY_NATURE,
hbm.PRIMARY_BUSINESS AS PRIMARY_BUSINESS,
hbm.MAIN_PRODUCTS AS MAIN_PRODUCTS,
hbm.BP_NAME_SP AS BP_NAME_SP,
hbm.GENDER_SP AS GENDER_SP,
hbm.DATE_OF_BIRTH_SP AS DATE_OF_BIRTH_SP,
hbm.ACADEMIC_BACKGROUND_SP AS ACADEMIC_BACKGROUND_SP,
hbm.ID_TYPE_SP AS ID_TYPE_SP,
hbm.ID_CARD_NO_SP AS ID_CARD_NO_SP,
hbm.COUNTRY_SP AS COUNTRY_SP,
hbm.PROVINCE_SP AS PROVINCE_SP,
hbm.CITY_SP AS CITY_SP,
hbm.DISTRICT_SP AS DISTRICT_SP,
hbm.ADDRESS_SP AS ADDRESS_SP,
hbm.CREATED_BY AS CREATED_BY,
hbm.CREATION_DATE AS CREATION_DATE,
hbm.LAST_UPDATED_BY AS LAST_UPDATED_BY,
hbm.LAST_UPDATETIME_DATE AS LAST_UPDATETIME_DATE,
hbm.REF_V01 AS REF_V01,
hbm.REF_V02 AS REF_V02,
hbm.REF_V03 AS REF_V03,
hbm.REF_V04 AS REF_V04,
hbm.REF_V05 AS REF_V05,
hbm.REF_V06 AS REF_V06,
hbm.REF_V07 AS REF_V07,
hbm.REF_V08 AS REF_V08,
hbm.REF_V09 AS REF_V09,
hbm.REF_V10 AS REF_V10,
hbm.REF_V11 AS REF_V11,
hbm.REF_V12 AS REF_V12,
hbm.REF_V13 AS REF_V13,
hbm.REF_V14 AS REF_V14,
hbm.REF_V15 AS REF_V15,
hbm.REF_N01 AS REF_N01,
hbm.REF_N02 AS REF_N02,
hbm.REF_N03 AS REF_N03,
hbm.REF_N04 AS REF_N04,
hbm.REF_N05 AS REF_N05,
hbm.REF_N06 AS REF_N06,
hbm.REF_N07 AS REF_N07,
hbm.REF_N08 AS REF_N08,
hbm.REF_N09 AS REF_N09,
hbm.REF_N10 AS REF_N10,
hbm.REF_D01 AS REF_D01,
hbm.REF_D02 AS REF_D02,
hbm.REF_D03 AS REF_D03,
hbm.REF_D04 AS REF_D04,
hbm.REF_D05 AS REF_D05,
hbm.ENTERPRISE_SCALE AS ENTERPRISE_SCALE,
hbm.LEGAL_PERSON AS LEGAL_PERSON,
hbm.SPOUSE_PHONE AS SPOUSE_PHONE,
hbm.LICENSE_TERMS AS LICENSE_TERMS,
hbm.PORPORTION_OF_GUARANTEE AS PORPORTION_OF_GUARANTEE,
hbm.PERIOD_IN_JOB AS PERIOD_IN_JOB,
hbm.NET_MONTHLY_INCOME AS NET_MONTHLY_INCOME,
hbm.LEADER_FLAG AS LEADER_FLAG,
hbm.MORTGAGE_FLAG AS MORTGAGE_FLAG,
hbm.LOCAL_PERSON_FLAG AS LOCAL_PERSON_FLAG,
hbm.HIGH_MENTAL_FLAG AS HIGH_MENTAL_FLAG,
hbm.HAS_HOUSE_FLAG AS HAS_HOUSE_FLAG,
hbm.HAS_CAR_FLAG AS HAS_CAR_FLAG,
hbm.COMMUNITY_LEADER_FLAG AS COMMUNITY_LEADER_FLAG,
hbm.BANK_MATCH_FLAG AS BANK_MATCH_FLAG,
hbm.HOUSE_PROPERTY_VALUE AS HOUSE_PROPERTY_VALUE,
hbm.HOUSE_LOAN_BALANCE AS HOUSE_LOAN_BALANCE,
hbm.DEPOSIT_CERTIFICATE AS DEPOSIT_CERTIFICATE,
hbm.CREDIT_CARD_LIMIT AS CREDIT_CARD_LIMIT,
hbm.ADDRESS_ON_ID AS ADDRESS_ON_ID,
hbm.ADDRESS_ON_RESIDENT_BOOKLIT AS ADDRESS_ON_RESIDENT_BOOKLIT,
hbm.LIVING_ADDRESS AS LIVING_ADDRESS,
hbm.WORKING_PLACE AS WORKING_PLACE,
hbm.WORKING_DURATION AS WORKING_DURATION,
hbm.WORKING_ADDRESS AS WORKING_ADDRESS,
hbm.OPERATION_YEAR AS OPERATION_YEAR,
hbm.POSITION AS POSITION,
hbm.CELL_PHONE AS CELL_PHONE,
hbm.PHONE_EXTRA AS PHONE_EXTRA,
hbm.PHONE AS PHONE,
hbm.CONTACT_PERSON AS CONTACT_PERSON,
hbm.FAX_NUMBER AS FAX_NUMBER,
hbm.DEPARTMENT AS DEPARTMENT,
hbm.CREDIT_FLAG AS CREDIT_FLAG,
hbm.CREDIT_AMOUNT AS CREDIT_AMOUNT,
hbm.CREDIT_ALT AS CREDIT_ALT,
hbm.CREDIT_FORBID AS CREDIT_FORBID,
hbm.EMAIL AS EMAIL,
hbm.NC_STATUS AS NC_STATUS,
hbm.CELL_PHONE_2 AS CELL_PHONE_2,
hbm.EMPLOYEE_AMOUNT AS EMPLOYEE_AMOUNT,
hbm.BILLING_STATUS AS BILLING_STATUS,
hbm.FIN_NET_CASH_INFLOW AS FIN_NET_CASH_INFLOW,
hbm.FIN_MONTHLY_PAYMENT AS FIN_MONTHLY_PAYMENT,
hbm.FIN_MONTHS AS FIN_MONTHS,
hbm.FIN_LIQUIDITY_RATIO AS FIN_LIQUIDITY_RATIO,
hbm.FIN_LEVERAGE AS FIN_LEVERAGE,
hbm.FIN_DATA AS FIN_DATA,
hbm.FIN_EVALUATION AS FIN_EVALUATION,
hbm.CDD_LIST_ID AS CDD_LIST_ID,
hbm.PB_NUMBER AS PB_NUMBER,
hbm.REPO_NUMBER AS REPO_NUMBER,
hbm.FIN_NOTE1 AS FIN_NOTE1,
hbm.FIN_NOTE AS FIN_NOTE,
hbm.BLACK_FLAG AS BLACK_FLAG,
hbm.LOCK_FLAG AS LOCK_FLAG,
hbm.OLD_FLAG AS OLD_FLAG,
hbm.LIMIT_FROM AS LIMIT_FROM,
hbm.LIMIT_TO AS LIMIT_TO,
hbm.LAW_BP_FLAG AS LAW_BP_FLAG,
hbm.FIN_TURNOVER_1 AS FIN_TURNOVER_1,
hbm.FIN_TURNOVER AS FIN_TURNOVER,
hbm.IF_TO_ZX_FLAG AS IF_TO_ZX_FLAG,
hbm.ZX_LAST_UPDATE_DATE AS ZX_LAST_UPDATE_DATE,
hbm.ZX_LAST_UPDATED_BY AS ZX_LAST_UPDATED_BY,
hbm.CREDIT_AMOUNT_USED_FIX AS CREDIT_AMOUNT_USED_FIX,
hbm.CREDIT_AMOUNT_USED_CYCLE AS CREDIT_AMOUNT_USED_CYCLE,
hbm.CREDIT_AMOUNT_FROZEN_FIX AS CREDIT_AMOUNT_FROZEN_FIX,
hbm.CREDIT_AMOUNT_FROZEN_CYCLE AS CREDIT_AMOUNT_FROZEN_CYCLE,
hbm.POLLING_TIMES AS POLLING_TIMES,
hbm.SALE_TYPE AS SALE_TYPE,
hbm.ASSET_TYPE AS ASSET_TYPE,
hbm.AREA AS AREA,
hbm.LISTED_COMPANY_FLAG AS LISTED_COMPANY_FLAG,
hbm.MARKET_LOCATION AS MARKET_LOCATION,
hbm.CREDIT_RATING AS CREDIT_RATING,
hbm.RATING_DATE AS RATING_DATE,
hbm.INVOICE_KIND AS INVOICE_KIND,
hbm.LICENSE_TERMS_IF_LONG AS LICENSE_TERMS_IF_LONG,
hbm.LISTED_SUBJECT AS LISTED_SUBJECT,
hbm.LISTED_SUBJECT_ORG_CODE AS LISTED_SUBJECT_ORG_CODE,
hbm.ACTUAL_CONTROLLER_ID_TYPE AS ACTUAL_CONTROLLER_ID_TYPE,
hbm.ACTUAL_CONTROLLER_ID AS ACTUAL_CONTROLLER_ID,
hbm.ACTUAL_CONTROLLER_ORG_CODE AS ACTUAL_CONTROLLER_ORG_CODE,
hbm.BP_NAME_SP_CLASS AS BP_NAME_SP_CLASS
FROM
hls_bp_master hbm
</sql>
<sql id="sys_function_vl">
SELECT
t1.FUNCTION_ID AS function_id,
t1.FUNCTION_CODE AS function_code,
t1.FUNCTION_NAME_ID AS function_name_id,
t1.FUNCTION_NAME AS function_name,
t1.FUNCTION_TYPE AS function_type,
t1.PARENT_FUNCTION_ID AS parent_function_id,
t1.MODULE_CODE AS module_CODE,
t1.ICON AS icon,
t1.SEQUENCE AS sequence,
t1.SERVICE_ID AS service_id,
t1.CREATION_DATE AS creation_date,
t1.CREATED_BY AS created_by,
t1.LAST_UPDATE_DATE AS last_update_date,
t1.LAST_UPDATED_BY AS last_updated_by
FROM
sys_function_b t1
</sql>
<sql id="sys_role_function_item_v">
SELECT
e.role_function_group_id AS role_function_group_id,
e.role_id AS role_id,
e.function_group_id AS function_group_id,
r.function_id AS function_id,
f.function_code AS function_code,
f.function_name AS function_name,
f.service_id AS main_service_id,
(
SELECT
s.service_name
FROM
sys_service s
WHERE
(
s.service_id = f.service_id
)
) AS main_service_name,
r.layout_sequence AS layout_sequence,
r.enabled_flag AS enabled_flag,
e.parent_role_function_group_id AS parent_role_function_group_id,
r.creation_date AS creation_date,
r.created_by AS created_by,
r.last_update_date AS last_update_date,
r.last_updated_by AS last_updated_by
FROM
(
(
sys_role_function_group e
JOIN sys_function_group_item r
)
JOIN <include refid="hls.core.sys.mapper.SysCodeValueMapper.sys_function_vl"/> f
)
WHERE
(
(
e.function_group_id = r.function_group_id
)
AND (
r.function_id = f.function_id
)
AND (
NOT (
EXISTS (
SELECT
1
FROM
sys_role_function_exclude_item i
WHERE
(
(
i.role_function_group_id = e.role_function_group_id
)
AND (
i.function_id = r.function_id
)
)
)
)
)
AND (r.enabled_flag = 'Y')
)
</sql>
<sql id="sys_service_lookup_v">
SELECT
a.code_id AS code_id,
a.code AS code,
v.code_value_id AS code_value_id,
v.code_value AS code_value,
v.code_value_name_id AS code_value_name_id,
b.description_text AS code_value_name,
b.language AS language,
v.enabled_flag AS enabled_flag,
v.order_seq AS order_seq
FROM
sys_codes a,
sys_code_values v
LEFT JOIN fnd_descriptions b ON (
(
v.code_value_name_id = b.description_id
)
)
WHERE
(
(
a.code_id = v.code_id
)
AND (
b.ref_table = 'SYS_CODE_VALUES'
)
AND (
b.ref_field = 'CODE_VALUE_NAME_ID'
)
)
ORDER BY
v.order_seq
</sql>
</mapper>
\ No newline at end of file
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