• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / mybatis-3 / 4017

02 Dec 2026 01:21PM UTC coverage: 87.564% (+0.1%) from 87.454%
4017

Pull #3809

github

web-flow
Merge de6182685 into 19b82e6ae
Pull Request #3809: Fix JDBC array cleanup when binding or extraction fails

3907 of 4711 branches covered (82.93%)

22 of 22 new or added lines in 1 file covered. (100.0%)

152 existing lines in 10 files now uncovered.

10034 of 11459 relevant lines covered (87.56%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.58
/src/main/java/org/apache/ibatis/scripting/defaults/DefaultParameterHandler.java
1
/*
2
 *    Copyright 2009-2026 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.apache.ibatis.scripting.defaults;
17

18
import java.lang.reflect.Type;
19
import java.sql.ParameterMetaData;
20
import java.sql.PreparedStatement;
21
import java.sql.SQLException;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Map.Entry;
25

26
import org.apache.ibatis.binding.MapperMethod.ParamMap;
27
import org.apache.ibatis.executor.ErrorContext;
28
import org.apache.ibatis.executor.parameter.ParameterHandler;
29
import org.apache.ibatis.mapping.BoundSql;
30
import org.apache.ibatis.mapping.MappedStatement;
31
import org.apache.ibatis.mapping.ParameterMapping;
32
import org.apache.ibatis.mapping.ParameterMode;
33
import org.apache.ibatis.reflection.MetaClass;
34
import org.apache.ibatis.reflection.MetaObject;
35
import org.apache.ibatis.reflection.ParamNameResolver;
36
import org.apache.ibatis.reflection.property.PropertyTokenizer;
37
import org.apache.ibatis.session.Configuration;
38
import org.apache.ibatis.type.JdbcType;
39
import org.apache.ibatis.type.ObjectTypeHandler;
40
import org.apache.ibatis.type.TypeException;
41
import org.apache.ibatis.type.TypeHandler;
42
import org.apache.ibatis.type.TypeHandlerRegistry;
43

44
/**
45
 * @author Clinton Begin
46
 * @author Eduardo Macarron
47
 */
48
public class DefaultParameterHandler implements ParameterHandler {
49

50
  private final TypeHandlerRegistry typeHandlerRegistry;
51

52
  private final MappedStatement mappedStatement;
53
  private final Object parameterObject;
54
  private final BoundSql boundSql;
55
  private final Configuration configuration;
56

57
  private ParameterMetaData paramMetaData;
58
  private MetaObject paramMetaObject;
59
  private HashMap<Class<?>, MetaClass> metaClassCache = new HashMap<>();
1 ✔
60
  private static final ParameterMetaData NULL_PARAM_METADATA = new ParameterMetaData() {
1 ✔
61
    // @formatter:off
62
    public <T> T unwrap(Class<T> iface) throws SQLException { return null; }
×
63
    public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; }
×
64
    public boolean isSigned(int param) throws SQLException { return false; }
×
65
    public int isNullable(int param) throws SQLException { return 0; }
×
66
    public int getScale(int param) throws SQLException { return 0; }
×
67
    public int getPrecision(int param) throws SQLException { return 0; }
×
68
    public String getParameterTypeName(int param) throws SQLException { return null; }
×
69
    public int getParameterType(int param) throws SQLException { return 0; }
×
70
    public int getParameterMode(int param) throws SQLException { return 0; }
×
71
    public int getParameterCount() throws SQLException { return 0; }
×
72
    public String getParameterClassName(int param) throws SQLException { return null; }
×
73
    // @formatter:on
74
  };
75

76
  public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
1 ✔
77
    this.mappedStatement = mappedStatement;
1 ✔
78
    this.configuration = mappedStatement.getConfiguration();
1 ✔
79
    this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
1 ✔
80
    this.parameterObject = parameterObject;
1 ✔
81
    this.boundSql = boundSql;
1 ✔
82

83
    if (configuration.isIgnoreParameterMetaData()) {
1 ✔
84
      paramMetaData = NULL_PARAM_METADATA;
1 ✔
85
    }
86
  }
1 ✔
87

88
  @Override
89
  public Object getParameterObject() {
90
    return parameterObject;
1 ✔
91
  }
92

93
  @SuppressWarnings({ "rawtypes", "unchecked" })
94
  @Override
