Skip to content

Commit

Permalink
Remove DynamicContext reference
Browse files Browse the repository at this point in the history
It's used as a Map.
  • Loading branch information
harawata committed Dec 31, 2022
1 parent bd9bb99 commit 0b4a7f0
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -90,16 +91,19 @@ public BoundSql getBoundSql(Object parameterObject) {
processingParameterType = parameterType;
}

DynamicContext dynamicContext = new DynamicContext(configuration, parameterObject);
Map<String, Object> customVariables = dynamicContext.getBindings();
Map<String, Object> bindings = new HashMap<>();
bindings.put(DynamicContext.PARAMETER_OBJECT_KEY, parameterObject);
bindings.put(DynamicContext.DATABASE_ID_KEY, configuration.getDatabaseId());

Map<String, Object> customVariables = bindings;
customVariables.put(TemporaryTakeoverKeys.CONFIGURATION, configuration);
customVariables.put(TemporaryTakeoverKeys.DYNAMIC_CONTEXT, dynamicContext);
customVariables.put(TemporaryTakeoverKeys.DYNAMIC_CONTEXT, bindings);
customVariables.put(TemporaryTakeoverKeys.PROCESSING_PARAMETER_TYPE, processingParameterType);
String sql = sqlGenerator.generate(sqlTemplate, parameterObject, dynamicContext::bind, customVariables);
String sql = sqlGenerator.generate(sqlTemplate, parameterObject, bindings::put, customVariables);

SqlSource sqlSource = sqlSourceBuilder.parse(sql, processingParameterType, dynamicContext.getBindings());
SqlSource sqlSource = sqlSourceBuilder.parse(sql, processingParameterType, bindings);
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
dynamicContext.getBindings().forEach(boundSql::setAdditionalParameter);
bindings.forEach(boundSql::setAdditionalParameter);

return boundSql;
}
Expand All @@ -116,20 +120,20 @@ static class ContextFactory implements BiFunction<Object, Map<String, Object>, I
@Override
public IContext apply(Object parameter, Map<String, Object> customVariable) {
Configuration configuration = (Configuration) customVariable.remove(TemporaryTakeoverKeys.CONFIGURATION);
DynamicContext dynamicContext = (DynamicContext) customVariable.remove(TemporaryTakeoverKeys.DYNAMIC_CONTEXT);
Map<String, Object> bindings = (Map<String, Object>) customVariable.remove(TemporaryTakeoverKeys.DYNAMIC_CONTEXT);
Class<?> processingParameterType = (Class<?>) customVariable
.remove(TemporaryTakeoverKeys.PROCESSING_PARAMETER_TYPE);
MyBatisBindingContext bindingContext = new MyBatisBindingContext(
parameter != null && configuration.getTypeHandlerRegistry().hasTypeHandler(processingParameterType));
dynamicContext.bind(MyBatisBindingContext.CONTEXT_VARIABLE_NAME, bindingContext);
bindings.put(MyBatisBindingContext.CONTEXT_VARIABLE_NAME, bindingContext);
IContext context;
if (parameter instanceof Map) {
@SuppressWarnings(value = "unchecked")
Map<String, Object> map = (Map<String, Object>) parameter;
context = new MapBasedContext(map, dynamicContext, configuration.getVariables());
context = new MapBasedContext(map, bindings, configuration.getVariables());
} else {
MetaClass metaClass = MetaClass.forClass(processingParameterType, configuration.getReflectorFactory());
context = new MetaClassBasedContext(parameter, metaClass, processingParameterType, dynamicContext,
context = new MetaClassBasedContext(parameter, metaClass, processingParameterType, bindings,
configuration.getVariables());
}
return context;
Expand All @@ -138,15 +142,15 @@ public IContext apply(Object parameter, Map<String, Object> customVariable) {

private abstract static class AbstractContext implements IContext {

private final DynamicContext dynamicContext;
private final Map<String, Object> dynamicContext;
private final Properties configurationProperties;
private final Set<String> variableNames;

private AbstractContext(DynamicContext dynamicContext, Properties configurationProperties) {
private AbstractContext(Map<String, Object> dynamicContext, Properties configurationProperties) {
this.dynamicContext = dynamicContext;
this.configurationProperties = configurationProperties;
this.variableNames = new HashSet<>();
addVariableNames(dynamicContext.getBindings().keySet());
addVariableNames(dynamicContext.keySet());
Optional.ofNullable(configurationProperties).ifPresent(v -> addVariableNames(v.stringPropertyNames()));
}

Expand Down Expand Up @@ -183,8 +187,8 @@ public Set<String> getVariableNames() {
*/
@Override
public Object getVariable(String name) {
if (dynamicContext.getBindings().containsKey(name)) {
return dynamicContext.getBindings().get(name);
if (dynamicContext.containsKey(name)) {
return dynamicContext.get(name);
}
if (configurationProperties != null && configurationProperties.containsKey(name)) {
return configurationProperties.getProperty(name);
Expand All @@ -200,7 +204,7 @@ private static class MapBasedContext extends AbstractContext {

private final Map<String, Object> variables;

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

private MetaClassBasedContext(Object parameterObject, MetaClass parameterMetaClass, Class<?> parameterType,
DynamicContext dynamicContext, Properties configurationProperties) {
Map<String, Object> dynamicContext, Properties configurationProperties) {
super(dynamicContext, configurationProperties);
this.parameterObject = parameterObject;
this.parameterMetaClass = parameterMetaClass;
Expand Down

0 comments on commit 0b4a7f0

Please sign in to comment.