4
4
import com .baomidou .mybatisplus .core .toolkit .PluginUtils ;
5
5
import com .baomidou .mybatisplus .extension .handlers .AbstractSqlParserHandler ;
6
6
import lombok .extern .slf4j .Slf4j ;
7
+ import ooo .github .io .dm .convert .sql .SqlHandler ;
7
8
import org .apache .ibatis .executor .resultset .ResultSetHandler ;
8
9
import org .apache .ibatis .executor .statement .StatementHandler ;
9
10
import org .apache .ibatis .mapping .BoundSql ;
10
11
import org .apache .ibatis .plugin .*;
11
12
import org .apache .ibatis .reflection .MetaObject ;
12
13
import org .apache .ibatis .reflection .SystemMetaObject ;
14
+ import org .springframework .beans .factory .annotation .Autowired ;
13
15
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
14
16
import org .springframework .stereotype .Component ;
15
17
import org .springframework .util .ObjectUtils ;
19
21
import java .util .*;
20
22
21
23
/**
22
- * @author kaiqin
24
+ * @author fengwang26
25
+ * @version 1.0
26
+ * mybatis-plus 全局拦截器
23
27
*/
24
- @ ConditionalOnProperty (prefix = "ooo .dm.interceptor" , name = "enable" , havingValue = "true" , matchIfMissing = true )
28
+ @ ConditionalOnProperty (prefix = "iflytek .dm.interceptor" , name = "enable" , havingValue = "true" , matchIfMissing = true )
25
29
@ Intercepts ({
26
30
//SQL语句处理器
27
31
@ Signature (type = StatementHandler .class , method = "prepare" , args = {Connection .class , Integer .class }), //预备工作
36
40
@ Slf4j
37
41
public class DataScopeInterceptor extends AbstractSqlParserHandler implements Interceptor {
38
42
39
-
40
- private static final String ANTI_QUOTATION_MARKS = "`" ;
41
-
42
- /**
43
- * Mysql 的字符转换为日期 -》 Dm to_date(char,fmt)
44
- */
45
- private static final String STR_TO_DATE = "str_to_date" ;
46
-
47
- /**
48
- * Mysql 的字符转换为日期 -》 Dm to_date(char,fmt)
49
- */
50
- private static final String TO_DATE = "to_date" ;
51
-
52
-
53
- /**
54
- * Mysql 的字符转换为日期 -》 Dm to_date(char,fmt)
55
- */
56
- private static final String STR_TO_DATE_FMT = "%Y-%m-%d" ;
57
-
58
- /**
59
- * Mysql 的字符转换为日期 -》 Dm to_date(char,fmt)
60
- */
61
- private static final String TO_DATE_FMT = "YYYY-MM-DD" ;
62
-
63
- /**
64
- * Mysql 中 DATEDIFF(date1, date2) => Dm DATEDIFF(datepart,date1,date2)
65
- * Mysql 中计算时间边界 DATEDIFF(date1, date2) => Dm DATEDIFF(datepart,date1,date2)
66
- * datepart 取值:
67
- * DD:返回两个日期间隔的天数
68
- * MONTH:
69
- * WK:
70
- * MS:
71
- */
72
- private static final String DATEDIFF = "datediff" ;
73
-
74
43
/**
75
44
* 如果是identity,需要转为大写并加引号 todo
76
45
*/
77
46
private static final String IDENTITY = "identity" ;
78
47
private static final String SLASH_IDENTITY_SLASH = "\" identity\" " ;
79
48
80
- private static final String DATEDIFF_DATEPART = "datediff(DD," ;
81
- private static final String SCHEMA_TABLES = "information_schema.tables" ;
82
- private static final String SCHEMA_COLUMNS = "information_schema.columns" ;
49
+
50
+ @ Autowired
51
+ private List < SqlHandler > sqlHandlers ;
83
52
84
53
85
54
@ Override
@@ -100,21 +69,7 @@ public Object intercept(Invocation invocation) throws Throwable {
100
69
Object result = invocation .proceed ();
101
70
Object o = this .formatResultMap (result );
102
71
return this .formatResultMap (o );
103
- }
104
-
105
-
106
- //else if(handler instanceof ParameterHandler){
107
- // ParameterHandler parameterHandler = (ParameterHandler) handler;
108
- // PreparedStatement ps = (PreparedStatement) invocation.getArgs()[0];
109
- //
110
- // // 反射获取 BoundSql 对象,此对象包含生成的sql和sql的参数map映射
111
- // Field boundSqlField = parameterHandler.getClass().getDeclaredField("boundSql");
112
- // boundSqlField.setAccessible(true);
113
- // BoundSql boundSql = (BoundSql) boundSqlField.get(parameterHandler);
114
- //
115
- // List<String> paramNames = new ArrayList<>();
116
- //}
117
- else {
72
+ } else {
118
73
log .info ("无法判断Mybatis执行类:{}" , handler .getClass ().getName ());
119
74
}
120
75
@@ -124,7 +79,7 @@ public Object intercept(Invocation invocation) throws Throwable {
124
79
/**
125
80
* 处理返回结果集
126
81
*
127
- * @param result
82
+ * @param result 入参
128
83
*/
129
84
private Object formatResultMap (Object result ) {
130
85
if (ObjectUtils .isEmpty (result )) {
@@ -160,24 +115,9 @@ private Object formatResultMap(Object result) {
160
115
*/
161
116
private String modifySql (String sql ) {
162
117
String transSql = sql .toLowerCase ();
163
- //判断Sql中是否含有
164
- if (transSql .contains (ANTI_QUOTATION_MARKS )) {
165
- log .info ("达梦数据库适配-去除反引号[`]!" );
166
- transSql = transSql .replaceAll (ANTI_QUOTATION_MARKS , "\" " );
167
-
168
- }
169
- //将 MYSQL 中 str_to_date() 函数替换为达梦 to_date(),注意时间格式掩码
170
- if (transSql .contains (STR_TO_DATE )) {
171
- log .info ("达梦数据库适配-替换字符转化时间函数str_to_date()" );
172
- transSql = transSql .replaceAll (STR_TO_DATE_FMT , TO_DATE_FMT ).replaceAll (STR_TO_DATE , TO_DATE );
118
+ for (SqlHandler sqlHandler : sqlHandlers ) {
119
+ transSql = sqlHandler .handle (transSql );
173
120
}
174
-
175
- // Datediff
176
- if (transSql .contains (DATEDIFF )) {
177
- log .info ("达梦数据库适配-替换字符转化时间函数DATEDIFF()" );
178
- transSql = transSql .replaceAll (DATEDIFF + "\\ (" , DATEDIFF_DATEPART );
179
- }
180
-
181
121
return transSql ;
182
122
183
123
}
0 commit comments