当前位置:网站首页>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;
}边栏推荐
- Xftp 7 (ftp/sftp client) v7.0.0107 official Chinese free official version (with file + installation tutorial)
- The win10 desktop icon disappears and the toolbar turns black
- mysql指令执行sql文件
- Noah fortune plans to land on the Hong Kong Stock Exchange: the performance fell sharply in the first quarter, and once stepped on the thunder "Chengxing case"
- MySQL stored procedure exception handling error code: 1337
- 启牛学堂给的中信建投证券账户是不是真的?开户安全吗
- be based on. NETCORE development blog project starblog - (12) razor page dynamic compilation
- Cloud minimalist deployment svelte3 chat room
- Thoughts on joint primary key
- Short video live broadcast source code, use of EditText input box
猜你喜欢

AD20/Altium designer——过孔盖油

Xshell 7 (SSH Remote Terminal tool) v7.0.0109 official Chinese Version (with file + installation tutorial)

Qt Notes - qmap Custom key

Simple integration of client go gin -update
![[face recognition] matlab simulation of face recognition based on googlenet deep learning network](/img/e8/050ca85542ccbf1402b84c5dbf6f5e.png)
[face recognition] matlab simulation of face recognition based on googlenet deep learning network

Interview shock 58: Six differences among truncate, delete and drop!

WPF effect chapter 190: playing listbox again

Quartus Prime 18.0软件安装包和安装教程

Traitement des valeurs manquantes

How to do well in R & D efficiency measurement and index selection
随机推荐
Quickly master asp Net authentication framework identity - user registration
Application description of DAP fact table processing summary function
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Figure operation flow of HAMA BSP Model
When online and offline integration accelerates and information docking channels are diversified, the traditional center will not be necessary
Recommend 7 super easy-to-use terminal tools - ssh+ftp
CMB model 23 ukey is not recognized on win7
##Kibana+ELK集群日志处理
【招聘】[北京中关村/远程][TensorBase][开源数据仓库]等一群人,做一件事
MySQL string field to floating point field
redis. clients. jedis. exceptions. JedisDataException ERR invalid password.
Quartus Prime 18.0软件安装包和安装教程
A new mode of enterprise software development: low code
mysql指令执行sql文件
有同学问PHP要学什么框架?
[mysql] install multiple MySQL versions on one Windows computer
Is flush easy to open an account? Is it safe to open an account online?
Seeing the big from the small: a simple example of domain modeling, understanding "domain driven".
WPF achieves star effect
It may be the most comprehensive Matplotlib visualization tutorial in the whole network