├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── test └── java └── sql ├── AliasVisitorTest.java ├── SQLExprTest.java ├── SQLSelectGroupByTest.java ├── SQLSelectQueryTest.java ├── SqlParseTest.java ├── SqlQueryTest.java ├── StatementTest.java └── Utils.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > 本篇主要学习Druid 对Sql的语法解析。学习完之后,我们可以对任意sql进行解析,同时也可以基于AST语法树来生成sql语句。 2 | 3 | ![](https://img.springlearn.cn/blog/learn_1648308606000.png) 4 | 5 | 6 | [![](https://img.shields.io/badge/SQL%E8%A7%A3%E6%9E%90-sql--parse--example-green)](https://github.com/lxchinesszz/sql-parse-example) 7 | 8 | ## 一、AST 9 | 10 | AST是abstract syntax tree的缩写,也就是抽象语法树。和所有的Parser一样,Druid Parser会生成一个抽象语法树。 11 | 12 | 13 | 在Druid中,AST节点类型主要包括SQLObject、SQLExpr、SQLStatement三种抽象类型。 14 | 15 | ```java 16 | 17 | interface SQLObject {} 18 | interface SQLExpr extends SQLObject {} 19 | interface SQLStatement extends SQLObject {} 20 | 21 | interface SQLTableSource extends SQLObject {} 22 | class SQLSelect extends SQLObject {} 23 | class SQLSelectQueryBlock extends SQLObject {} 24 | ``` 25 | 26 | ## 二、语法树解析 27 | 28 | ## 2.1 核心类介绍 29 | 30 | ### 2.1.1 SQLStatemment DQL & DML顶级抽象 31 | 32 | - DQL 数据查询语言 select 33 | - DML 数据操纵语言 insert update delete 34 | 35 | 最常用的Statement当然是SELECT/UPDATE/DELETE/INSERT,他们分别是 36 | 37 | |核心类|说明| 38 | |:--|:--| 39 | |SQLSelectStatement|查询语句| 40 | |SQLUpdateStatement|更新语句| 41 | |SQLDeleteStatement|删除语句| 42 | |SQLInsertStatement|新增语句| 43 | 44 | ```java 45 | @Test 46 | public void statement() { 47 | // 以下全部 true 48 | System.out.println(SQLUtils.parseSingleMysqlStatement("select * from users") instanceof SQLSelectStatement); 49 | System.out.println(SQLUtils.parseSingleMysqlStatement("insert into users(id,name,age) values (1,'孙悟空',500)") instanceof SQLInsertStatement); 50 | System.out.println(SQLUtils.parseSingleMysqlStatement("update users set name = '唐僧' where id = 1 ") instanceof SQLUpdateStatement); 51 | System.out.println(SQLUtils.parseSingleMysqlStatement("delete from users where id = 1") instanceof SQLDeleteStatement); 52 | } 53 | ``` 54 | 55 | ### 2.1.2 SQLSelect SQL查询 56 | 57 | SQLSelectStatement包含一个SQLSelect,SQLSelect包含一个SQLSelectQuery。SQLSelectQuery有主要的两个派生类, 58 | 分别是SQLSelectQueryBlock(单表sql查询)和SQLUnionQuery([union查询](https://www.w3school.com.cn/sql/sql_union.asp))。 59 | 60 | ```java 61 | /** 62 | * SQLSelectStatement包含一个SQLSelect,SQLSelect包含一个SQLSelectQuery。SQLSelectQuery有主要的两个派生类, 63 | * 分别是SQLSelectQueryBlock(单表sql查询)和SQLUnionQuery(联合查询)。 64 | */ 65 | @Test 66 | public void SQLSelectQuery() { 67 | // true 68 | System.out.println(parseSQLSelectQuery("select * from users") instanceof SQLSelectQueryBlock); 69 | // true 70 | System.out.println(parseSQLSelectQuery("select name from users union select name from school") instanceof SQLUnionQuery); 71 | } 72 | 73 | public SQLSelectQuery parseSQLSelectQuery(String sql) { 74 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); 75 | SQLSelectStatement sqlSelectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 76 | SQLSelect select = sqlSelectStatement.getSelect(); 77 | return select.getQuery(); 78 | } 79 | ``` 80 | 81 | ### 2.1.3 SQLExpr 82 | 83 | SQLExpr 是有几个实现类的。 84 | 85 | 86 | ```sql 87 | select id,name,age from users where id = 1 and name = '孙悟空'; 88 | 89 | select u.id, u.name from users as u where id = 1 and name = ?; 90 | ``` 91 | 92 | 93 | |核心类|举例|说明|适用范围|快速记忆| 94 | |:--|:--|:--|:--|:--| 95 | |SQLIdentifierExpr|id,name,age|SQLIdentifierExpr|查询字段或者where条件|唯一标记| 96 | |SQLPropertyExpr|u.id,u.name|区别于SQLIdentifierExpr,适用于有别名的场景; SQLPropertyExpr.name = id, SQLPropertyExpr.owner = SQLIdentifierExpr = u)|查询字段或者where条件|有别名就是它| 97 | |SQLBinaryOpExpr|id = 1, id > 5 |SQLBinaryOpExpr(left = SQLIdentifierExpr = id ,right = SQLValuableExpr = 1)|where条件|有操作符就是它| 98 | |SQLVariantRefExpr|id = ?|变量|where条件|有变量符就是它| 99 | |SQLIntegerExpr|id = 1|数字类型|值类型| - | 100 | |SQLCharExpr|name = '孙悟空'|字符类型|值类型| - | 101 | 102 | #### 2.1.3.1 SQLBinaryOpExpr 103 | 104 | ```java 105 | /** 106 | * 操作符相关: SQLBinaryOpExpr 107 | */ 108 | @Test 109 | public void SQLBinaryOpExpr() { 110 | String sql = "select * from users where id > 1 and age = 18"; 111 | SQLSelectQuery sqlSelectQuery = Utils.parseSQLSelectQuery(sql); 112 | SQLSelectQueryBlock selectQueryBlock = Utils.cast(sqlSelectQuery, SQLSelectQueryBlock.class); 113 | SQLExpr where = selectQueryBlock.getWhere(); 114 | List conditions = where.getChildren(); 115 | // [id > 1 , age = 18] 出现了操作符所以是SQLBinaryOpExpr 116 | for (SQLObject condition : conditions) { 117 | SQLBinaryOpExpr conditionExpr = Utils.cast(condition, SQLBinaryOpExpr.class); 118 | SQLBinaryOperator operator = conditionExpr.getOperator(); 119 | SQLIdentifierExpr conditionColumn = Utils.cast(conditionExpr.getLeft(), SQLIdentifierExpr.class); 120 | SQLValuableExpr conditionColumnValue = Utils.cast(conditionExpr.getRight(), SQLValuableExpr.class); 121 | Utils.print("条件字段:{},操作符号:{},条件值:{}", conditionColumn.getName(), operator.name, conditionColumnValue); 122 | } 123 | } 124 | ``` 125 | 126 | #### 2.1.3.2 SQLVariantRefExpr 127 | 128 | ```java 129 | @Test 130 | public void SQLVariantRefExpr() { 131 | String sql = "select * from users where id = ? and name = ?"; 132 | SQLSelectQuery sqlSelectQuery = Utils.parseSQLSelectQuery(sql); 133 | SQLSelectQueryBlock selectQueryBlock = Utils.cast(sqlSelectQuery, SQLSelectQueryBlock.class); 134 | SQLExpr where = selectQueryBlock.getWhere(); 135 | List conditions = where.getChildren(); 136 | // [id = ?] 出现了变量符,所以要用SQLVariantRefExpr 137 | for (SQLObject condition : conditions) { 138 | SQLBinaryOpExpr conditionExpr = Utils.cast(condition, SQLBinaryOpExpr.class); 139 | SQLBinaryOperator operator = conditionExpr.getOperator(); 140 | SQLIdentifierExpr conditionColumn = Utils.cast(conditionExpr.getLeft(), SQLIdentifierExpr.class); 141 | SQLVariantRefExpr conditionColumnValue = Utils.cast(conditionExpr.getRight(), SQLVariantRefExpr.class); 142 | int index = conditionColumnValue.getIndex(); 143 | Utils.print("条件字段:{},操作符号:{},索引位:{}", conditionColumn.getName(), operator.name, index); 144 | } 145 | } 146 | ``` 147 | 148 | ### 2.1.4 SQLTableSource 149 | 150 | 常见的SQLTableSource包括SQLExprTableSource、SQLJoinTableSource、SQLSubqueryTableSource、SQLWithSubqueryClause.Entry 151 | 152 | 153 | |核心类|举例|说明|快速记忆| 154 | |:--|:--|:--|:--|:--| 155 | |SQLExprTableSource|select * from emp where i = 3| name = SQLIdentifierExpr = emp| 单表查询 | 156 | |SQLJoinTableSource|select * from emp e inner join org o on e.org_id = o.id| left = SQLExprTableSource(emp e),right = SQLExprTableSource(org o), condition = SQLBinaryOpExpr(e.org_id = o.id) | join 查询使用 | 157 | |SQLSubqueryTableSource|select * from (select * from temp) a|from(...)是一个SQLSubqueryTableSource|子查询语句| 158 | |SQLWithSubqueryClause| WITH RECURSIVE ancestors AS (SELECT * FROM org UNION SELECT f.* FROM org f, ancestors a WHERE f.id = a.parent_id ) SELECT * FROM ancestors; |ancestors AS (...) 是一个SQLWithSubqueryClause.Entry|with| 159 | 160 | 161 | ## 2.2 SQL语句解析示例 162 | 163 | ### 2.2.1 解析 Where 164 | 165 | 注意如果条件语句中只有一个条件,那么where就是一个 `SQLBinaryOpExpr`。 166 | 当条件大于2个,使用 `where.getChildren()` 167 | 168 | ```java 169 | /** 170 | * 判断where要 171 | * 1. 注意是SQLBinaryOpExpr(id = 1) or (u.id = 1) 需要注意是否使用了别名
172 | * 2. 注意如果只有一个查询添加 where本身就是一个SQLBinaryOpExpr,如果是多个就要用 where.getChildren()
173 | * 如果有别名: SQLPropertyExpr(name = id , ownerName = u)
174 | * 如果没别名: SQLIdentifierExpr(name = id)
175 | * 值对象: SQLValuableExpr 176 | * 177 | * @param where 条件对象 178 | */ 179 | public static void parseWhere(SQLExpr where) { 180 | if (where instanceof SQLBinaryOpExpr) { 181 | parseSQLBinaryOpExpr(cast(where, SQLBinaryOpExpr.class)); 182 | } else { 183 | List childrenList = where.getChildren(); 184 | for (SQLObject sqlObject : childrenList) { 185 | // 包含了 left 和 right 186 | SQLBinaryOpExpr conditionBinary = cast(sqlObject, SQLBinaryOpExpr.class); 187 | parseSQLBinaryOpExpr(conditionBinary); 188 | } 189 | } 190 | 191 | } 192 | 193 | public static void parseSQLBinaryOpExpr(SQLBinaryOpExpr conditionBinary) { 194 | SQLExpr conditionExpr = conditionBinary.getLeft(); 195 | SQLExpr conditionValueExpr = conditionBinary.getRight(); 196 | // 左边有别名所以是SQLPropertyExpr 197 | if (conditionExpr instanceof SQLPropertyExpr) { 198 | SQLPropertyExpr conditionColumnExpr = cast(conditionExpr, SQLPropertyExpr.class); 199 | // 右边根据类型进行转换 id是SQLIntegerExpr name是SQLCharExpr 200 | SQLValuableExpr conditionColumnValue = cast(conditionValueExpr, SQLValuableExpr.class); 201 | print("条件列名:{},条件别名:{},条件值:{}", conditionColumnExpr.getName(), conditionColumnExpr.getOwnernName(), conditionColumnValue); 202 | } 203 | // 如果没有别名 204 | if (conditionExpr instanceof SQLIdentifierExpr) { 205 | SQLIdentifierExpr conditionColumnExpr = cast(conditionExpr, SQLIdentifierExpr.class); 206 | SQLValuableExpr conditionColumnValue = cast(conditionValueExpr, SQLValuableExpr.class); 207 | print("条件列名:{},条件值:{}", conditionColumnExpr.getName(), conditionColumnValue); 208 | } 209 | } 210 | ``` 211 | 212 | ### 2.2.2 解析 SQLSelectItem 213 | 214 | 解析查询的列信息 215 | 216 | ```java 217 | /** 218 | * 解析查询字段,注意是否使用了别名.u.id as userId, u.name as userName, u.age as userAge
219 | * userId(sqlSelectItem.getAlias)
220 | * 如果有别名: u.id( id = SQLPropertyExpr.getName,u = SQLPropertyExpr.getOwnernName)
221 | * 如果没别名: id(id = SQLIdentifierExpr.name) 222 | * 223 | * @param selectColumnList 查询字段 224 | */ 225 | private void parseSQLSelectItem(List selectColumnList) { 226 | for (SQLSelectItem sqlSelectItem : selectColumnList) { 227 | // u.id as userId(selectColumnAlias) 228 | String selectColumnAlias = sqlSelectItem.getAlias(); 229 | // u.id = SQLPropertyExpr 230 | SQLExpr expr = sqlSelectItem.getExpr(); 231 | if (expr instanceof SQLPropertyExpr) { 232 | SQLPropertyExpr selectColumnExpr = cast(expr, SQLPropertyExpr.class); 233 | print("列名:{},别名:{},表别名:{}", selectColumnExpr.getName(), selectColumnAlias, selectColumnExpr.getOwnernName()); 234 | } 235 | if (expr instanceof SQLIdentifierExpr) { 236 | SQLIdentifierExpr selectColumnExpr = cast(expr, SQLIdentifierExpr.class); 237 | print("列名:{},别名:{}", selectColumnExpr.getName(), selectColumnAlias); 238 | } 239 | } 240 | } 241 | ``` 242 | 243 | ### 2.2.3 解析 SQLUpdateSetItem 244 | 245 | ```java 246 | @Test 247 | public void SQLUpdateStatement() { 248 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("update users u set u.name = '唐僧',age = 18 where u.id = 1 "); 249 | SQLUpdateStatement sqlUpdateStatement = Utils.cast(sqlStatement, SQLUpdateStatement.class); 250 | List setItems = sqlUpdateStatement.getItems(); 251 | for (SQLUpdateSetItem setItem : setItems) { 252 | SQLExpr column = setItem.getColumn(); 253 | if (column instanceof SQLPropertyExpr) { 254 | SQLPropertyExpr sqlPropertyExpr = Utils.cast(column, SQLPropertyExpr.class); 255 | SQLExpr value = setItem.getValue(); 256 | Utils.print("column:{},列owner:{},value:{}", sqlPropertyExpr.getName(), sqlPropertyExpr.getOwnernName(), value); 257 | } 258 | if (column instanceof SQLIdentifierExpr) { 259 | SQLExpr value = setItem.getValue(); 260 | Utils.print("column:{},value:{}", column, value); 261 | } 262 | } 263 | SQLExpr where = sqlUpdateStatement.getWhere(); 264 | Utils.startParse("解析where", Utils::parseWhere, where); 265 | } 266 | ``` 267 | 268 | ### 2.2.4 解析 SQLLimit 269 | 270 | ```java 271 | /** 272 | * 偏移量,只有2个值 273 | * 274 | * @param limit 限制 275 | */ 276 | private void parseLimit(SQLLimit limit) { 277 | // 偏移量 278 | SQLExpr offset = limit.getOffset(); 279 | // 便宜数量 280 | SQLExpr rowCount = limit.getRowCount(); 281 | print("偏移量:{},偏移数量:{}", offset, rowCount); 282 | } 283 | ``` 284 | 285 | 286 | ### 2.2.5 解析 SQLSelectGroupBy 287 | 288 | ```java 289 | @Test 290 | public void groupBy() { 291 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("select name,count(1) as count from users group by name,age having count > 2"); 292 | SQLSelectStatement selectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 293 | SQLSelect select = selectStatement.getSelect(); 294 | SQLSelectQueryBlock query = Utils.cast(select.getQuery(), SQLSelectQueryBlock.class); 295 | SQLSelectGroupByClause groupBy = query.getGroupBy(); 296 | List items = groupBy.getItems(); 297 | for (SQLExpr item : items) { 298 | // group by name 299 | // group by age 300 | SQLIdentifierExpr groupByColumn = Utils.cast(item, SQLIdentifierExpr.class); 301 | Utils.print("group by {}", groupByColumn); 302 | } 303 | } 304 | 305 | ``` 306 | ### 2.2.6 解析 Having 307 | 308 | ```java 309 | @Test 310 | public void having() { 311 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("select name,count(1) as count from users group by name,age having count > 2"); 312 | SQLSelectStatement selectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 313 | SQLSelect select = selectStatement.getSelect(); 314 | SQLSelectQueryBlock query = Utils.cast(select.getQuery(), SQLSelectQueryBlock.class); 315 | SQLSelectGroupByClause groupBy = query.getGroupBy(); 316 | SQLExpr having = groupBy.getHaving(); 317 | // 因为只有一个条件,所以having就是SQLBinaryOpExpr 318 | SQLBinaryOpExpr havingExpr = Utils.cast(having, SQLBinaryOpExpr.class); 319 | // 没有使用别名,所以就是SQLIdentifierExpr 320 | SQLExpr left = havingExpr.getLeft(); 321 | SQLIdentifierExpr leftExpr = Utils.cast(left, SQLIdentifierExpr.class); 322 | // 数字类型就是 323 | SQLExpr right = havingExpr.getRight(); 324 | SQLValuableExpr rightValue = Utils.cast(right, SQLValuableExpr.class); 325 | SQLBinaryOperator operator = havingExpr.getOperator(); 326 | // left:count, operator:>,right:2 327 | Utils.print("left:{}, operator:{},right:{}", leftExpr.getName(), operator.name, rightValue.getValue()); 328 | } 329 | ``` 330 | 331 | ## 三、语法树生成 332 | 333 | 前面的内容如果都搞清楚了,那么我们就能对sql进行解析,通知可以修改sql解析后的语法树,同时再将修改后的语法树,重新转换成sql 334 | 335 | ## 3.1 修改语法树 336 | 337 | ### 3.1.1 增加一个条件 338 | 339 | ```java 340 | @Test 341 | public void SQLDeleteStatement(){ 342 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("delete from users where id = 1"); 343 | SQLDeleteStatement sqlDeleteStatement = Utils.cast(sqlStatement, SQLDeleteStatement.class); 344 | sqlDeleteStatement.addCondition(SQLUtils.toSQLExpr("name = '孙悟空'")); 345 | // DELETE FROM users 346 | // WHERE id = 1 347 | // AND name = '孙悟空' 348 | System.out.println(SQLUtils.toSQLString(sqlDeleteStatement)); 349 | } 350 | ``` 351 | 352 | ### 3.1.2 修改一个条件值 353 | 354 | 将条件id = 1 修改成 id = 2 355 | 356 | ```java 357 | @Test 358 | public void SQLDeleteStatement2(){ 359 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("delete from users where id = 1"); 360 | SQLDeleteStatement sqlDeleteStatement = Utils.cast(sqlStatement, SQLDeleteStatement.class); 361 | SQLExpr where = sqlDeleteStatement.getWhere(); 362 | SQLBinaryOpExpr sqlBinaryOpExpr = Utils.cast(where, SQLBinaryOpExpr.class); 363 | // DELETE FROM users 364 | // WHERE id = 2 365 | sqlBinaryOpExpr.setRight(SQLUtils.toSQLExpr("2")); 366 | System.out.println(SQLUtils.toSQLString(sqlDeleteStatement)); 367 | } 368 | ``` 369 | 370 | ## 四、Visitor模式 371 | 372 | 访问者模式 373 | 374 | 所有的AST节点都支持Visitor模式,需要自定义遍历逻辑,可以实现相应的ASTVisitorAdapter派生类 375 | 376 | ```java 377 | public static class CustomerMySqlASTVisitorAdapter extends MySqlASTVisitorAdapter { 378 | 379 | private final Map ALIAS_MAP = new HashMap(); 380 | 381 | private final Map ALIAS_COLUMN_MAP = new HashMap(); 382 | 383 | 384 | public boolean visit(SQLExprTableSource x) { 385 | String alias = x.getAlias(); 386 | ALIAS_MAP.put(alias, x); 387 | return true; 388 | } 389 | 390 | @Override 391 | public boolean visit(MySqlSelectQueryBlock x) { 392 | List selectList = x.getSelectList(); 393 | for (SQLSelectItem sqlSelectItem : selectList) { 394 | String alias = sqlSelectItem.getAlias(); 395 | SQLExpr expr = sqlSelectItem.getExpr(); 396 | ALIAS_COLUMN_MAP.put(alias, expr); 397 | } 398 | return true; 399 | } 400 | 401 | public Map getAliasMap() { 402 | return ALIAS_MAP; 403 | } 404 | 405 | public Map getAliasColumnMap() { 406 | return ALIAS_COLUMN_MAP; 407 | } 408 | } 409 | 410 | @Test 411 | public void AliasVisitor() { 412 | String sql = "select u.id as userId, u.name as userName, age as userAge from users as u where u.id = 1 and u.name = '孙悟空' limit 2,10"; 413 | // 解析SQL 414 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); 415 | CustomerMySqlASTVisitorAdapter customerMySqlASTVisitorAdapter = new CustomerMySqlASTVisitorAdapter(); 416 | sqlStatement.accept(customerMySqlASTVisitorAdapter); 417 | // 表别名:{u=users} 418 | System.out.println("表别名:" + customerMySqlASTVisitorAdapter.getAliasMap()); 419 | // 列别名{userName=u.name, userId=u.id, userAge=age} 420 | System.out.println("列别名" + customerMySqlASTVisitorAdapter.getAliasColumnMap()); 421 | } 422 | ``` 423 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.lxchinesszz 8 | sql-parse-example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | com.alibaba 15 | druid 16 | 1.1.16 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.13.2 23 | test 24 | 25 | 26 | 27 | org.projectlombok 28 | lombok 29 | 1.18.22 30 | 31 | 32 | 33 | ch.qos.logback 34 | logback-classic 35 | 1.2.10 36 | 37 | 38 | 39 | com.h2database 40 | h2 41 | 1.3.174 42 | runtime 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/test/java/sql/AliasVisitorTest.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.SQLExpr; 5 | import com.alibaba.druid.sql.ast.SQLStatement; 6 | import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; 7 | import com.alibaba.druid.sql.ast.statement.SQLSelectItem; 8 | import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; 9 | import com.alibaba.druid.sql.ast.statement.SQLTableSource; 10 | import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; 11 | import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex; 12 | import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter; 13 | import org.junit.Test; 14 | 15 | import java.util.HashMap; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | /** 20 | * @author liuxin 21 | * 2022/3/26 23:04 22 | */ 23 | public class AliasVisitorTest { 24 | 25 | public static class CustomerMySqlASTVisitorAdapter extends MySqlASTVisitorAdapter { 26 | 27 | private final Map ALIAS_MAP = new HashMap(); 28 | 29 | private final Map ALIAS_COLUMN_MAP = new HashMap(); 30 | 31 | 32 | public boolean visit(SQLExprTableSource x) { 33 | String alias = x.getAlias(); 34 | ALIAS_MAP.put(alias, x); 35 | return true; 36 | } 37 | 38 | @Override 39 | public boolean visit(MySqlSelectQueryBlock x) { 40 | List selectList = x.getSelectList(); 41 | for (SQLSelectItem sqlSelectItem : selectList) { 42 | String alias = sqlSelectItem.getAlias(); 43 | SQLExpr expr = sqlSelectItem.getExpr(); 44 | ALIAS_COLUMN_MAP.put(alias, expr); 45 | } 46 | return true; 47 | } 48 | 49 | public Map getAliasMap() { 50 | return ALIAS_MAP; 51 | } 52 | 53 | public Map getAliasColumnMap() { 54 | return ALIAS_COLUMN_MAP; 55 | } 56 | } 57 | 58 | @Test 59 | public void AliasVisitor() { 60 | String sql = "select u.id as userId, u.name as userName, age as userAge from users as u where u.id = 1 and u.name = '孙悟空' limit 2,10"; 61 | // 解析SQL 62 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); 63 | CustomerMySqlASTVisitorAdapter customerMySqlASTVisitorAdapter = new CustomerMySqlASTVisitorAdapter(); 64 | sqlStatement.accept(customerMySqlASTVisitorAdapter); 65 | // 表别名:{u=users} 66 | System.out.println("表别名:" + customerMySqlASTVisitorAdapter.getAliasMap()); 67 | // 列别名{userName=u.name, userId=u.id, userAge=age} 68 | System.out.println("列别名" + customerMySqlASTVisitorAdapter.getAliasColumnMap()); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/sql/SQLExprTest.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.SQLExpr; 5 | import com.alibaba.druid.sql.ast.SQLObject; 6 | import com.alibaba.druid.sql.ast.SQLStatement; 7 | import com.alibaba.druid.sql.ast.expr.*; 8 | import com.alibaba.druid.sql.ast.statement.*; 9 | import org.junit.Test; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * @author liuxin 15 | * 2022/3/26 20:57 16 | */ 17 | public class SQLExprTest { 18 | 19 | /** 20 | * select id,name,age from users where id = 1 and name = '孙悟空' 21 | * - id,name,age 这里SQLExpr 22 | * - id = 1 和 name = 孙悟空 也是SQLExpr 23 | */ 24 | @Test 25 | public void SQLExpr() { 26 | String sql = "select id,u.name,age from users as u where id = 1 and name = '孙悟空'"; // 解析SQL 27 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); 28 | // 因为我们的sql是一个查询语句,所以根据我们上面的介绍就是一个SQLSelectStatement 29 | SQLSelectStatement sqlSelectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 30 | // 从查询语句顶级抽象中获取查询对象 31 | SQLSelect select = sqlSelectStatement.getSelect(); 32 | SQLSelectQuery query = select.getQuery(); 33 | // SQLSelectQuery 有两个实现 SQLSelectQueryBlock 和 SQLUnionQuery。这里我们先用SQLSelectQueryBlock举例 34 | SQLSelectQueryBlock queryBlock = Utils.cast(query, SQLSelectQueryBlock.class); 35 | // 首先我们拿到语句的from对象 36 | // 首先我们先拿到查询的字段,这里因为用到了别名 37 | List selectColumnList = queryBlock.getSelectList(); 38 | for (SQLSelectItem sqlSelectItem : selectColumnList) { 39 | // id 40 | SQLExpr expr = sqlSelectItem.getExpr(); 41 | System.out.println(expr); 42 | } 43 | // 拿到查询条件 u.id = 1 and u.name = '孙悟空' // 因为使用到了别名 44 | SQLExpr where = queryBlock.getWhere(); 45 | List childrenList = where.getChildren(); 46 | for (SQLObject sqlObject : childrenList) { 47 | System.out.println(sqlObject); 48 | } 49 | } 50 | 51 | /** 52 | * 操作符相关: SQLBinaryOpExpr 53 | */ 54 | @Test 55 | public void SQLBinaryOpExpr() { 56 | String sql = "select * from users where id > 1 and age = 18"; 57 | SQLSelectQuery sqlSelectQuery = Utils.parseSQLSelectQuery(sql); 58 | SQLSelectQueryBlock selectQueryBlock = Utils.cast(sqlSelectQuery, SQLSelectQueryBlock.class); 59 | SQLExpr where = selectQueryBlock.getWhere(); 60 | List conditions = where.getChildren(); 61 | // [id > 1 , age = 18] 出现了操作符所以是SQLBinaryOpExpr 62 | for (SQLObject condition : conditions) { 63 | SQLBinaryOpExpr conditionExpr = Utils.cast(condition, SQLBinaryOpExpr.class); 64 | SQLBinaryOperator operator = conditionExpr.getOperator(); 65 | SQLIdentifierExpr conditionColumn = Utils.cast(conditionExpr.getLeft(), SQLIdentifierExpr.class); 66 | SQLValuableExpr conditionColumnValue = Utils.cast(conditionExpr.getRight(), SQLValuableExpr.class); 67 | Utils.print("条件字段:{},操作符号:{},条件值:{}", conditionColumn.getName(), operator.name, conditionColumnValue); 68 | } 69 | } 70 | 71 | @Test 72 | public void SQLVariantRefExpr() { 73 | String sql = "select * from users where id = ? and name = ?"; 74 | SQLSelectQuery sqlSelectQuery = Utils.parseSQLSelectQuery(sql); 75 | SQLSelectQueryBlock selectQueryBlock = Utils.cast(sqlSelectQuery, SQLSelectQueryBlock.class); 76 | SQLExpr where = selectQueryBlock.getWhere(); 77 | List conditions = where.getChildren(); 78 | // [id = ?] 出现了变量符,所以要用SQLVariantRefExpr 79 | for (SQLObject condition : conditions) { 80 | SQLBinaryOpExpr conditionExpr = Utils.cast(condition, SQLBinaryOpExpr.class); 81 | SQLBinaryOperator operator = conditionExpr.getOperator(); 82 | SQLIdentifierExpr conditionColumn = Utils.cast(conditionExpr.getLeft(), SQLIdentifierExpr.class); 83 | SQLVariantRefExpr conditionColumnValue = Utils.cast(conditionExpr.getRight(), SQLVariantRefExpr.class); 84 | int index = conditionColumnValue.getIndex(); 85 | Utils.print("条件字段:{},操作符号:{},索引位:{}", conditionColumn.getName(), operator.name, index); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/test/java/sql/SQLSelectGroupByTest.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.SQLExpr; 5 | import com.alibaba.druid.sql.ast.SQLStatement; 6 | import com.alibaba.druid.sql.ast.expr.*; 7 | import com.alibaba.druid.sql.ast.statement.SQLSelect; 8 | import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause; 9 | import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; 10 | import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; 11 | import org.junit.Test; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * @author liuxin 17 | * 2022/3/26 23:37 18 | */ 19 | public class SQLSelectGroupByTest { 20 | 21 | @Test 22 | public void groupBy() { 23 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("select name,count(1) as count from users group by name,age having count > 2"); 24 | SQLSelectStatement selectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 25 | SQLSelect select = selectStatement.getSelect(); 26 | SQLSelectQueryBlock query = Utils.cast(select.getQuery(), SQLSelectQueryBlock.class); 27 | SQLSelectGroupByClause groupBy = query.getGroupBy(); 28 | List items = groupBy.getItems(); 29 | for (SQLExpr item : items) { 30 | // group by name 31 | // group by age 32 | SQLIdentifierExpr groupByColumn = Utils.cast(item, SQLIdentifierExpr.class); 33 | Utils.print("group by {}", groupByColumn); 34 | } 35 | } 36 | 37 | @Test 38 | public void having() { 39 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("select name,count(1) as count from users group by name,age having count > 2"); 40 | SQLSelectStatement selectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 41 | SQLSelect select = selectStatement.getSelect(); 42 | SQLSelectQueryBlock query = Utils.cast(select.getQuery(), SQLSelectQueryBlock.class); 43 | SQLSelectGroupByClause groupBy = query.getGroupBy(); 44 | SQLExpr having = groupBy.getHaving(); 45 | // 因为只有一个条件,所以having就是SQLBinaryOpExpr 46 | SQLBinaryOpExpr havingExpr = Utils.cast(having, SQLBinaryOpExpr.class); 47 | // 没有使用别名,所以就是SQLIdentifierExpr 48 | SQLExpr left = havingExpr.getLeft(); 49 | SQLIdentifierExpr leftExpr = Utils.cast(left, SQLIdentifierExpr.class); 50 | // 数字类型就是 51 | SQLExpr right = havingExpr.getRight(); 52 | SQLValuableExpr rightValue = Utils.cast(right, SQLValuableExpr.class); 53 | SQLBinaryOperator operator = havingExpr.getOperator(); 54 | // left:count, operator:>,right:2 55 | Utils.print("left:{}, operator:{},right:{}", leftExpr.getName(), operator.name, rightValue.getValue()); 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/sql/SQLSelectQueryTest.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.SQLStatement; 5 | import com.alibaba.druid.sql.ast.statement.*; 6 | import org.junit.Test; 7 | 8 | /** 9 | * @author liuxin 10 | * 2022/3/26 20:34 11 | */ 12 | public class SQLSelectQueryTest { 13 | 14 | /** 15 | * SQLSelectStatement包含一个SQLSelect,SQLSelect包含一个SQLSelectQuery。SQLSelectQuery有主要的两个派生类, 16 | * 分别是SQLSelectQueryBlock(单表sql查询)和SQLUnionQuery(联合查询)。 17 | */ 18 | @Test 19 | public void SQLSelectQuery() { 20 | // true 21 | System.out.println(parseSQLSelectQuery("select * from users") instanceof SQLSelectQueryBlock); 22 | // true 23 | System.out.println(parseSQLSelectQuery("select name from users union select name from school") instanceof SQLUnionQuery); 24 | } 25 | 26 | public SQLSelectQuery parseSQLSelectQuery(String sql) { 27 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); 28 | SQLSelectStatement sqlSelectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 29 | SQLSelect select = sqlSelectStatement.getSelect(); 30 | return select.getQuery(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/sql/SqlParseTest.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.SQLExpr; 5 | import com.alibaba.druid.sql.ast.SQLObject; 6 | import com.alibaba.druid.sql.ast.SQLStatement; 7 | import com.alibaba.druid.sql.ast.expr.*; 8 | import com.alibaba.druid.sql.ast.statement.*; 9 | import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; 10 | import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; 11 | import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; 12 | import com.alibaba.druid.sql.parser.SQLExprParser; 13 | import com.alibaba.druid.sql.parser.SQLParserUtils; 14 | import com.alibaba.druid.sql.parser.SQLStatementParser; 15 | import com.alibaba.druid.stat.TableStat; 16 | import com.alibaba.druid.util.JdbcConstants; 17 | import com.alibaba.druid.util.JdbcUtils; 18 | import org.junit.Test; 19 | 20 | import java.util.*; 21 | 22 | /** 23 | * @author liuxin 24 | * 2022/3/26 01:28 25 | * @link https://github.com/alibaba/druid/wiki/SQL-Parser 26 | */ 27 | public class SqlParseTest { 28 | 29 | @Test 30 | public void set() { 31 | String sql = "select * from t where t.id in (select id from users)"; 32 | List sqlStatements = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL); 33 | SQLStatement sqlStatement = sqlStatements.get(0); 34 | System.out.println(sqlStatement.getChildren()); 35 | } 36 | 37 | @Test 38 | public void set2() { 39 | String sql = "select u.id,u.name from user as u where u.id = 1 and u.name = 'xi' and u.age = ?"; 40 | // 新建 MySQL Parser 41 | SQLStatementParser parser = new MySqlStatementParser(sql); 42 | // 使用Parser解析生成AST,这里SQLStatement就是AST 43 | SQLStatement statement = parser.parseStatement(); 44 | // 使用visitor来访问AST 45 | MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); 46 | // statement.accept(visitor); 47 | // 从visitor中拿出你所关注的信息 48 | // System.out.println(visitor.getColumns()); 49 | List conditions = visitor.getConditions(); 50 | // System.out.println(conditions); 51 | if (statement instanceof SQLSelectStatement) { 52 | SQLSelectStatement sqlSelectStatement = (SQLSelectStatement) statement; 53 | SQLSelect select = sqlSelectStatement.getSelect(); 54 | System.out.println(select); 55 | SQLSelectQueryBlock queryBlock = select.getQueryBlock(); 56 | List selectList = queryBlock.getSelectList(); 57 | for (SQLSelectItem sqlSelectItem : selectList) { 58 | String alias = sqlSelectItem.getAlias(); 59 | SQLExpr expr = sqlSelectItem.getExpr(); 60 | } 61 | SQLTableSource from = queryBlock.getFrom(); 62 | System.out.println(from); 63 | SQLExpr where = queryBlock.getWhere(); 64 | System.out.println(where); 65 | List childrens = where.getChildren(); 66 | for (SQLObject children : childrens) { 67 | if (children instanceof SQLIdentifierExpr) { 68 | System.out.println(((SQLIdentifierExpr) children)); 69 | } 70 | if (children instanceof SQLBinaryOpExpr) { 71 | SQLBinaryOpExpr children1 = (SQLBinaryOpExpr) children; 72 | SQLExpr left = children1.getLeft(); 73 | if (left instanceof SQLIdentifierExpr) { 74 | System.out.println(left); 75 | } 76 | if (left instanceof SQLPropertyExpr) { 77 | SQLPropertyExpr right1 = (SQLPropertyExpr) left; 78 | System.out.println(right1); 79 | } 80 | SQLExpr right = children1.getRight(); 81 | if (right instanceof SQLIntegerExpr) { 82 | System.out.println(((SQLIntegerExpr) right)); 83 | } 84 | if (right instanceof SQLCharExpr) { 85 | System.out.println(((SQLCharExpr) right)); 86 | } 87 | // 88 | if (right instanceof SQLVariantRefExpr) { 89 | System.out.println(right); 90 | } 91 | 92 | } 93 | } 94 | } 95 | } 96 | 97 | public static T cast(Object o, Class type) { 98 | return ((T) o); 99 | } 100 | 101 | @Test 102 | public void SQLSelect() { 103 | String sql = "select id as 'userId',name as 'userName',age as 'userAge' from users where age = 18"; 104 | SQLStatementParser parser = new MySqlStatementParser(sql); 105 | SQLStatement sqlStatement = parser.parseStatement(); 106 | 107 | System.out.println(SQLUtils.toSQLString(sqlStatement, "mysql")); 108 | // db类型 109 | String dbType = sqlStatement.getDbType(); 110 | System.out.println("dbType:" + dbType); 111 | SQLSelectStatement sqlSelectStatement = cast(sqlStatement, SQLSelectStatement.class); 112 | SQLSelect select = sqlSelectStatement.getSelect(); 113 | SQLSelectQuery query = select.getQuery(); 114 | MySqlSelectQueryBlock mysqlQueryBlock = cast(query, MySqlSelectQueryBlock.class); 115 | // 这里的sql,没有用到别名,所以是一个SQLIdentifierExpr 116 | List selectList = mysqlQueryBlock.getSelectList(); 117 | for (SQLSelectItem sqlSelectItem : selectList) { 118 | String queryColumnAlias = sqlSelectItem.getAlias(); 119 | SQLExpr expr = sqlSelectItem.getExpr(); 120 | if (expr instanceof SQLIdentifierExpr) { 121 | SQLIdentifierExpr columnIdentifier = cast(expr, SQLIdentifierExpr.class); 122 | String queryColumnName = columnIdentifier.getName(); 123 | System.out.println("列名:" + queryColumnName + ",别名:" + queryColumnAlias); 124 | } 125 | } 126 | // where age = 18 127 | SQLExpr where = mysqlQueryBlock.getWhere(); 128 | List childrenList = where.getChildren(); 129 | for (SQLObject sqlObject : childrenList) { 130 | if (sqlObject instanceof SQLBinaryOpExpr){ 131 | SQLBinaryOpExpr whereItem = cast(sqlObject, SQLBinaryOpExpr.class); 132 | // 这里没有用到别名就是 SQLIdentifierExpr 133 | SQLExpr left = whereItem.getLeft(); 134 | // 数字类型是 135 | SQLExpr right = whereItem.getRight(); 136 | if (left instanceof SQLIdentifierExpr){ 137 | String name = cast(left, SQLIdentifierExpr.class).getName(); 138 | System.out.println(name); 139 | } 140 | if (right instanceof SQLIntegerExpr){ 141 | Object value = cast(right, SQLIntegerExpr.class).getValue(); 142 | System.out.println(value); 143 | } 144 | } 145 | } 146 | 147 | } 148 | 149 | @Test 150 | public void sec() { 151 | String sql = "select * from users"; 152 | Map conditions = new HashMap<>(); 153 | conditions.put("id", 12L); 154 | conditions.put("name", "liu"); 155 | System.out.println(search(sql, conditions)); 156 | } 157 | 158 | public String search(String sql, Map conditions) { 159 | List> result = new ArrayList<>(); 160 | // SQLParserUtils.createSQLStatementParser可以将sql装载到Parser里面 161 | SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcUtils.MYSQL); 162 | // parseStatementList的返回值SQLStatement本身就是druid里面的语法树对象 163 | List stmtList = parser.parseStatementList(); 164 | 165 | 166 | SQLStatement stmt = stmtList.get(0); 167 | if (stmt instanceof SQLSelectStatement) { 168 | // convert conditions to 'and' statement 169 | StringBuffer constraintsBuffer = new StringBuffer(); 170 | Set keys = conditions.keySet(); 171 | Iterator keyIter = keys.iterator(); 172 | if (keyIter.hasNext()) { 173 | constraintsBuffer.append(keyIter.next()).append(" = ?"); 174 | } 175 | while (keyIter.hasNext()) { 176 | constraintsBuffer.append(" AND ").append(keyIter.next()).append(" = ?"); 177 | } 178 | SQLExprParser constraintsParser = SQLParserUtils.createExprParser(constraintsBuffer.toString(), JdbcUtils.MYSQL); 179 | SQLExpr constraintsExpr = constraintsParser.expr(); 180 | 181 | SQLSelectStatement selectStmt = (SQLSelectStatement) stmt; 182 | // 拿到SQLSelect 通过在这里打断点看对象我们可以看出这是一个树的结构 183 | SQLSelect sqlselect = selectStmt.getSelect(); 184 | SQLSelectQueryBlock query = (SQLSelectQueryBlock) sqlselect.getQuery(); 185 | SQLExpr whereExpr = query.getWhere(); 186 | // 修改where表达式 187 | if (whereExpr == null) { 188 | query.setWhere(constraintsExpr); 189 | } else { 190 | SQLBinaryOpExpr newWhereExpr = new SQLBinaryOpExpr(whereExpr, SQLBinaryOperator.BooleanAnd, constraintsExpr); 191 | query.setWhere(newWhereExpr); 192 | } 193 | sqlselect.setQuery(query); 194 | sql = sqlselect.toString(); 195 | 196 | } 197 | return sql; 198 | } 199 | 200 | } 201 | -------------------------------------------------------------------------------- /src/test/java/sql/SqlQueryTest.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.*; 5 | import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; 6 | import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; 7 | import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; 8 | import com.alibaba.druid.sql.ast.expr.SQLValuableExpr; 9 | import com.alibaba.druid.sql.ast.statement.*; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.junit.Test; 12 | import org.slf4j.helpers.MessageFormatter; 13 | 14 | import java.util.List; 15 | import java.util.function.Consumer; 16 | 17 | /** 18 | * @author liuxin 19 | * 2022/3/26 17:41 20 | */ 21 | @Slf4j 22 | public class SqlQueryTest { 23 | 24 | @SuppressWarnings("unchecked") 25 | public static T cast(Object o, Class type) { 26 | return ((T) o); 27 | } 28 | 29 | public static void startParse(String taskName, Consumer consumer, T args) { 30 | System.out.println("---------------------" + taskName + "--------------------"); 31 | consumer.accept(args); 32 | } 33 | 34 | public static void print(String format, Object... args) { 35 | System.err.println(MessageFormatter.arrayFormat(format, args).getMessage()); 36 | } 37 | 38 | /** 39 | * 1. 简单的查询语句 40 | * 2. 包含子查询的查询语句 41 | */ 42 | @Test 43 | public void sqlQueryTest() { 44 | String sql = "select u.id as userId, u.name as userName, age as userAge from users as u where u.id = 1 and u.name = '孙悟空' limit 2,10"; 45 | // 解析SQL 46 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); 47 | // 因为我们的sql是一个查询语句,所以根据我们上面的介绍就是一个SQLSelectStatement 48 | SQLSelectStatement sqlSelectStatement = cast(sqlStatement, SQLSelectStatement.class); 49 | // 从查询语句顶级抽象中获取查询对象 50 | SQLSelect select = sqlSelectStatement.getSelect(); 51 | SQLSelectQuery query = select.getQuery(); 52 | // SQLSelectQuery 有两个实现 SQLSelectQueryBlock 和 SQLUnionQuery。这里我们先用SQLSelectQueryBlock举例 53 | SQLSelectQueryBlock queryBlock = cast(query, SQLSelectQueryBlock.class); 54 | // 首先我们拿到语句的from对象 55 | SQLTableSource from = queryBlock.getFrom(); 56 | startParse("解析From", this::parseFrom, from); 57 | // 首先我们先拿到查询的字段,这里因为用到了别名 58 | List selectColumnList = queryBlock.getSelectList(); 59 | startParse("解析SQLSelectItem", this::parseSQLSelectItem, selectColumnList); 60 | // 拿到查询条件 u.id = 1 and u.name = '孙悟空' // 因为使用到了别名 61 | SQLExpr where = queryBlock.getWhere(); 62 | startParse("解析Where", this::parseWhere, where); 63 | // 解析limit 64 | SQLLimit limit = queryBlock.getLimit(); 65 | startParse("解析SQLLimit", this::parseLimit, limit); 66 | } 67 | 68 | private void parseFrom(SQLTableSource from) { 69 | // from 对象同时有 4个实现,可以看上面的介绍.这里因为是一个最简单的查询,所以就是SQLExprTableSource 70 | SQLExprTableSource fromTableSource = cast(from, SQLExprTableSource.class); 71 | SQLName name = fromTableSource.getName(); 72 | String alias = fromTableSource.getAlias(); 73 | // 首先我们先拿到要查询的表是哪个,并且判断是否有别名 74 | print("表名:{},别名:{}", name, alias); 75 | } 76 | 77 | /** 78 | * 解析查询字段,注意是否使用了别名.u.id as userId, u.name as userName, u.age as userAge
79 | * userId(sqlSelectItem.getAlias)
80 | * 如果有别名: u.id( id = SQLPropertyExpr.getName,u = SQLPropertyExpr.getOwnernName)
81 | * 如果没别名: id(id = SQLIdentifierExpr.name) 82 | * 83 | * @param selectColumnList 查询字段 84 | */ 85 | private void parseSQLSelectItem(List selectColumnList) { 86 | for (SQLSelectItem sqlSelectItem : selectColumnList) { 87 | // u.id as userId(selectColumnAlias) 88 | String selectColumnAlias = sqlSelectItem.getAlias(); 89 | // u.id = SQLPropertyExpr 90 | SQLExpr expr = sqlSelectItem.getExpr(); 91 | if (expr instanceof SQLPropertyExpr) { 92 | SQLPropertyExpr selectColumnExpr = cast(expr, SQLPropertyExpr.class); 93 | print("列名:{},别名:{},表别名:{}", selectColumnExpr.getName(), selectColumnAlias, selectColumnExpr.getOwnernName()); 94 | } 95 | if (expr instanceof SQLIdentifierExpr) { 96 | SQLIdentifierExpr selectColumnExpr = cast(expr, SQLIdentifierExpr.class); 97 | print("列名:{},别名:{}", selectColumnExpr.getName(), selectColumnAlias); 98 | } 99 | } 100 | } 101 | 102 | /** 103 | * 判断where要 104 | * 1. 注意是SQLBinaryOpExpr(id = 1) or (u.id = 1) 需要注意是否使用了别名
105 | * 2. 注意如果只有一个查询添加 where本身就是一个SQLBinaryOpExpr,如果是多个就要用 where.getChildren() 106 | * 如果有别名: SQLPropertyExpr(name = id , ownerName = u)
107 | * 如果没别名: SQLIdentifierExpr(name = id)
108 | * 值对象: SQLValuableExpr 109 | * 110 | * @param where 条件对象 111 | */ 112 | private void parseWhere(SQLExpr where) { 113 | List childrenList = where.getChildren(); 114 | for (SQLObject sqlObject : childrenList) { 115 | // 包含了 left 和 right 116 | SQLBinaryOpExpr conditionBinary = cast(sqlObject, SQLBinaryOpExpr.class); 117 | SQLExpr conditionExpr = conditionBinary.getLeft(); 118 | SQLExpr conditionValueExpr = conditionBinary.getRight(); 119 | // 左边有别名所以是SQLPropertyExpr 120 | if (conditionExpr instanceof SQLPropertyExpr) { 121 | SQLPropertyExpr conditionColumnExpr = cast(conditionExpr, SQLPropertyExpr.class); 122 | // 右边根据类型进行转换 id是SQLIntegerExpr name是SQLCharExpr 123 | SQLValuableExpr conditionColumnValue = cast(conditionValueExpr, SQLValuableExpr.class); 124 | print("条件列名:{},条件别名:{},条件值:{}", conditionColumnExpr.getName(), conditionColumnExpr.getOwnernName(), conditionColumnValue); 125 | } 126 | // 如果没有别名 127 | if (conditionExpr instanceof SQLIdentifierExpr) { 128 | SQLIdentifierExpr conditionColumnExpr = cast(conditionExpr, SQLIdentifierExpr.class); 129 | SQLValuableExpr conditionColumnValue = cast(conditionValueExpr, SQLValuableExpr.class); 130 | print("条件列名:{},条件值:{}", conditionColumnExpr.getName(), conditionColumnValue); 131 | } 132 | } 133 | } 134 | 135 | /** 136 | * 偏移量,只有2个值 137 | * 138 | * @param limit 限制 139 | */ 140 | private void parseLimit(SQLLimit limit) { 141 | // 偏移量 142 | SQLExpr offset = limit.getOffset(); 143 | // 便宜数量 144 | SQLExpr rowCount = limit.getRowCount(); 145 | print("偏移量:{},偏移数量:{}", offset, rowCount); 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /src/test/java/sql/StatementTest.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.SQLExpr; 5 | import com.alibaba.druid.sql.ast.SQLStatement; 6 | import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; 7 | import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; 8 | import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; 9 | import com.alibaba.druid.sql.ast.statement.*; 10 | import org.junit.Test; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * @author liuxin 16 | * 2022/3/26 20:21 17 | */ 18 | public class StatementTest { 19 | 20 | @Test 21 | public void statement() { 22 | System.out.println(SQLUtils.parseSingleMysqlStatement("select * from users") instanceof SQLSelectStatement); 23 | System.out.println(SQLUtils.parseSingleMysqlStatement("insert into users(id,name,age) values (1,'孙悟空',500)") instanceof SQLInsertStatement); 24 | System.out.println(SQLUtils.parseSingleMysqlStatement("update users set name = '唐僧' where id = 1 ") instanceof SQLUpdateStatement); 25 | System.out.println(SQLUtils.parseSingleMysqlStatement("delete from users where id = 1") instanceof SQLDeleteStatement); 26 | } 27 | 28 | 29 | 30 | @Test 31 | public void SQLDeleteStatement() { 32 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("delete from users where id = 1"); 33 | SQLDeleteStatement sqlDeleteStatement = Utils.cast(sqlStatement, SQLDeleteStatement.class); 34 | sqlDeleteStatement.addCondition(SQLUtils.toSQLExpr("name = '孙悟空'")); 35 | // DELETE FROM users 36 | // WHERE id = 1 37 | // AND name = '孙悟空' 38 | System.out.println(SQLUtils.toSQLString(sqlDeleteStatement)); 39 | } 40 | 41 | 42 | @Test 43 | public void SQLDeleteStatement2() { 44 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("delete from users where id = 1"); 45 | SQLDeleteStatement sqlDeleteStatement = Utils.cast(sqlStatement, SQLDeleteStatement.class); 46 | SQLExpr where = sqlDeleteStatement.getWhere(); 47 | SQLBinaryOpExpr sqlBinaryOpExpr = Utils.cast(where, SQLBinaryOpExpr.class); 48 | // DELETE FROM users 49 | // WHERE id = 2 50 | sqlBinaryOpExpr.setRight(SQLUtils.toSQLExpr("2")); 51 | System.out.println(SQLUtils.toSQLString(sqlDeleteStatement)); 52 | } 53 | 54 | @Test 55 | public void SQLUpdateStatement() { 56 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement("update users u set u.name = '唐僧',age = 18 where u.id = 1 "); 57 | SQLUpdateStatement sqlUpdateStatement = Utils.cast(sqlStatement, SQLUpdateStatement.class); 58 | List setItems = sqlUpdateStatement.getItems(); 59 | for (SQLUpdateSetItem setItem : setItems) { 60 | SQLExpr column = setItem.getColumn(); 61 | if (column instanceof SQLPropertyExpr) { 62 | SQLPropertyExpr sqlPropertyExpr = Utils.cast(column, SQLPropertyExpr.class); 63 | SQLExpr value = setItem.getValue(); 64 | Utils.print("column:{},列owner:{},value:{}", sqlPropertyExpr.getName(), sqlPropertyExpr.getOwnernName(), value); 65 | } 66 | if (column instanceof SQLIdentifierExpr) { 67 | SQLExpr value = setItem.getValue(); 68 | Utils.print("column:{},value:{}", column, value); 69 | } 70 | } 71 | SQLExpr where = sqlUpdateStatement.getWhere(); 72 | Utils.startParse("解析where", Utils::parseWhere, where); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/sql/Utils.java: -------------------------------------------------------------------------------- 1 | package sql; 2 | 3 | import com.alibaba.druid.sql.SQLUtils; 4 | import com.alibaba.druid.sql.ast.SQLExpr; 5 | import com.alibaba.druid.sql.ast.SQLObject; 6 | import com.alibaba.druid.sql.ast.SQLStatement; 7 | import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; 8 | import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; 9 | import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; 10 | import com.alibaba.druid.sql.ast.expr.SQLValuableExpr; 11 | import com.alibaba.druid.sql.ast.statement.SQLSelect; 12 | import com.alibaba.druid.sql.ast.statement.SQLSelectQuery; 13 | import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; 14 | import org.slf4j.helpers.MessageFormatter; 15 | 16 | import java.util.List; 17 | import java.util.function.Consumer; 18 | 19 | /** 20 | * @author liuxin 21 | * 2022/3/26 20:35 22 | */ 23 | public class Utils { 24 | 25 | @SuppressWarnings("unchecked") 26 | public static T cast(Object o, Class type) { 27 | return ((T) o); 28 | } 29 | 30 | public static void startParse(String taskName, Consumer consumer, T args) { 31 | System.out.println("---------------------" + taskName + "--------------------"); 32 | consumer.accept(args); 33 | } 34 | 35 | public static void print(String format, Object... args) { 36 | System.err.println(MessageFormatter.arrayFormat(format, args).getMessage()); 37 | } 38 | 39 | public static SQLSelectQuery parseSQLSelectQuery(String sql) { 40 | SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); 41 | SQLSelectStatement sqlSelectStatement = Utils.cast(sqlStatement, SQLSelectStatement.class); 42 | SQLSelect select = sqlSelectStatement.getSelect(); 43 | return select.getQuery(); 44 | } 45 | 46 | public static void parseSQLBinaryOpExpr(SQLBinaryOpExpr conditionBinary) { 47 | SQLExpr conditionExpr = conditionBinary.getLeft(); 48 | SQLExpr conditionValueExpr = conditionBinary.getRight(); 49 | // 左边有别名所以是SQLPropertyExpr 50 | if (conditionExpr instanceof SQLPropertyExpr) { 51 | SQLPropertyExpr conditionColumnExpr = cast(conditionExpr, SQLPropertyExpr.class); 52 | // 右边根据类型进行转换 id是SQLIntegerExpr name是SQLCharExpr 53 | SQLValuableExpr conditionColumnValue = cast(conditionValueExpr, SQLValuableExpr.class); 54 | print("条件列名:{},条件别名:{},条件值:{}", conditionColumnExpr.getName(), conditionColumnExpr.getOwnernName(), conditionColumnValue); 55 | } 56 | // 如果没有别名 57 | if (conditionExpr instanceof SQLIdentifierExpr) { 58 | SQLIdentifierExpr conditionColumnExpr = cast(conditionExpr, SQLIdentifierExpr.class); 59 | SQLValuableExpr conditionColumnValue = cast(conditionValueExpr, SQLValuableExpr.class); 60 | print("条件列名:{},条件值:{}", conditionColumnExpr.getName(), conditionColumnValue); 61 | } 62 | } 63 | 64 | /** 65 | * 判断where要 66 | * 1. 注意是SQLBinaryOpExpr(id = 1) or (u.id = 1) 需要注意是否使用了别名
67 | * 2. 注意如果只有一个查询添加 where本身就是一个SQLBinaryOpExpr,如果是多个就要用 where.getChildren()
68 | * 如果有别名: SQLPropertyExpr(name = id , ownerName = u)
69 | * 如果没别名: SQLIdentifierExpr(name = id)
70 | * 值对象: SQLValuableExpr 71 | * 72 | * @param where 条件对象 73 | */ 74 | public static void parseWhere(SQLExpr where) { 75 | if (where instanceof SQLBinaryOpExpr) { 76 | parseSQLBinaryOpExpr(cast(where, SQLBinaryOpExpr.class)); 77 | } else { 78 | List childrenList = where.getChildren(); 79 | for (SQLObject sqlObject : childrenList) { 80 | // 包含了 left 和 right 81 | SQLBinaryOpExpr conditionBinary = cast(sqlObject, SQLBinaryOpExpr.class); 82 | parseSQLBinaryOpExpr(conditionBinary); 83 | } 84 | } 85 | } 86 | } 87 | --------------------------------------------------------------------------------