33
44import com .codingapi .springboot .authorization .handler .Condition ;
55import com .codingapi .springboot .authorization .handler .RowHandler ;
6- import lombok .Getter ;
7- import net .sf .jsqlparser .expression .Alias ;
86import net .sf .jsqlparser .expression .Expression ;
97import net .sf .jsqlparser .expression .operators .conditional .AndExpression ;
108import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
11- import net .sf .jsqlparser .schema .Column ;
129import net .sf .jsqlparser .schema .Table ;
1310import net .sf .jsqlparser .statement .Statement ;
14- import net .sf .jsqlparser .statement .select .*;
11+ import net .sf .jsqlparser .statement .select .FromItem ;
12+ import net .sf .jsqlparser .statement .select .Join ;
13+ import net .sf .jsqlparser .statement .select .PlainSelect ;
14+ import net .sf .jsqlparser .statement .select .Select ;
1515
1616import java .sql .SQLException ;
17- import java .util .HashMap ;
18- import java .util .List ;
19- import java .util .Map ;
2017
2118/**
2219 * 数据权限 SQL 增强器
@@ -25,32 +22,31 @@ public class DataPermissionSQLEnhancer {
2522
2623 private final String sql ;
2724 private final RowHandler rowHandler ;
25+ private final TableColumnAliasHolder tableColumnAliasHolder ;
26+ private final Statement statement ;
2827
29- @ Getter
30- private final Map <String , String > tableAlias ;
31-
32- private final TableColumnAliasContext aliasContext ;
3328
3429 // 构造函数
35- public DataPermissionSQLEnhancer (String sql , RowHandler rowHandler ) {
36- // 如何sql中存在? 则在?后面添加空格
37- this .sql = sql .replaceAll ("\\ ?" , " ? " );
38- this .rowHandler = rowHandler ;
39- this .tableAlias = new HashMap <>();
40- this .aliasContext = new TableColumnAliasContext ();
30+ public DataPermissionSQLEnhancer (String sql , RowHandler rowHandler ) throws SQLException {
31+ try {
32+ // 如何sql中存在? 则在?后面添加空格
33+ this .sql = sql .replaceAll ("\\ ?" , " ? " );
34+ this .rowHandler = rowHandler ;
35+ this .statement = CCJSqlParserUtil .parse (this .sql );
36+ this .tableColumnAliasHolder = new TableColumnAliasHolder (statement );
37+ } catch (Exception e ) {
38+ throw new SQLException (e );
39+ }
4140 }
4241
4342 // 获取增强后的SQL
4443 public String getNewSQL () throws SQLException {
4544 try {
46- Statement statement = CCJSqlParserUtil .parse (sql );
4745 if (statement instanceof Select ) {
46+ tableColumnAliasHolder .holderAlias ();
4847 Select select = (Select ) statement ;
4948 PlainSelect plainSelect = select .getPlainSelect ();
5049 this .enhanceDataPermissionInSelect (plainSelect );
51- this .appendColumnAlias (plainSelect .getSelectItems ());
52- aliasContext .print ();
53- System .out .println (tableAlias );
5450 return statement .toString ();
5551 }
5652 } catch (Exception e ) {
@@ -59,6 +55,10 @@ public String getNewSQL() throws SQLException {
5955 return sql ;
6056 }
6157
58+ public TableColumnAliasContext getTableAlias () {
59+ return tableColumnAliasHolder .getAliasContext ();
60+ }
61+
6262
6363 // 增强 SELECT 语句
6464 private void enhanceDataPermissionInSelect (PlainSelect plainSelect ) throws Exception {
@@ -67,13 +67,11 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
6767 // FROM 项是表
6868 if (fromItem instanceof Table ) {
6969 Table table = (Table ) fromItem ;
70- this .appendTableAlias (fromItem );
7170 this .injectDataPermissionCondition (plainSelect , table , plainSelect .getWhere ());
7271 }
7372
7473 // FROM是子查询
7574 if (fromItem instanceof Select ) {
76- this .appendTableAlias (fromItem );
7775 PlainSelect subPlainSelect = ((Select ) fromItem ).getPlainSelect ();
7876 this .enhanceDataPermissionInSelect (subPlainSelect );
7977 }
@@ -83,65 +81,20 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
8381 for (Join join : plainSelect .getJoins ()) {
8482 if (join .getRightItem () instanceof Select ) {
8583 PlainSelect subPlainSelect = ((Select ) join .getRightItem ()).getPlainSelect ();
86- this .appendTableAlias (join .getRightItem ());
8784 this .enhanceDataPermissionInSelect (subPlainSelect );
8885 }
8986 if (join .getRightItem () instanceof Table ) {
90- this .appendTableAlias (join .getRightItem ());
9187 injectDataPermissionCondition (plainSelect , (Table ) join .getRightItem (), plainSelect .getWhere ());
9288 }
9389 }
9490 }
9591 }
9692
9793
98- private void appendTableAlias (FromItem fromItem ) {
99- if (fromItem instanceof Table ) {
100- Table table = (Table ) fromItem ;
101- Alias alias = table .getAlias ();
102- String aliasName = alias .getName ();
103- aliasContext .add (aliasName , table .getName ());
104- }
105- if (fromItem instanceof Select ) {
106- Select select = (Select ) fromItem ;
107- PlainSelect plainSelect = select .getPlainSelect ();
108- this .appendTableAlias (plainSelect .getFromItem ());
109- List <Join > joins = plainSelect .getJoins ();
110- if (joins != null ) {
111- for (Join join : joins ) {
112- if (join .getRightItem () instanceof Table ) {
113- this .appendTableAlias (join .getRightItem ());
114- }
115- if (join .getRightItem () instanceof Select ) {
116- this .appendTableAlias (join .getRightItem ());
117- }
118- }
119- }
120- this .appendColumnAlias (plainSelect .getSelectItems ());
121- }
122-
123- }
124-
125-
126- private void appendColumnAlias (List <SelectItem <?>> selectItems ) {
127- if (selectItems != null ) {
128- for (SelectItem <?> selectItem : selectItems ) {
129- if (selectItem .getExpression () instanceof Column ) {
130- Column column = (Column ) selectItem .getExpression ();
131- String aliasName = column .getTable ().getName ();
132- String columnName = column .getColumnName ();
133- aliasContext .add (aliasName , columnName );
134- }
135- }
136- }
137- }
138-
139-
14094 // 注入数据权限条件
14195 private void injectDataPermissionCondition (PlainSelect plainSelect , Table table , Expression where ) throws Exception {
14296 String tableName = table .getName ();
14397 String aliaName = table .getAlias () != null ? table .getAlias ().getName () : tableName ;
144- tableAlias .put (aliaName , tableName );
14598 Condition condition = rowHandler .handler (plainSelect .toString (), tableName , aliaName );
14699 if (condition != null ) {
147100 // 添加自定义条件
0 commit comments