|
| 1 | +package com.codingapi.springboot.authorization.analyzer; |
| 2 | + |
| 3 | +import com.codingapi.springboot.authorization.handler.Condition; |
| 4 | +import com.codingapi.springboot.authorization.handler.RowHandler; |
| 5 | +import net.sf.jsqlparser.expression.Expression; |
| 6 | +import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
| 7 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 8 | +import net.sf.jsqlparser.schema.Table; |
| 9 | +import net.sf.jsqlparser.statement.Statement; |
| 10 | +import net.sf.jsqlparser.statement.select.FromItem; |
| 11 | +import net.sf.jsqlparser.statement.select.Join; |
| 12 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 13 | +import net.sf.jsqlparser.statement.select.Select; |
| 14 | + |
| 15 | +import java.sql.SQLException; |
| 16 | + |
| 17 | +public class SelectSQLAnalyzer { |
| 18 | + |
| 19 | + private final String sql; |
| 20 | + private final RowHandler rowHandler; |
| 21 | + |
| 22 | + public SelectSQLAnalyzer(String sql, RowHandler rowHandler) { |
| 23 | + this.sql = sql; |
| 24 | + this.rowHandler = rowHandler; |
| 25 | + } |
| 26 | + |
| 27 | + public String getNewSQL() throws SQLException { |
| 28 | + try { |
| 29 | + Statement statement = CCJSqlParserUtil.parse(sql); |
| 30 | + if (statement instanceof Select) { |
| 31 | + Select select = (Select) statement; |
| 32 | + PlainSelect plainSelect = select.getPlainSelect(); |
| 33 | + this.processFromItems(plainSelect); |
| 34 | + return statement.toString(); |
| 35 | + } |
| 36 | + } catch (Exception e) { |
| 37 | + throw new SQLException(e); |
| 38 | + } |
| 39 | + return sql; |
| 40 | + } |
| 41 | + |
| 42 | + private void processFromItems(PlainSelect plainSelect) throws Exception { |
| 43 | + this.addConditionToSubSelect(plainSelect); |
| 44 | + |
| 45 | + FromItem fromItem = plainSelect.getFromItem(); |
| 46 | + |
| 47 | + // 处理主 FROM 项(如果是子查询) |
| 48 | + if (fromItem instanceof Select) { |
| 49 | + this.addConditionToSubSelect((Select) fromItem); |
| 50 | + } |
| 51 | + |
| 52 | + // 处理 JOIN 子查询 |
| 53 | + if (plainSelect.getJoins() != null) { |
| 54 | + for (Join join : plainSelect.getJoins()) { |
| 55 | + if (join.getRightItem() instanceof Select) { |
| 56 | + this.addConditionToSubSelect((Select) join.getRightItem()); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + private void addConditionToSubSelect(Select subSelect) throws Exception { |
| 64 | + PlainSelect selectBody = subSelect.getPlainSelect(); |
| 65 | + if (selectBody != null) { |
| 66 | + // 获取 WHERE 子句 |
| 67 | + Expression where = selectBody.getWhere(); |
| 68 | + FromItem fromItem = selectBody.getFromItem(); |
| 69 | + if (fromItem instanceof Table) { |
| 70 | + Table table = (Table) fromItem; |
| 71 | + String tableName = table.getName(); |
| 72 | + String aliaName = table.getAlias() != null ? table.getAlias().getName() : null; |
| 73 | + Condition condition = rowHandler.handler(selectBody.toString(), tableName, aliaName); |
| 74 | + if (condition != null) { |
| 75 | + // 添加自定义条件 |
| 76 | + Expression customExpression = CCJSqlParserUtil.parseCondExpression(condition.getCondition()); |
| 77 | + if (where != null) { |
| 78 | + selectBody.setWhere(new AndExpression(customExpression, where)); |
| 79 | + } else { |
| 80 | + selectBody.setWhere(customExpression); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments