4
4
import com .codingapi .springboot .authorization .handler .Condition ;
5
5
import com .codingapi .springboot .authorization .handler .RowHandler ;
6
6
import lombok .Getter ;
7
+ import net .sf .jsqlparser .expression .Alias ;
7
8
import net .sf .jsqlparser .expression .Expression ;
8
9
import net .sf .jsqlparser .expression .operators .conditional .AndExpression ;
9
10
import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
11
+ import net .sf .jsqlparser .schema .Column ;
10
12
import net .sf .jsqlparser .schema .Table ;
11
13
import 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 .*;
16
15
17
16
import java .sql .SQLException ;
18
17
import java .util .HashMap ;
18
+ import java .util .List ;
19
19
import java .util .Map ;
20
20
21
21
/**
@@ -29,12 +29,15 @@ public class DataPermissionSQLEnhancer {
29
29
@ Getter
30
30
private final Map <String , String > tableAlias ;
31
31
32
+ private final TableColumnAliasContext aliasContext ;
33
+
32
34
// 构造函数
33
35
public DataPermissionSQLEnhancer (String sql , RowHandler rowHandler ) {
34
36
// 如何sql中存在? 则在?后面添加空格
35
37
this .sql = sql .replaceAll ("\\ ?" , " ? " );
36
38
this .rowHandler = rowHandler ;
37
39
this .tableAlias = new HashMap <>();
40
+ this .aliasContext = new TableColumnAliasContext ();
38
41
}
39
42
40
43
// 获取增强后的SQL
@@ -44,8 +47,9 @@ public String getNewSQL() throws SQLException {
44
47
if (statement instanceof Select ) {
45
48
Select select = (Select ) statement ;
46
49
PlainSelect plainSelect = select .getPlainSelect ();
47
-
48
50
this .enhanceDataPermissionInSelect (plainSelect );
51
+ this .appendColumnAlias (plainSelect .getSelectItems ());
52
+ aliasContext .print ();
49
53
System .out .println (tableAlias );
50
54
return statement .toString ();
51
55
}
@@ -63,11 +67,13 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
63
67
// FROM 项是表
64
68
if (fromItem instanceof Table ) {
65
69
Table table = (Table ) fromItem ;
70
+ this .appendTableAlias (fromItem );
66
71
this .injectDataPermissionCondition (plainSelect , table , plainSelect .getWhere ());
67
72
}
68
73
69
74
// FROM是子查询
70
75
if (fromItem instanceof Select ) {
76
+ this .appendTableAlias (fromItem );
71
77
PlainSelect subPlainSelect = ((Select ) fromItem ).getPlainSelect ();
72
78
this .enhanceDataPermissionInSelect (subPlainSelect );
73
79
}
@@ -77,15 +83,60 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
77
83
for (Join join : plainSelect .getJoins ()) {
78
84
if (join .getRightItem () instanceof Select ) {
79
85
PlainSelect subPlainSelect = ((Select ) join .getRightItem ()).getPlainSelect ();
86
+ this .appendTableAlias (join .getRightItem ());
80
87
this .enhanceDataPermissionInSelect (subPlainSelect );
81
88
}
82
89
if (join .getRightItem () instanceof Table ) {
90
+ this .appendTableAlias (join .getRightItem ());
83
91
injectDataPermissionCondition (plainSelect , (Table ) join .getRightItem (), plainSelect .getWhere ());
84
92
}
85
93
}
86
94
}
87
95
}
88
96
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
+
89
140
// 注入数据权限条件
90
141
private void injectDataPermissionCondition (PlainSelect plainSelect , Table table , Expression where ) throws Exception {
91
142
String tableName = table .getName ();
0 commit comments