当前位置:网站首页>SQL syntax detection
SQL syntax detection
2022-06-22 17:53:00 【Stay for love】
maven To configure :
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.2</version>
</dependency>use jsqlparser You can limit what kind of DML sql sentence , If only limited select Query statement , You can verify with the execution plan sql Grammar mistakes , as follows :
public SqlCheckRspVo sqlCheck(SqlCheckReqVo sqlCheckReqVo) {
SqlCheckRspVo sqlCheckRspVo = new SqlCheckRspVo();
if(!sqlCheckReqVo.getSql().contains("$")){
return new SqlCheckRspVo(false,"sql Not added in $");
}
sqlCheckReqVo.setSql(sqlCheckReqVo.getSql().trim());
if(sqlCheckReqVo.getSql().endsWith(";")){
sqlCheckReqVo.setSql(sqlCheckReqVo.getSql().substring(0,sqlCheckReqVo.getSql().length()-1));
}
for(AddReqVo.TableInfo tableInfo : sqlCheckReqVo.getRuleTables()) {
String sql = sqlCheckReqVo.getSql().replace("$",tableInfo.getTableName());
sqlCheckRspVo = sqlparse(sql);
if(sqlCheckRspVo.getCheckResult() == false){
return sqlCheckRspVo;
}
SqlSession sqlSession = null;
PreparedStatement pst ;
try {
sqlSession = getNativeSqlSession();
Connection dbCon = sqlSession.getConnection();
pst = dbCon.prepareStatement("explain plan for " +sql);
pst.executeUpdate();
pst.close();
} catch (SQLException e) {
sqlCheckRspVo.setCheckResult(false);
sqlCheckRspVo.setErrorLog(ExceptionUtils.getStackTrace(e));
return sqlCheckRspVo;
} finally {
if (null != sqlSession) {
closeNativeSqlSession(sqlSession);
}
}
}
return sqlCheckRspVo;
}
public SqlCheckRspVo sqlparse(String sqlContent){
SqlCheckRspVo sqlCheckRspVo = new SqlCheckRspVo();
try {
Statement parse = CCJSqlParserUtil.parse(sqlContent);
if(parse instanceof Select){
sqlCheckRspVo.setCheckResult(true);
}else{
sqlCheckRspVo.setCheckResult(false);
sqlCheckRspVo.setErrorLog(" Input only select sentence ");
}
}catch (Exception e){
sqlCheckRspVo.setCheckResult(false);
sqlCheckRspVo.setErrorLog(ExceptionUtils.getStackTrace(e));
}
return sqlCheckRspVo;
}边栏推荐
- 【招聘】[北京中关村/远程][TensorBase][开源数据仓库]等一群人,做一件事
- 一文带你掌握Tcpdump命令的使用
- 抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
- Xftp 7 (ftp/sftp client) v7.0.0107 official Chinese free official version (with file + installation tutorial)
- Qt Notes - qmap Custom key
- Source code of live video system, hiding and Title Modification of the top title bar
- MySQL instruction executes SQL file
- Mybaits:常用数据库操作(东软的操作)
- redis. clients. jedis. exceptions. JedisDataException ERR invalid password.
- Post to asp Net core length limitation and solution when transferring data
猜你喜欢

内容推荐流程

It may be the most comprehensive Matplotlib visualization tutorial in the whole network

试用了多款报表工具,终于找到了基于.Net 6开发的一个了
![[step 1 of advanced automated testing] 1 minute to introduce you to automated testing](/img/00/9647d552749092954a91bd84307773.png)
[step 1 of advanced automated testing] 1 minute to introduce you to automated testing

math_角函数&反三角函数

WPF effect chapter 190: playing listbox again

How to do well in R & D efficiency measurement and index selection

Team management | how to improve the thinking skills of technical leaders?

新手必会的静态站点生成器——Gridsome

How can the new generation of HTAP databases be reshaped in the cloud? Tidb V6 online conference will be announced soon!
随机推荐
云端极简部署Svelte3聊天室
Mybaits:接口代理方式实现Dao
What is flush software? Is it safe to open a mobile account?
Quartus Prime 18.0软件安装包和安装教程
同花顺软件是什么?网上开户安全么?
Quickly master asp Net authentication framework identity - user registration
JSP learning (I) -- overview of JSP
The MySQL of docker restarts, prompting error response from daemon: driver failed programming external connectivity on**
What is a flush? Is online account opening safe?
Some difficulties in making web pages
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
面试突击58:truncate、delete和drop的6大区别!
写给 Kubernetes 工程师的 mTLS 指南
How to understand volatile
170million passwords of Netcom learning link have been leaked! What are the remedies?
Source code of live video system, hiding and Title Modification of the top title bar
##Kibana+ELK集群日志处理
Mybaits: interface proxy implementation Dao
当线上线下的融合加速,当信息对接渠道的多样化,传统意义上的中心将没有必要
快速掌握 ASP.NET 身份认证框架 Identity - 用户注册