1- package com .codingapi .springboot .authorization .jdbc . proxy ;
1+ package com .codingapi .springboot .authorization .interceptor ;
22
3- import com .codingapi .springboot .authorization .interceptor .SQLInterceptState ;
4- import com .codingapi .springboot .authorization .interceptor .SQLInterceptor ;
5- import com .codingapi .springboot .authorization .interceptor .SQLInterceptorContext ;
63import lombok .Getter ;
74
85import java .sql .SQLException ;
9- import java .util .function .Supplier ;
106
117/**
128 * SQLRunningContext SQL执行拦截上下文
@@ -18,9 +14,7 @@ public class SQLRunningContext {
1814
1915 private final ThreadLocal <Boolean > skipInterceptor = ThreadLocal .withInitial (() -> false );
2016
21- private SQLRunningContext () {
22-
23- }
17+ private SQLRunningContext () {}
2418
2519 /**
2620 * 拦截SQL
@@ -29,40 +23,32 @@ private SQLRunningContext() {
2923 * @return SQLInterceptState
3024 * @throws SQLException SQLException
3125 */
32- SQLInterceptState intercept (String sql ) throws SQLException {
26+ public SQLInterceptState intercept (String sql ) throws SQLException {
3327 SQLInterceptor sqlInterceptor = SQLInterceptorContext .getInstance ().getSqlInterceptor ();
3428
3529 if (skipInterceptor .get ()) {
3630 return SQLInterceptState .unIntercept (sql );
3731 }
3832
3933 if (sqlInterceptor .beforeHandler (sql )) {
34+ // 在拦截器中执行的查询操作将不会被拦截
35+ skipInterceptor .set (true );
4036 try {
4137 String newSql = sqlInterceptor .postHandler (sql );
4238 sqlInterceptor .afterHandler (sql , newSql , null );
4339 return SQLInterceptState .intercept (sql , newSql );
4440 } catch (SQLException exception ) {
4541 sqlInterceptor .afterHandler (sql , null , exception );
4642 throw exception ;
43+ }finally {
44+ // 重置拦截器状态
45+ skipInterceptor .set (false );
4746 }
4847 }
4948 return SQLInterceptState .unIntercept (sql );
5049 }
5150
5251
53- /**
54- * 执行SQL查询 (非拦截模型执行)
55- *
56- * @param supplier 业务逻辑
57- * @param <T> T
58- * @return T
59- */
60- public <T > T run (Supplier <T > supplier ) {
61- try {
62- skipInterceptor .set (true );
63- return supplier .get ();
64- } finally {
65- skipInterceptor .set (false );
66- }
67- }
52+
53+
6854}
0 commit comments