18
18
import java .lang .reflect .InvocationTargetException ;
19
19
import java .util .Arrays ;
20
20
import java .util .Collection ;
21
+ import java .util .HashMap ;
21
22
import java .util .HashSet ;
22
23
import java .util .Locale ;
23
24
import java .util .Map ;
@@ -90,16 +91,19 @@ public BoundSql getBoundSql(Object parameterObject) {
90
91
processingParameterType = parameterType ;
91
92
}
92
93
93
- DynamicContext dynamicContext = new DynamicContext (configuration , parameterObject );
94
- Map <String , Object > customVariables = dynamicContext .getBindings ();
94
+ Map <String , Object > bindings = new HashMap <>();
95
+ bindings .put (DynamicContext .PARAMETER_OBJECT_KEY , parameterObject );
96
+ bindings .put (DynamicContext .DATABASE_ID_KEY , configuration .getDatabaseId ());
97
+
98
+ Map <String , Object > customVariables = bindings ;
95
99
customVariables .put (TemporaryTakeoverKeys .CONFIGURATION , configuration );
96
- customVariables .put (TemporaryTakeoverKeys .DYNAMIC_CONTEXT , dynamicContext );
100
+ customVariables .put (TemporaryTakeoverKeys .DYNAMIC_CONTEXT , bindings );
97
101
customVariables .put (TemporaryTakeoverKeys .PROCESSING_PARAMETER_TYPE , processingParameterType );
98
- String sql = sqlGenerator .generate (sqlTemplate , parameterObject , dynamicContext :: bind , customVariables );
102
+ String sql = sqlGenerator .generate (sqlTemplate , parameterObject , bindings :: put , customVariables );
99
103
100
- SqlSource sqlSource = sqlSourceBuilder .parse (sql , processingParameterType , dynamicContext . getBindings () );
104
+ SqlSource sqlSource = sqlSourceBuilder .parse (sql , processingParameterType , bindings );
101
105
BoundSql boundSql = sqlSource .getBoundSql (parameterObject );
102
- dynamicContext . getBindings () .forEach (boundSql ::setAdditionalParameter );
106
+ bindings .forEach (boundSql ::setAdditionalParameter );
103
107
104
108
return boundSql ;
105
109
}
@@ -116,20 +120,20 @@ static class ContextFactory implements BiFunction<Object, Map<String, Object>, I
116
120
@ Override
117
121
public IContext apply (Object parameter , Map <String , Object > customVariable ) {
118
122
Configuration configuration = (Configuration ) customVariable .remove (TemporaryTakeoverKeys .CONFIGURATION );
119
- DynamicContext dynamicContext = ( DynamicContext ) customVariable .remove (TemporaryTakeoverKeys .DYNAMIC_CONTEXT );
123
+ Map < String , Object > bindings = ( Map < String , Object > ) customVariable .remove (TemporaryTakeoverKeys .DYNAMIC_CONTEXT );
120
124
Class <?> processingParameterType = (Class <?>) customVariable
121
125
.remove (TemporaryTakeoverKeys .PROCESSING_PARAMETER_TYPE );
122
126
MyBatisBindingContext bindingContext = new MyBatisBindingContext (
123
127
parameter != null && configuration .getTypeHandlerRegistry ().hasTypeHandler (processingParameterType ));
124
- dynamicContext . bind (MyBatisBindingContext .CONTEXT_VARIABLE_NAME , bindingContext );
128
+ bindings . put (MyBatisBindingContext .CONTEXT_VARIABLE_NAME , bindingContext );
125
129
IContext context ;
126
130
if (parameter instanceof Map ) {
127
131
@ SuppressWarnings (value = "unchecked" )
128
132
Map <String , Object > map = (Map <String , Object >) parameter ;
129
- context = new MapBasedContext (map , dynamicContext , configuration .getVariables ());
133
+ context = new MapBasedContext (map , bindings , configuration .getVariables ());
130
134
} else {
131
135
MetaClass metaClass = MetaClass .forClass (processingParameterType , configuration .getReflectorFactory ());
132
- context = new MetaClassBasedContext (parameter , metaClass , processingParameterType , dynamicContext ,
136
+ context = new MetaClassBasedContext (parameter , metaClass , processingParameterType , bindings ,
133
137
configuration .getVariables ());
134
138
}
135
139
return context ;
@@ -138,15 +142,15 @@ public IContext apply(Object parameter, Map<String, Object> customVariable) {
138
142
139
143
private abstract static class AbstractContext implements IContext {
140
144
141
- private final DynamicContext dynamicContext ;
145
+ private final Map < String , Object > dynamicContext ;
142
146
private final Properties configurationProperties ;
143
147
private final Set <String > variableNames ;
144
148
145
- private AbstractContext (DynamicContext dynamicContext , Properties configurationProperties ) {
149
+ private AbstractContext (Map < String , Object > dynamicContext , Properties configurationProperties ) {
146
150
this .dynamicContext = dynamicContext ;
147
151
this .configurationProperties = configurationProperties ;
148
152
this .variableNames = new HashSet <>();
149
- addVariableNames (dynamicContext .getBindings (). keySet ());
153
+ addVariableNames (dynamicContext .keySet ());
150
154
Optional .ofNullable (configurationProperties ).ifPresent (v -> addVariableNames (v .stringPropertyNames ()));
151
155
}
152
156
@@ -183,8 +187,8 @@ public Set<String> getVariableNames() {
183
187
*/
184
188
@ Override
185
189
public Object getVariable (String name ) {
186
- if (dynamicContext .getBindings (). containsKey (name )) {
187
- return dynamicContext .getBindings (). get (name );
190
+ if (dynamicContext .containsKey (name )) {
191
+ return dynamicContext .get (name );
188
192
}
189
193
if (configurationProperties != null && configurationProperties .containsKey (name )) {
190
194
return configurationProperties .getProperty (name );
@@ -200,7 +204,7 @@ private static class MapBasedContext extends AbstractContext {
200
204
201
205
private final Map <String , Object > variables ;
202
206
203
- private MapBasedContext (Map <String , Object > parameterMap , DynamicContext dynamicContext ,
207
+ private MapBasedContext (Map <String , Object > parameterMap , Map < String , Object > dynamicContext ,
204
208
Properties configurationProperties ) {
205
209
super (dynamicContext , configurationProperties );
206
210
this .variables = parameterMap ;
@@ -224,7 +228,7 @@ private static class MetaClassBasedContext extends AbstractContext {
224
228
private final Class <?> parameterType ;
225
229
226
230
private MetaClassBasedContext (Object parameterObject , MetaClass parameterMetaClass , Class <?> parameterType ,
227
- DynamicContext dynamicContext , Properties configurationProperties ) {
231
+ Map < String , Object > dynamicContext , Properties configurationProperties ) {
228
232
super (dynamicContext , configurationProperties );
229
233
this .parameterObject = parameterObject ;
230
234
this .parameterMetaClass = parameterMetaClass ;
0 commit comments