44import com .codingapi .springboot .authorization .handler .Condition ;
55import com .codingapi .springboot .authorization .handler .RowHandler ;
66import lombok .Getter ;
7+ import net .sf .jsqlparser .expression .Alias ;
78import net .sf .jsqlparser .expression .Expression ;
89import net .sf .jsqlparser .expression .operators .conditional .AndExpression ;
910import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
11+ import net .sf .jsqlparser .schema .Column ;
1012import net .sf .jsqlparser .schema .Table ;
1113import net .sf .jsqlparser .statement .Statement ;
12- import net .sf .jsqlparser .statement .select .FromItem ;
13- import net .sf .jsqlparser .statement .select .Join ;
14- import net .sf .jsqlparser .statement .select .PlainSelect ;
15- import net .sf .jsqlparser .statement .select .Select ;
14+ import net .sf .jsqlparser .statement .select .*;
1615
1716import java .sql .SQLException ;
1817import java .util .HashMap ;
18+ import java .util .List ;
1919import java .util .Map ;
2020
2121/**
@@ -29,12 +29,15 @@ public class DataPermissionSQLEnhancer {
2929 @ Getter
3030 private final Map <String , String > tableAlias ;
3131
32+ private final TableColumnAliasContext aliasContext ;
33+
3234 // 构造函数
3335 public DataPermissionSQLEnhancer (String sql , RowHandler rowHandler ) {
3436 // 如何sql中存在? 则在?后面添加空格
3537 this .sql = sql .replaceAll ("\\ ?" , " ? " );
3638 this .rowHandler = rowHandler ;
3739 this .tableAlias = new HashMap <>();
40+ this .aliasContext = new TableColumnAliasContext ();
3841 }
3942
4043 // 获取增强后的SQL
@@ -44,8 +47,9 @@ public String getNewSQL() throws SQLException {
4447 if (statement instanceof Select ) {
4548 Select select = (Select ) statement ;
4649 PlainSelect plainSelect = select .getPlainSelect ();
47-
4850 this .enhanceDataPermissionInSelect (plainSelect );
51+ this .appendColumnAlias (plainSelect .getSelectItems ());
52+ aliasContext .print ();
4953 System .out .println (tableAlias );
5054 return statement .toString ();
5155 }
@@ -63,11 +67,13 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
6367 // FROM 项是表
6468 if (fromItem instanceof Table ) {
6569 Table table = (Table ) fromItem ;
70+ this .appendTableAlias (fromItem );
6671 this .injectDataPermissionCondition (plainSelect , table , plainSelect .getWhere ());
6772 }
6873
6974 // FROM是子查询
7075 if (fromItem instanceof Select ) {
76+ this .appendTableAlias (fromItem );
7177 PlainSelect subPlainSelect = ((Select ) fromItem ).getPlainSelect ();
7278 this .enhanceDataPermissionInSelect (subPlainSelect );
7379 }
@@ -77,15 +83,60 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
7783 for (Join join : plainSelect .getJoins ()) {
7884 if (join .getRightItem () instanceof Select ) {
7985 PlainSelect subPlainSelect = ((Select ) join .getRightItem ()).getPlainSelect ();
86+ this .appendTableAlias (join .getRightItem ());
8087 this .enhanceDataPermissionInSelect (subPlainSelect );
8188 }
8289 if (join .getRightItem () instanceof Table ) {
90+ this .appendTableAlias (join .getRightItem ());
8391 injectDataPermissionCondition (plainSelect , (Table ) join .getRightItem (), plainSelect .getWhere ());
8492 }
8593 }
8694 }
8795 }
8896
97+
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+
89140 // 注入数据权限条件
90141 private void injectDataPermissionCondition (PlainSelect plainSelect , Table table , Expression where ) throws Exception {
91142 String tableName = table .getName ();
0 commit comments