95
  public void setParameters(PreparedStatement ps) {
96
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
1 ✔
97
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
1 ✔
98
    if (parameterMappings != null) {
1 !
99
      ParamNameResolver paramNameResolver = mappedStatement.getParamNameResolver();
1 ✔
100
      for (int i = 0; i < parameterMappings.size(); i++) {
1 ✔
101
        ParameterMapping parameterMapping = parameterMappings.get(i);
1 ✔
102
        if (parameterMapping.getMode() != ParameterMode.OUT) {
1 ✔
103
          Object value;
104
          String propertyName = parameterMapping.getProperty();
1 ✔
105
          JdbcType jdbcType = parameterMapping.getJdbcType();
1 ✔
106
          JdbcType actualJdbcType = jdbcType == null ? getParamJdbcType(ps, i + 1) : jdbcType;
1 ✔
107
          Type propertyGenericType = null;
1 ✔
108
          TypeHandler typeHandler = parameterMapping.getTypeHandler();
1 ✔
109
          if (parameterMapping.hasValue()) {
1 ✔
110
            value = parameterMapping.getValue();
1 ✔
111
            if (value == null && typeHandler == null) {
1 ✔
112
              typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.getJavaType(), actualJdbcType);
1 ✔
113
            }
114
          } else if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
1 ✔
115
            value = boundSql.getAdditionalParameter(propertyName);
1 ✔
116
          } else if (parameterObject == null) {
1 ✔
117
            value = null;
1 ✔
118
          } else {
119
            Class<? extends Object> parameterClass = parameterObject.getClass();
1 ✔
120
            TypeHandler paramTypeHandler = typeHandlerRegistry.getTypeHandler(parameterClass, actualJdbcType);
1 ✔
121
            if (paramTypeHandler != null) {
1 ✔
122
              value = parameterObject;
1 ✔
123
              typeHandler = paramTypeHandler;
1 ✔
124
            } else {
125
              paramMetaObject = getParamMetaObject();
1 ✔
126
              value = paramMetaObject.getValue(propertyName);
1 ✔
127
              if (typeHandler == null) {
1 ✔
128
                if (paramNameResolver != null && ParamMap.class.equals(parameterClass)) {
1 ✔
129
                  Type actualParamType = paramNameResolver.getType(propertyName);
1 ✔
130
                  if (actualParamType instanceof Class) {
1 ✔
131
                    Class<?> actualParamClass = (Class<?>) actualParamType;
1 ✔
132
                    MetaClass metaClass = metaClassCache.computeIfAbsent(actualParamClass,
1 ✔
133
                        k -> MetaClass.forClass(k, configuration.getReflectorFactory()));
1 ✔
134
                    PropertyTokenizer propertyTokenizer = new PropertyTokenizer(propertyName);
1 ✔
135
                    String multiParamsPropertyName;
136
                    if (propertyTokenizer.hasNext()) {
1 ✔
137
                      multiParamsPropertyName = propertyTokenizer.getChildren();
1 ✔
138
                      if (metaClass.hasGetter(multiParamsPropertyName)) {
1 !
139
                        Entry<Type, Class<?>> getterType = metaClass.getGenericGetterType(multiParamsPropertyName);
1 ✔
140
                        propertyGenericType = getterType.getKey();
1 ✔
141
                      }
1 ✔
142
                    } else {
143
                      propertyGenericType = actualParamClass;
1 ✔
144
                    }
145
                  } else {
1 ✔
146
                    propertyGenericType = actualParamType;
1 ✔
147
                  }
148
                } else {
1 ✔
149
                  try {
150
                    propertyGenericType = paramMetaObject.getGenericGetterType(propertyName).getKey();
1 ✔
151
                    typeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyGenericType,
1 ✔
152
                        actualJdbcType, null);
153
                  } catch (Exception e) {
1 ✔
154
                    // Not always resolvable
155
                  }
1 ✔
156
                }
157
              }
158
            }
159
          }
160
          if (value == null) {
1 ✔
161
            if (jdbcType == null) {
1 ✔
162
              jdbcType = configuration.getJdbcTypeForNull();
1 ✔
163
            }
164
            if (typeHandler == null) {
1 ✔
165
              typeHandler = ObjectTypeHandler.INSTANCE;
1 ✔
166
            }
167
          } else if (typeHandler == null) {
1 ✔
168
            final Class<?> valueClass = value.getClass();
1 ✔
169
            if (propertyGenericType == null) {
1 ✔
170
              propertyGenericType = valueClass;
1 ✔
171
            } else {
172
              typeHandler = typeHandlerRegistry.getTypeHandler(propertyGenericType, actualJdbcType, null);
1 ✔
173
            }
174
            if (typeHandler == null) {
1 ✔
175
              // The declared type may be too generic (e.g. Object), so try the runtime type
176
              typeHandler = typeHandlerRegistry.getTypeHandler(valueClass, actualJdbcType, null);
1 ✔
177
            }
178
          }
179
          if (typeHandler == null) {
1 ✔
180
            typeHandler = typeHandlerRegistry.getTypeHandler(actualJdbcType);
1 ✔
181
          }
182
          if (typeHandler == null) {
1 ✔
183
            throw new TypeException("Could not find type handler for Java type '" + propertyGenericType.getTypeName()
1 ✔
184
                + "' nor JDBC type '" + actualJdbcType + "'");
185
          }
186
          try {
187
            typeHandler.setParameter(ps, i + 1, value, jdbcType);
1 ✔
188
          } catch (TypeException | SQLException e) {
1 ✔
189
            throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
1 ✔
190
          }
1 ✔
191
        }
192
      }
193
    }
194
  }
1 ✔
195

196
  private MetaObject getParamMetaObject() {
197
    if (paramMetaObject != null) {
1 ✔
198
      return paramMetaObject;
1 ✔
199
    }
200
    paramMetaObject = configuration.newMetaObject(parameterObject);
1 ✔
201
    return paramMetaObject;
1 ✔
202
  }
203

204
  private JdbcType getParamJdbcType(PreparedStatement ps, int paramIndex) {
205
    try {
206
      if (paramMetaData == null) {
1 ✔
207
        paramMetaData = ps.getParameterMetaData();
1 ✔
208
      }
209
      return paramMetaData == NULL_PARAM_METADATA ? null : JdbcType.forCode(paramMetaData.getParameterType(paramIndex));
1 ✔
UNCOV
210
    } catch (SQLException e) {
×
UNCOV
211
      if (paramMetaData == null) {
×
UNCOV
212
        paramMetaData = NULL_PARAM_METADATA;
×
213
      }
UNCOV
214
      return null;
×
215
    }
216
  }
217

218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc