1
1
package org .hswebframework .web .crud .events .expr ;
2
2
3
- import io .netty .util .concurrent .FastThreadLocal ;
4
3
import jakarta .annotation .Nonnull ;
5
4
import lombok .extern .slf4j .Slf4j ;
6
5
import org .hswebframework .ezorm .rdb .mapping .EntityColumnMapping ;
7
6
import org .hswebframework .web .crud .query .QueryHelperUtils ;
7
+ import org .hswebframework .web .recycler .Recycler ;
8
8
import org .springframework .context .expression .MapAccessor ;
9
9
import org .springframework .core .convert .TypeDescriptor ;
10
10
import org .springframework .expression .*;
20
20
@ Slf4j
21
21
public class SpelSqlExpressionInvoker extends AbstractSqlExpressionInvoker {
22
22
23
+ static ExtMapAccessor accessor = new ExtMapAccessor ();
24
+
23
25
protected static class SqlFunctions extends HashMap <String , Object > {
24
26
25
27
private final EntityColumnMapping mapping ;
@@ -37,9 +39,9 @@ public Object get(Object key) {
37
39
}
38
40
if (val == null ) {
39
41
val = mapping
40
- .getPropertyByColumnName (String .valueOf (key ))
41
- .map (super ::get )
42
- .orElse (null );
42
+ .getPropertyByColumnName (String .valueOf (key ))
43
+ .map (super ::get )
44
+ .orElse (null );
43
45
}
44
46
return val ;
45
47
}
@@ -82,37 +84,35 @@ public Object coalesce(Object... args) {
82
84
}
83
85
}
84
86
85
- static final FastThreadLocal <StandardEvaluationContext > SHARED_CONTEXT = new FastThreadLocal <StandardEvaluationContext >() {
86
- @ Override
87
- protected StandardEvaluationContext initialValue () {
88
- StandardEvaluationContext context = new StandardEvaluationContext ();
89
- context .addPropertyAccessor (accessor );
90
- context .addMethodResolver (new ReflectiveMethodResolver () {
91
- @ Override
92
- public MethodExecutor resolve (@ Nonnull EvaluationContext context ,
93
- @ Nonnull Object targetObject ,
94
- @ Nonnull String name ,
95
- @ Nonnull List <TypeDescriptor > argumentTypes ) throws AccessException {
96
- return super .resolve (context , targetObject , name .toLowerCase (), argumentTypes );
97
- }
98
- });
99
- context .setOperatorOverloader (new OperatorOverloader () {
100
- @ Override
101
- public boolean overridesOperation (@ Nonnull Operation operation , Object leftOperand , Object rightOperand ) throws EvaluationException {
102
- if (leftOperand instanceof Number || rightOperand instanceof Number ) {
103
- return leftOperand == null || rightOperand == null ;
104
- }
105
- return leftOperand == null && rightOperand == null ;
87
+ static final Recycler <StandardEvaluationContext > SHARED_CONTEXT = Recycler .create (() -> {
88
+ StandardEvaluationContext context = new StandardEvaluationContext ();
89
+ context .addPropertyAccessor (accessor );
90
+ context .addMethodResolver (new ReflectiveMethodResolver () {
91
+ @ Override
92
+ public MethodExecutor resolve (@ Nonnull EvaluationContext context ,
93
+ @ Nonnull Object targetObject ,
94
+ @ Nonnull String name ,
95
+ @ Nonnull List <TypeDescriptor > argumentTypes ) throws AccessException {
96
+ return super .resolve (context , targetObject , name .toLowerCase (), argumentTypes );
97
+ }
98
+ });
99
+ context .setOperatorOverloader (new OperatorOverloader () {
100
+ @ Override
101
+ public boolean overridesOperation (@ Nonnull Operation operation , Object leftOperand , Object rightOperand ) throws EvaluationException {
102
+ if (leftOperand instanceof Number || rightOperand instanceof Number ) {
103
+ return leftOperand == null || rightOperand == null ;
106
104
}
105
+ return leftOperand == null && rightOperand == null ;
106
+ }
107
107
108
- @ Override
109
- public Object operate (@ Nonnull Operation operation , Object leftOperand , Object rightOperand ) throws EvaluationException {
110
- return null ;
111
- }
112
- });
113
- return context ;
114
- }
115
- };
108
+ @ Override
109
+ public Object operate (@ Nonnull Operation operation , Object leftOperand , Object rightOperand ) throws EvaluationException {
110
+ return null ;
111
+ }
112
+ });
113
+ return context ;
114
+ }, ctx -> {
115
+ }, 512 ) ;
116
116
117
117
@ Override
118
118
protected Function3 <EntityColumnMapping , Object [], Map <String , Object >, Object > compile (String sql ) {
@@ -145,21 +145,24 @@ protected Function3<EntityColumnMapping, Object[], Map<String, Object>, Object>
145
145
object .put ("_arg" + index , parameter );
146
146
}
147
147
}
148
- StandardEvaluationContext context = SHARED_CONTEXT .get ();
149
- try {
150
- context .setRootObject (object );
151
- Object val = expression .getValue (context );
152
- errorCount .set (0 );
153
- return val ;
154
- } catch (Throwable err ) {
155
- log .warn ("invoke native sql [{}] value error" ,
156
- sql ,
157
- err );
158
- errorCount .incrementAndGet ();
159
- } finally {
160
- context .setRootObject (null );
161
- }
162
- return null ;
148
+ return SHARED_CONTEXT .doWith (
149
+ expression , object , errorCount , sql ,
150
+ (context , expr , obj , cnt , _sql ) -> {
151
+ try {
152
+ context .setRootObject (obj );
153
+ Object val = expr .getValue (context );
154
+ cnt .set (0 );
155
+ return val ;
156
+ } catch (Throwable err ) {
157
+ log .warn ("invoke native sql [{}] value error" ,
158
+ _sql ,
159
+ err );
160
+ cnt .incrementAndGet ();
161
+ } finally {
162
+ context .setRootObject (null );
163
+ }
164
+ return null ;
165
+ });
163
166
};
164
167
} catch (Throwable error ) {
165
168
return spelError (sql , error );
@@ -175,8 +178,6 @@ protected Function3<EntityColumnMapping, Object[], Map<String, Object>, Object>
175
178
return (mapping , args , data ) -> null ;
176
179
}
177
180
178
- static ExtMapAccessor accessor = new ExtMapAccessor ();
179
-
180
181
static class ExtMapAccessor extends MapAccessor {
181
182
@ Override
182
183
public boolean canRead (@ Nonnull EvaluationContext context , Object target , @ Nonnull String name ) throws AccessException {
0 commit comments