Skip to content

Commit 901a721

Browse files
committed
Extract Method for Parsing Cache Value
1 parent 9aebda7 commit 901a721

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

src/main/java/org/apache/ibatis/executor/BaseExecutor.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,8 @@ public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBo
206206
// mimic DefaultParameterHandler logic
207207
for (ParameterMapping parameterMapping : parameterMappings) {
208208
if (parameterMapping.getMode() != ParameterMode.OUT) {
209-
Object value;
210209
String propertyName = parameterMapping.getProperty();
211-
if (boundSql.hasAdditionalParameter(propertyName)) {
212-
value = boundSql.getAdditionalParameter(propertyName);
213-
} else if (parameterObject == null) {
214-
value = null;
215-
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
216-
value = parameterObject;
217-
} else {
218-
MetaObject metaObject = configuration.newMetaObject(parameterObject);
219-
value = metaObject.getValue(propertyName);
220-
}
210+
Object value = parseCacheValue(parameterObject, boundSql, typeHandlerRegistry, propertyName, configuration);
221211
cacheKey.update(value);
222212
}
223213
}
@@ -228,6 +218,23 @@ public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBo
228218
return cacheKey;
229219
}
230220

221+
public static Object parseCacheValue(Object parameterObject, BoundSql boundSql,
222+
TypeHandlerRegistry typeHandlerRegistry,
223+
String propertyName, Configuration configuration) {
224+
Object value;
225+
if (boundSql.hasAdditionalParameter(propertyName)) {
226+
value = boundSql.getAdditionalParameter(propertyName);
227+
} else if (parameterObject == null) {
228+
value = null;
229+
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
230+
value = parameterObject;
231+
} else {
232+
MetaObject metaObject = configuration.newMetaObject(parameterObject);
233+
value = metaObject.getValue(propertyName);
234+
}
235+
return value;
236+
}
237+
231238
@Override
232239
public boolean isCached(MappedStatement ms, CacheKey key) {
233240
return localCache.getObject(key) != null;

src/main/java/org/apache/ibatis/scripting/defaults/DefaultParameterHandler.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,13 +19,13 @@
1919
import java.sql.SQLException;
2020
import java.util.List;
2121

22+
import org.apache.ibatis.executor.BaseExecutor;
2223
import org.apache.ibatis.executor.ErrorContext;
2324
import org.apache.ibatis.executor.parameter.ParameterHandler;
2425
import org.apache.ibatis.mapping.BoundSql;
2526
import org.apache.ibatis.mapping.MappedStatement;
2627
import org.apache.ibatis.mapping.ParameterMapping;
2728
import org.apache.ibatis.mapping.ParameterMode;
28-
import org.apache.ibatis.reflection.MetaObject;
2929
import org.apache.ibatis.session.Configuration;
3030
import org.apache.ibatis.type.JdbcType;
3131
import org.apache.ibatis.type.TypeException;
@@ -66,18 +66,9 @@ public void setParameters(PreparedStatement ps) {
6666
for (int i = 0; i < parameterMappings.size(); i++) {
6767
ParameterMapping parameterMapping = parameterMappings.get(i);
6868
if (parameterMapping.getMode() != ParameterMode.OUT) {
69-
Object value;
7069
String propertyName = parameterMapping.getProperty();
71-
if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
72-
value = boundSql.getAdditionalParameter(propertyName);
73-
} else if (parameterObject == null) {
74-
value = null;
75-
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
76-
value = parameterObject;
77-
} else {
78-
MetaObject metaObject = configuration.newMetaObject(parameterObject);
79-
value = metaObject.getValue(propertyName);
80-
}
70+
Object value =
71+
BaseExecutor.parseCacheValue(parameterObject, boundSql, typeHandlerRegistry, propertyName, configuration);
8172
TypeHandler typeHandler = parameterMapping.getTypeHandler();
8273
JdbcType jdbcType = parameterMapping.getJdbcType();
8374
if (value == null && jdbcType == null) {

src/test/java/org/apache/ibatis/jdbc/PooledDataSourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)