20
20
import java .util .Optional ;
21
21
22
22
import org .apache .ibatis .mapping .ParameterMapping ;
23
- import org .apache .ibatis .parsing .GenericTokenParser ;
24
23
import org .apache .ibatis .session .Configuration ;
25
24
26
25
/**
27
26
* @author Clinton Begin
28
27
*/
29
28
public class ForEachSqlNode implements SqlNode {
30
- public static final String ITEM_PREFIX = "__frch_" ;
31
29
32
30
private final ExpressionEvaluator evaluator ;
33
31
private final String collectionExpression ;
@@ -44,14 +42,16 @@ public class ForEachSqlNode implements SqlNode {
44
42
* @deprecated Since 3.5.9, use the {@link #ForEachSqlNode(Configuration, SqlNode, String, Boolean, String, String, String, String, String)}.
45
43
*/
46
44
@ Deprecated
47
- public ForEachSqlNode (Configuration configuration , SqlNode contents , String collectionExpression , String index , String item , String open , String close , String separator ) {
45
+ public ForEachSqlNode (Configuration configuration , SqlNode contents , String collectionExpression , String index ,
46
+ String item , String open , String close , String separator ) {
48
47
this (configuration , contents , collectionExpression , null , index , item , open , close , separator );
49
48
}
50
49
51
50
/**
52
51
* @since 3.5.9
53
52
*/
54
- public ForEachSqlNode (Configuration configuration , SqlNode contents , String collectionExpression , Boolean nullable , String index , String item , String open , String close , String separator ) {
53
+ public ForEachSqlNode (Configuration configuration , SqlNode contents , String collectionExpression , Boolean nullable ,
54
+ String index , String item , String open , String close , String separator ) {
55
55
this .evaluator = new ExpressionEvaluator ();
56
56
this .collectionExpression = collectionExpression ;
57
57
this .nullable = nullable ;
@@ -68,7 +68,7 @@ public ForEachSqlNode(Configuration configuration, SqlNode contents, String coll
68
68
public boolean apply (DynamicContext context ) {
69
69
Map <String , Object > bindings = context .getBindings ();
70
70
final Iterable <?> iterable = evaluator .evaluateIterable (collectionExpression , bindings ,
71
- Optional .ofNullable (nullable ).orElseGet (configuration ::isNullableOnForEach ));
71
+ Optional .ofNullable (nullable ).orElseGet (configuration ::isNullableOnForEach ));
72
72
if (iterable == null || !iterable .iterator ().hasNext ()) {
73
73
return true ;
74
74
}
@@ -82,18 +82,17 @@ public boolean apply(DynamicContext context) {
82
82
} else {
83
83
scopedContext = new PrefixedContext (context , separator );
84
84
}
85
- int uniqueNumber = scopedContext .getUniqueNumber ();
86
85
// Issue #709
87
86
if (o instanceof Map .Entry ) {
88
87
@ SuppressWarnings ("unchecked" )
89
88
Map .Entry <Object , Object > mapEntry = (Map .Entry <Object , Object >) o ;
90
- applyIndex (scopedContext , mapEntry .getKey (), uniqueNumber );
91
- applyItem (scopedContext , mapEntry .getValue (), uniqueNumber );
89
+ applyIndex (scopedContext , mapEntry .getKey ());
90
+ applyItem (scopedContext , mapEntry .getValue ());
92
91
} else {
93
- applyIndex (scopedContext , i , uniqueNumber );
94
- applyItem (scopedContext , o , uniqueNumber );
92
+ applyIndex (scopedContext , i );
93
+ applyItem (scopedContext , o );
95
94
}
96
- contents .apply (new FilteredDynamicContext ( configuration , scopedContext , index , item , uniqueNumber ) );
95
+ contents .apply (scopedContext );
97
96
if (first ) {
98
97
first = !((PrefixedContext ) scopedContext ).isPrefixApplied ();
99
98
}
@@ -103,17 +102,15 @@ public boolean apply(DynamicContext context) {
103
102
return true ;
104
103
}
105
104
106
- private void applyIndex (DynamicContext context , Object o , int i ) {
105
+ private void applyIndex (DynamicContext context , Object o ) {
107
106
if (index != null ) {
108
107
context .bind (index , o );
109
- context .bind (itemizeItem (index , i ), o );
110
108
}
111
109
}
112
110
113
- private void applyItem (DynamicContext context , Object o , int i ) {
111
+ private void applyItem (DynamicContext context , Object o ) {
114
112
if (item != null ) {
115
113
context .bind (item , o );
116
- context .bind (itemizeItem (item , i ), o );
117
114
}
118
115
}
119
116
@@ -129,55 +126,6 @@ private void applyClose(DynamicContext context) {
129
126
}
130
127
}
131
128
132
- private static String itemizeItem (String item , int i ) {
133
- return ITEM_PREFIX + item + "_" + i ;
134
- }
135
-
136
- private static class FilteredDynamicContext extends DynamicContext {
137
- private final DynamicContext delegate ;
138
- private final int index ;
139
- private final String itemIndex ;
140
- private final String item ;
141
-
142
- public FilteredDynamicContext (Configuration configuration ,DynamicContext delegate , String itemIndex , String item , int i ) {
143
- super (configuration , delegate .getParameterObject (), delegate .getParameterType (), delegate .isParamExists ());
144
- this .delegate = delegate ;
145
- this .index = i ;
146
- this .itemIndex = itemIndex ;
147
- this .item = item ;
148
- this .bindings .putAll (delegate .getBindings ());
149
- }
150
-
151
- @ Override
152
- public String getSql () {
153
- return delegate .getSql ();
154
- }
155
-
156
- @ Override
157
- public void appendSql (String sql ) {
158
- GenericTokenParser parser = new GenericTokenParser ("#{" , "}" , content -> {
159
- String newContent = content .replaceFirst ("^\\ s*" + item + "(?![^.,:\\ s])" , itemizeItem (item , index ));
160
- if (itemIndex != null && newContent .equals (content )) {
161
- newContent = content .replaceFirst ("^\\ s*" + itemIndex + "(?![^.,:\\ s])" , itemizeItem (itemIndex , index ));
162
- }
163
- return "#{" + newContent + "}" ;
164
- });
165
-
166
- delegate .appendSql (parser .parse (sql ));
167
- }
168
-
169
- @ Override
170
- public int getUniqueNumber () {
171
- return delegate .getUniqueNumber ();
172
- }
173
-
174
- @ Override
175
- public List <ParameterMapping > getParameterMappings () {
176
- return delegate .getParameterMappings ();
177
- }
178
- }
179
-
180
-
181
129
private class PrefixedContext extends DynamicContext {
182
130
private final DynamicContext delegate ;
183
131
private final String prefix ;
@@ -209,11 +157,6 @@ public String getSql() {
209
157
return delegate .getSql ();
210
158
}
211
159
212
- @ Override
213
- public int getUniqueNumber () {
214
- return delegate .getUniqueNumber ();
215
- }
216
-
217
160
@ Override
218
161
public List <ParameterMapping > getParameterMappings () {
219
162
return delegate .getParameterMappings ();
0 commit comments