Skip to content

Commit b3bae31

Browse files
committed
fix SelectSQLAnalyzer
1 parent a32eaa9 commit b3bae31

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/analyzer/SelectSQLAnalyzer.java

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.codingapi.springboot.authorization.handler.Condition;
44
import com.codingapi.springboot.authorization.handler.RowHandler;
5+
import lombok.Getter;
56
import net.sf.jsqlparser.expression.Expression;
67
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
78
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
@@ -13,16 +14,22 @@
1314
import net.sf.jsqlparser.statement.select.Select;
1415

1516
import java.sql.SQLException;
17+
import java.util.HashMap;
18+
import java.util.Map;
1619

1720
public class SelectSQLAnalyzer {
1821

1922
private final String sql;
2023
private final RowHandler rowHandler;
2124

25+
@Getter
26+
private final Map<String, String> tableAliasMap;
27+
2228
public SelectSQLAnalyzer(String sql, RowHandler rowHandler) {
2329
// 如何sql中存在? 则在?后面添加空格
2430
this.sql = sql.replaceAll("\\?", " ? ");
2531
this.rowHandler = rowHandler;
32+
this.tableAliasMap = new HashMap<>();
2633
}
2734

2835
public String getNewSQL() throws SQLException {
@@ -31,15 +38,20 @@ public String getNewSQL() throws SQLException {
3138
if (statement instanceof Select) {
3239
Select select = (Select) statement;
3340
PlainSelect plainSelect = select.getPlainSelect();
41+
3442
this.processFromItems(plainSelect);
3543
return statement.toString();
3644
}
45+
46+
3747
} catch (Exception e) {
3848
throw new SQLException(e);
3949
}
4050
return sql;
4151
}
4252

53+
54+
4355
private void processFromItems(PlainSelect plainSelect) throws Exception {
4456
this.addConditionToSubSelect(plainSelect);
4557

@@ -49,15 +61,34 @@ private void processFromItems(PlainSelect plainSelect) throws Exception {
4961
if (fromItem instanceof Select) {
5062
this.addConditionToSubSelect((Select) fromItem);
5163
}
64+
Expression where = plainSelect.getWhere();
5265

5366
// 处理 JOIN 子查询
5467
if (plainSelect.getJoins() != null) {
5568
for (Join join : plainSelect.getJoins()) {
5669
if (join.getRightItem() instanceof Select) {
5770
this.addConditionToSubSelect((Select) join.getRightItem());
5871
}
72+
if(join.getRightItem() instanceof Table){
73+
Table table = (Table) join.getRightItem();
74+
String tableName = table.getName();
75+
String aliaName = table.getAlias() != null ? table.getAlias().getName() : tableName;
76+
tableAliasMap.put(aliaName, tableName);
77+
Condition condition = rowHandler.handler(plainSelect.toString(), tableName, aliaName);
78+
if (condition != null) {
79+
// 添加自定义条件
80+
Expression customExpression = CCJSqlParserUtil.parseCondExpression(condition.getCondition());
81+
if (where != null) {
82+
plainSelect.setWhere(new AndExpression(customExpression, where));
83+
} else {
84+
plainSelect.setWhere(customExpression);
85+
}
86+
}
87+
}
5988
}
6089
}
90+
91+
6192
}
6293

6394

@@ -71,6 +102,7 @@ private void addConditionToSubSelect(Select subSelect) throws Exception {
71102
Table table = (Table) fromItem;
72103
String tableName = table.getName();
73104
String aliaName = table.getAlias() != null ? table.getAlias().getName() : tableName;
105+
tableAliasMap.put(aliaName, tableName);
74106
Condition condition = rowHandler.handler(selectBody.toString(), tableName, aliaName);
75107
if (condition != null) {
76108
// 添加自定义条件

springboot-starter-data-authorization/src/test/java/com/codingapi/springboot/authorization/analyzer/SelectSQLAnalyzerTest.java

+68
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,72 @@ void test3() throws SQLException {
7070
System.out.println(builder.getNewSQL());
7171
assertEquals("SELECT aue1_0.ba_org_code FROM ba03_administrative_unit aue1_0 WHERE aue1_0.id > 100 AND aue1_0.ba_org_code LIKE (? || '__') ORDER BY aue1_0.ba_org_code DESC", builder.getNewSQL());
7272
}
73+
74+
@Test
75+
void test4() throws SQLException{
76+
String sql = "SELECT\n" +
77+
"\tUNYiV.id AS '历史工作经历编号',\n" +
78+
"\tUNYiV.company_name AS '历史工作单位',\n" +
79+
"\tUNYiV.depart_name AS '历史工作部门',\n" +
80+
"\tUNYiV.post_name AS '历史工作岗位',\n" +
81+
"\tUNYiV.start_date AS '开始时间',\n" +
82+
"\tUNYiV.end_date AS '结束时间',\n" +
83+
"\towasH.员工编号 AS '员工编号',\n" +
84+
"\towasH.员工姓名 AS '员工姓名',\n" +
85+
"\towasH.员工生日 AS '员工生日',\n" +
86+
"\towasH.员工地址 AS '员工地址',\n" +
87+
"\towasH.身份证号码 AS '身份证号码',\n" +
88+
"\towasH.手机号 AS '手机号',\n" +
89+
"\towasH.部门编号 AS '部门编号',\n" +
90+
"\towasH.岗位编号 AS '岗位编号',\n" +
91+
"\towasH.任现职编号 AS '任现职编号',\n" +
92+
"\towasH.社团编号 AS '社团编号',\n" +
93+
"\towasH.社团名称 AS '社团名称',\n" +
94+
"\towasH.创建时间 AS '创建时间' \n" +
95+
"FROM\n" +
96+
"\tt_work AS pehMS,\n" +
97+
"\tt_employee AS OGwG7,\n" +
98+
"\tt_work_history AS UNYiV,\n" +
99+
"\t(\n" +
100+
"\t\tSELECT\n" +
101+
"\t\t\tWXJj8.id AS '员工编号',\n" +
102+
"\t\t\tWXJj8.NAME AS '员工姓名',\n" +
103+
"\t\t\tWXJj8.birth_date AS '员工生日',\n" +
104+
"\t\t\tWXJj8.address AS '员工地址',\n" +
105+
"\t\t\tWXJj8.id_card AS '身份证号码',\n" +
106+
"\t\t\tWXJj8.phone AS '手机号',\n" +
107+
"\t\t\tWXJj8.depart_id AS '部门编号',\n" +
108+
"\t\t\tWXJj8.post_id AS '岗位编号',\n" +
109+
"\t\t\tWXJj8.work_id AS '任现职编号',\n" +
110+
"\t\t\trnGD4.id AS '社团编号',\n" +
111+
"\t\t\trnGD4.NAME AS '社团名称',\n" +
112+
"\t\t\trnGD4.create_date AS '创建时间' \n" +
113+
"\t\tFROM\n" +
114+
"\t\t\tt_employee AS WXJj8,\n" +
115+
"\t\t\tt_league_employee AS dEj96,\n" +
116+
"\t\t\tt_league AS rnGD4 \n" +
117+
"\t\tWHERE\n" +
118+
"\t\t\tdEj96.employee_id = WXJj8.id \n" +
119+
"\t\t\tAND dEj96.league_id = rnGD4.id \n" +
120+
"\t\t\tAND 1 = 1 \n" +
121+
"\t) AS owasH \n" +
122+
"WHERE\n" +
123+
"\tUNYiV.employee_id = OGwG7.id \n" +
124+
"\tAND OGwG7.work_id = pehMS.id \n" +
125+
"\tAND owasH.任现职编号 = pehMS.id \n" +
126+
"\tAND 1 = 1";
127+
128+
129+
RowHandler rowHandler = (subSql, tableName, tableAlias) -> {
130+
if (tableName.equalsIgnoreCase("t_employee")) {
131+
String conditionTemplate = "%s.id > 100 ";
132+
return Condition.formatCondition(conditionTemplate, tableAlias);
133+
}
134+
return null;
135+
};
136+
137+
SelectSQLAnalyzer builder = new SelectSQLAnalyzer(sql, rowHandler);
138+
System.out.println(builder.getNewSQL());
139+
System.out.println(builder.getTableAliasMap());;
140+
}
73141
}

0 commit comments

Comments
 (0)