20人参与 • 2025-05-29 • Javascript
jsqlparser(github:https://github.com/jsqlparser/jsqlparser)是一个java语言的sql语句解析工具,功能十分强大,它可以将sql语句解析成为java类的层次结构,还支持改写sql,常见的持久层框架mybatis-plus就采用它作为sql解析工具来实现某些功能。
可以理解为能够表示任意一种sql语句的对象,select、update、delete、insert都是它的子类,例如以下用法:
statement statement = jsqlparserglobal.parse(sql); if (statement instanceof insert) { this.processinsert((insert) statement, index, sql, obj); } else if (statement instanceof select) { this.processselect((select) statement, index, sql, obj); } else if (statement instanceof update) { this.processupdate((update) statement, index, sql, obj); } else if (statement instanceof delete) { this.processdelete((delete) statement, index, sql, obj); }
是jsqlparser库中的一个核心接口,是用于表示sql语句中的各种表达式的基类接口,通过调用对象的.tostring()
方法,就能看到具体的语句结构。
例如:
longvalue
(整数值)、stringvalue
(字符串值)、doublevalue
(浮点数值)等。column
(表示列名,如 column_name
或 table.column
)。addition
(+
)、subtraction
(-
)、multiplication
(*
)、division
(/
)等。function
(如 count(*)
、substring(str, 1, 2)
)。equalsto
(=
)、notequalsto
(<>
或 !=
)、greaterthan
(>
)、likeexpression
(like
)等。andexpression
(and
)、orexpression
(or
)、notexpression
(not
)。subselect
(如 (select ...)
)。caseexpression
(case when ... then ... end
)。castexpression
(cast(... as ...)
)、intervalexpression
(时间间隔)等。用于表示查询sql语句,有三个常见子类:plainselect,parenthesedselect,setoperationlist
用于表示更新的sql语句
获得对应表
table table = update.gettable();
获得要更新的值
list<updateset> sets = update.getupdatesets();
获取where条件
expression expression = update.getwhere()
用于表示删除的sql语句
获得对应表
table table = delete.gettable();
获取where条件
expression expression = delete.getwhere()
用于表示添加sql语句,有以下几种常见方法
获取添加的列
list<column> columns = insert.getcolumns();
获取添加的值
values values = insert.getvalues();
获取添加时冲突进行更新的结构
insert into ... values ...on duplicate key update ...
list<updateset> duplicateupdatecolumns = insert.getduplicateupdatesets();
insert select的结构,获取select
insert ... select ...
select select = insert.getselect();
用于表示最常规的那种查询结构,例如:
select...from...join...where...
获取select后面的结构
list<selectitem<?>> selectitems = plainselect.getselectitems();
获取select语句的where结构
expression where = plainselect.getwhere();
获取查询的from后的结构(表,子查询等)
fromitem fromitem = plainselect.getfromitem();
存在连接查询时,获取连接查询(left/right/inner)join后的结构
list<join> joins = plainselect.getjoins();
用于表示多个select语句通过union
,union all
连接在一起的联合查询sql对象
select...from... union all select...from... union all select...from...
将语句拆分,获取构成它的若干select
setoperationlist operationlist = (setoperationlist) selectbody; list<select> selectbodylist = operationlist.getselects();
用于表示子查询,被小括号包裹的一个查询结构,例如:
(select....from...) as t
“去括号”,得到一个plainselect
parenthesedselect parenthesedselect = (parenthesedselect) selectbody; select select = parenthesedselect.getselect();
接口,from
后面的sql结构,parenthesedselect,parenthesedfromitem,table都是它的实现
fromitem fromitem = plainselect.getfromitem(); if (fromitem instanceof table) { } else if (fromitem instanceof parenthesedselect) { } else if (fromitem instanceof parenthesedfromitem) { }
用于表示sql中的表
小括号包裹的可被查询的结构,但不是子查询,不常用,例如小括号包裹的join:
(tab1 join tab2)
用于表示select语句中,select和from之间的部分,例如:
select fun(1, 2) as a, (select x from ...) as b, name as c, exists (...) as d from t
list<selectitem<?>> selectitems = plainselect.getselectitems(); selectitems.foreach(selectitem -> { expression expression = selectitem.getexpression(); if (expression instanceof select) { } else if (expression instanceof function) { } else if (expression instanceof existsexpression) { } });
泛指比较符号:and
or
=
>=
=<
,这种结构左右连接着其他结构。equalsto,orexpression,andexpression都是它的子类。
获取左右两侧的结构:
binaryexpression expression = (binaryexpression) obj; expression left = expression.getleftexpression(); expression right = expression.getrightexpression();
x in (...)
获取右侧的结构,可能是子查询或(*,*,*...)
:
inexpression expression = (inexpression) obk; expression inexpression = expression.getrightexpression();
exists (...)
获取右侧结构
existsexpression expression = (existsexpression) obj; expression e = expression.getrightexpression() ;
not,与其他的配合使用,例如:
not in (...) not exists (...)
获取not
后面的结构,会提取出in
exists
等结构
notexpression expression = (notexpression) obj; expression e = expression.getexpression();
代表小括号()
括起来的结构
(...)
去括号,拿到括号中的结构:
parenthesis expression = (parenthesis) obj; expression e = expression.getexpression();
函数结构,通常会获取参数,对参数进行操作
fun()
expressionlist<?> parameters = function.getparameters(); if (parameters != null) { parameters.foreach(expression -> { if (expression instanceof select) { } else if (expression instanceof function) { } }); }
=
or
and
sql中连接查询的join结构,从select中获得。
获取join后的结构,一般可能是表也可能是子查询
fromitem joinitem = join.getrightitem();
判断是否为隐式内连接
join.issimple();
判断是内/左/右连接
join.isright(); join.isinner(); join.isleft();
获取join的on条件
collection<expression> originonexpressions = join.getonexpressions();
改写join的on条件
join.setonexpressions(onexpressions);
用于表示sql中的字段对象,例如从一个insert对象获取sql要添加的全部字段:name,age,tenant_id
insert into t_user (name, age, tenant_id) values ('liming', 15), ('zhaoying', 16)
list<column> columns = insert.getcolumns();
updateset是一种类似xx = xx, ...
的结构,出现在update的set
后面
update user set username = 5 where id = 1
list<updateset> sets = update.getupdatesets();
也能在insert语句处理添加的数据冲突的情况时,出现在on duplicate key update
后面
insert into table_name (col1, col2) values (val1, val2) on duplicate key update col1 = val3, col2 = col4 + 1;
list<updateset> duplicateupdatecolumns = insert.getduplicateupdatesets();
expression列表,本质上是list<expression>
,当insert语句values
后面批量跟了多组值,就能得到这种结构。
('liming', 15), ('zhaoying', 16)
values values = insert.getvalues(); expressionlist<expression> expressions = (expressionlist<expression>) values.getexpressions();
继承自expressionlist,本质上也是list<expression>
,一种带着括号的expression结构,例如获取insert语句values
后面的值就能得到这种结构
('liming', 15)
values values = insert.getvalues(); expressionlist<expression> expressions = (expressionlist<expression>) values.getexpressions(); if (expressions instanceof parenthesedexpressionlist) { // parenthesedexpressionlist } else { // expressionlist }
原文首发:https://blog.liuzijian.com/post/jsqlparser.html
附:类路径
net.sf.jsqlparser.statement.statement
net.sf.jsqlparser.statement.select.select
net.sf.jsqlparser.statement.update.update
net.sf.jsqlparser.statement.delete.delete
net.sf.jsqlparser.statement.insert.insert
net.sf.jsqlparser.schema.table
net.sf.jsqlparser.expression.expression
net.sf.jsqlparser.statement.select.parenthesedselect
net.sf.jsqlparser.statement.select.setoperationlist
net.sf.jsqlparser.statement.select.selectitem
net.sf.jsqlparser.expression.binaryexpression
net.sf.jsqlparser.expression.operators.relational.inexpression
net.sf.jsqlparser.expression.operators.relational.existsexpression
net.sf.jsqlparser.expression.notexpression
net.sf.jsqlparser.expression.parenthesis
net.sf.jsqlparser.statement.select.parenthesedfromitem
net.sf.jsqlparser.statement.select.fromitem
net.sf.jsqlparser.expression.function
net.sf.jsqlparser.expression.operators.relational.equalsto
net.sf.jsqlparser.expression.operators.conditional.orexpression
net.sf.jsqlparser.expression.operators.conditional.andexpression
net.sf.jsqlparser.statement.select.join
net.sf.jsqlparser.schema.column
net.sf.jsqlparser.expression.operators.relational.expressionlist
net.sf.jsqlparser.expression.operators.relational.parenthesedexpressionlist
到此这篇关于sql解析工具jsqlparser的文章就介绍到这了,更多相关sql解析工具jsqlparser内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论