|
|
@@ -0,0 +1,55 @@ |
|
|
|
package com.hz.pm.api.sys.manage; |
|
|
|
|
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner; |
|
|
|
import com.hz.pm.api.common.exception.ReturnException; |
|
|
|
import com.hz.pm.api.sys.model.req.SqlRunnerReq; |
|
|
|
import com.ningdatech.basic.exception.BizException; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
/** |
|
|
|
* <p> |
|
|
|
* SqlRunnerManage |
|
|
|
* </p> |
|
|
|
* |
|
|
|
* @author WendyYang |
|
|
|
* @since 22:43 2024/12/16 |
|
|
|
*/ |
|
|
|
@Component |
|
|
|
public class SqlRunnerManage { |
|
|
|
|
|
|
|
@Value("${sql-runner.token:}") |
|
|
|
private String token; |
|
|
|
|
|
|
|
@Value("${sql-runner.open:false}") |
|
|
|
private Boolean open; |
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public synchronized Object execute(SqlRunnerReq req) { |
|
|
|
if (Boolean.FALSE.equals(open)) { |
|
|
|
throw ReturnException.wrap("未开启执行sql功能"); |
|
|
|
} |
|
|
|
if (!token.equals(req.getExecToken())) { |
|
|
|
throw ReturnException.wrap("execToken错误"); |
|
|
|
} |
|
|
|
try (SqlRunner runner = SqlRunner.db()) { |
|
|
|
switch (req.getExecType()) { |
|
|
|
case DELETE: |
|
|
|
return runner.delete(req.getExecSql()); |
|
|
|
case INSERT: |
|
|
|
return runner.insert(req.getExecSql()); |
|
|
|
case SELECT: |
|
|
|
return runner.selectList(req.getExecSql()); |
|
|
|
case UPDATE: |
|
|
|
return runner.update(req.getExecSql()); |
|
|
|
default: |
|
|
|
throw ReturnException.wrap("不支持的execType"); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
throw BizException.wrap("执行sql异常"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |