Skip to content

Commit 0b4a7f0

Browse files
committed
Remove DynamicContext reference
It's used as a Map.
1 parent bd9bb99 commit 0b4a7f0

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/main/java/org/mybatis/scripting/thymeleaf/ThymeleafSqlSource.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.lang.reflect.InvocationTargetException;
1919
import java.util.Arrays;
2020
import java.util.Collection;
21+
import java.util.HashMap;
2122
import java.util.HashSet;
2223
import java.util.Locale;
2324
import java.util.Map;
@@ -90,16 +91,19 @@ public BoundSql getBoundSql(Object parameterObject) {
9091
processingParameterType = parameterType;
9192
}
9293

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;
9599
customVariables.put(TemporaryTakeoverKeys.CONFIGURATION, configuration);
96-
customVariables.put(TemporaryTakeoverKeys.DYNAMIC_CONTEXT, dynamicContext);
100+
customVariables.put(TemporaryTakeoverKeys.DYNAMIC_CONTEXT, bindings);
97101
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);
99103

100-
SqlSource sqlSource = sqlSourceBuilder.parse(sql, processingParameterType, dynamicContext.getBindings());
104+
SqlSource sqlSource = sqlSourceBuilder.parse(sql, processingParameterType, bindings);
101105
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
102-
dynamicContext.getBindings().forEach(boundSql::setAdditionalParameter);
106+
bindings.forEach(boundSql::setAdditionalParameter);
103107

104108
return boundSql;
105109
}
@@ -116,20 +120,20 @@ static class ContextFactory implements BiFunction<Object, Map<String, Object>, I
116120
@Override
117121
public IContext apply(Object parameter, Map<String, Object> customVariable) {
118122
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);
120124
Class<?> processingParameterType = (Class<?>) customVariable
121125
.remove(TemporaryTakeoverKeys.PROCESSING_PARAMETER_TYPE);
122126
MyBatisBindingContext bindingContext = new MyBatisBindingContext(
123127
parameter != null && configuration.getTypeHandlerRegistry().hasTypeHandler(processingParameterType));
124-
dynamicContext.bind(MyBatisBindingContext.CONTEXT_VARIABLE_NAME, bindingContext);
128+
bindings.put(MyBatisBindingContext.CONTEXT_VARIABLE_NAME, bindingContext);
125129
IContext context;
126130
if (parameter instanceof Map) {
127131
@SuppressWarnings(value = "unchecked")
128132
Map<String, Object> map = (Map<String, Object>) parameter;
129-
context = new MapBasedContext(map, dynamicContext, configuration.getVariables());
133+
context = new MapBasedContext(map, bindings, configuration.getVariables());
130134
} else {
131135
MetaClass metaClass = MetaClass.forClass(processingParameterType, configuration.getReflectorFactory());
132-
context = new MetaClassBasedContext(parameter, metaClass, processingParameterType, dynamicContext,
136+
context = new MetaClassBasedContext(parameter, metaClass, processingParameterType, bindings,
133137
configuration.getVariables());
134138
}
135139
return context;
@@ -138,15 +142,15 @@ public IContext apply(Object parameter, Map<String, Object> customVariable) {
138142

139143
private abstract static class AbstractContext implements IContext {
140144

141-
private final DynamicContext dynamicContext;
145+
private final Map<String, Object> dynamicContext;
142146
private final Properties configurationProperties;
143147
private final Set<String> variableNames;
144148

145-
private AbstractContext(DynamicContext dynamicContext, Properties configurationProperties) {
149+
private AbstractContext(Map<String, Object> dynamicContext, Properties configurationProperties) {
146150
this.dynamicContext = dynamicContext;
147151
this.configurationProperties = configurationProperties;
148152
this.variableNames = new HashSet<>();
149-
addVariableNames(dynamicContext.getBindings().keySet());
153+
addVariableNames(dynamicContext.keySet());
150154
Optional.ofNullable(configurationProperties).ifPresent(v -> addVariableNames(v.stringPropertyNames()));
151155
}
152156

@@ -183,8 +187,8 @@ public Set<String> getVariableNames() {
183187
*/
184188
@Override
185189
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);
188192
}
189193
if (configurationProperties != null && configurationProperties.containsKey(name)) {
190194
return configurationProperties.getProperty(name);
@@ -200,7 +204,7 @@ private static class MapBasedContext extends AbstractContext {
200204

201205
private final Map<String, Object> variables;
202206

203-
private MapBasedContext(Map<String, Object> parameterMap, DynamicContext dynamicContext,
207+
private MapBasedContext(Map<String, Object> parameterMap, Map<String, Object> dynamicContext,
204208
Properties configurationProperties) {
205209
super(dynamicContext, configurationProperties);
206210
this.variables = parameterMap;
@@ -224,7 +228,7 @@ private static class MetaClassBasedContext extends AbstractContext {
224228
private final Class<?> parameterType;
225229

226230
private MetaClassBasedContext(Object parameterObject, MetaClass parameterMetaClass, Class<?> parameterType,
227-
DynamicContext dynamicContext, Properties configurationProperties) {
231+
Map<String, Object> dynamicContext, Properties configurationProperties) {
228232
super(dynamicContext, configurationProperties);
229233
this.parameterObject = parameterObject;
230234
this.parameterMetaClass = parameterMetaClass;

0 commit comments

Comments
 (0)