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

mybatis / mybatis-3 / 2682

29 Jan 2025 07:08PM UTC coverage: 87.101% (-0.1%) from 87.217%
2682

Pull #3379

github

web-flow
Merge 1c658c09b into e20272705
Pull Request #3379: Resolve type handler based on `java.lang.reflect.Type` instead of `Class` and respect runtime JDBC type

3812 of 4645 branches covered (82.07%)

475 of 534 new or added lines in 37 files covered. (88.95%)

26 existing lines in 5 files now uncovered.

9906 of 11373 relevant lines covered (87.1%)

0.87 hits per line

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

90.24
/src/main/java/org/apache/ibatis/executor/resultset/ResultSetWrapper.java
1
/*
2
 *    Copyright 2009-2024 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.executor.resultset;
17

18
import java.sql.ResultSet;
19
import java.sql.ResultSetMetaData;
20
import java.sql.SQLException;
21
import java.util.ArrayList;
22
import java.util.Collections;
23
import java.util.HashMap;
24
import java.util.HashSet;
25
import java.util.List;
26
import java.util.Locale;
27
import java.util.Map;
28
import java.util.Set;
29

30
import org.apache.ibatis.io.Resources;
31
import org.apache.ibatis.mapping.ResultMap;
32
import org.apache.ibatis.session.Configuration;
33
import org.apache.ibatis.type.JdbcType;
34
import org.apache.ibatis.type.ObjectTypeHandler;
35
import org.apache.ibatis.type.TypeHandler;
36
import org.apache.ibatis.type.TypeHandlerRegistry;
37
import org.apache.ibatis.type.UnknownTypeHandler;
38

39
/**
40
 * @author Iwao AVE!
41
 */
42
public class ResultSetWrapper {
43

44
  private final ResultSet resultSet;
45
  private final TypeHandlerRegistry typeHandlerRegistry;
46
  private final List<String> columnNames = new ArrayList<>();
1✔
47
  private final List<String> classNames = new ArrayList<>();
1✔
48
  private final List<JdbcType> jdbcTypes = new ArrayList<>();
1✔
49
  private final Map<String, Map<Class<?>, TypeHandler<?>>> typeHandlerMap = new HashMap<>();
1✔
50
  private final Map<String, Set<String>> mappedColumnNamesMap = new HashMap<>();
1✔
51
  private final Map<String, List<String>> unMappedColumnNamesMap = new HashMap<>();
1✔
52

53
  public ResultSetWrapper(ResultSet rs, Configuration configuration) throws SQLException {
1✔
54
    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
1✔
55
    this.resultSet = rs;
1✔
56
    final ResultSetMetaData metaData = rs.getMetaData();
1✔
57
    final int columnCount = metaData.getColumnCount();
1✔
58
    for (int i = 1; i <= columnCount; i++) {
1✔
59
      columnNames.add(configuration.isUseColumnLabel() ? metaData.getColumnLabel(i) : metaData.getColumnName(i));
1✔
60
      jdbcTypes.add(JdbcType.forCode(metaData.getColumnType(i)));
1✔
61
      classNames.add(metaData.getColumnClassName(i));
1✔
62
    }
63
  }
1✔
64

65
  public ResultSet getResultSet() {
66
    return resultSet;
1✔
67
  }
68

69
  public List<String> getColumnNames() {
70
    return this.columnNames;
1✔
71
  }
72

73
  public List<String> getClassNames() {
74
    return Collections.unmodifiableList(classNames);
1✔
75
  }
76

77
  public List<JdbcType> getJdbcTypes() {
78
    return jdbcTypes;
1✔
79
  }
80

81
  public JdbcType getJdbcType(String columnName) {
82
    for (int i = 0; i < columnNames.size(); i++) {
1✔
83
      if (columnNames.get(i).equalsIgnoreCase(columnName)) {
1✔
84
        return jdbcTypes.get(i);
1✔
85
      }
86
    }
87
    return null;
1✔
88
  }
89

90
  /**
91
   * Gets the type handler to use when reading the result set. Tries to get from the TypeHandlerRegistry by searching
92
   * for the property type. If not found it gets the column JDBC type and tries to get a handler for it.
93
   *
94
   * @param propertyType
95
   *          the property type
96
   * @param columnName
97
   *          the column name
98
   *
99
   * @return the type handler
100
   */
101
  public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
102
    TypeHandler<?> handler = null;
1✔
103
    Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
1✔
104
    if (columnHandlers == null) {
1✔
105
      columnHandlers = new HashMap<>();
1✔
106
      typeHandlerMap.put(columnName, columnHandlers);
1✔
107
    } else {
108
      handler = columnHandlers.get(propertyType);
1✔
109
    }
110
    if (handler == null) {
1✔
111
      JdbcType jdbcType = getJdbcType(columnName);
1✔
112
      handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
1✔
113
      // Replicate logic of UnknownTypeHandler#resolveTypeHandler
114
      // See issue #59 comment 10
115
      if (handler == null || handler instanceof UnknownTypeHandler) {
1!
116
        final int index = columnNames.indexOf(columnName);
1✔
117
        final Class<?> javaType = resolveClass(classNames.get(index));
1✔
118
        if (javaType != null && jdbcType != null) {
1!
119
          handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
1✔
120
        } else if (javaType != null) {
×
121
          handler = typeHandlerRegistry.getTypeHandler(javaType);
×
122
        } else if (jdbcType != null) {
×
123
          handler = typeHandlerRegistry.getTypeHandler(jdbcType);
×
124
        }
125
      }
126
      if (handler == null || handler instanceof UnknownTypeHandler) {
1!
UNCOV
127
        handler = new ObjectTypeHandler();
×
128
      }
129
      columnHandlers.put(propertyType, handler);
1✔
130
    }
131
    return handler;
1✔
132
  }
133

134
  private Class<?> resolveClass(String className) {
135
    try {
136
      // #699 className could be null
137
      if (className != null) {
1!
138
        return Resources.classForName(className);
1✔
139
      }
140
    } catch (ClassNotFoundException e) {
×
141
      // ignore
142
    }
×
143
    return null;
×
144
  }
145

146
  private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
147
    Set<String> mappedColumnNames = new HashSet<>();
1✔
148
    List<String> unmappedColumnNames = new ArrayList<>();
1✔
149
    final String upperColumnPrefix = columnPrefix == null ? null : columnPrefix.toUpperCase(Locale.ENGLISH);
1✔
150
    final Set<String> mappedColumns = prependPrefixes(resultMap.getMappedColumns(), upperColumnPrefix);
1✔
151
    for (String columnName : columnNames) {
1✔
152
      final String upperColumnName = columnName.toUpperCase(Locale.ENGLISH);
1✔
153
      if (mappedColumns.contains(upperColumnName)) {
1✔
154
        mappedColumnNames.add(upperColumnName);
1✔
155
      } else {
156
        unmappedColumnNames.add(columnName);
1✔
157
      }
158
    }
1✔
159
    mappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), mappedColumnNames);
1✔
160
    unMappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), unmappedColumnNames);
1✔
161
  }
1✔
162

163
  public Set<String> getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
164
    Set<String> mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
165
    if (mappedColumnNames == null) {
1✔
166
      loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
1✔
167
      mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
168
    }
169
    return mappedColumnNames;
1✔
170
  }
171

172
  public List<String> getUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
173
    List<String> unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
174
    if (unMappedColumnNames == null) {
1✔
175
      loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
1✔
176
      unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
177
    }
178
    return unMappedColumnNames;
1✔
179
  }
180

181
  private String getMapKey(ResultMap resultMap, String columnPrefix) {
182
    return resultMap.getId() + ":" + columnPrefix;
1✔
183
  }
184

185
  private Set<String> prependPrefixes(Set<String> columnNames, String prefix) {
186
    if (columnNames == null || columnNames.isEmpty() || prefix == null || prefix.length() == 0) {
1!
187
      return columnNames;
1✔
188
    }
189
    final Set<String> prefixed = new HashSet<>();
1✔
190
    for (String columnName : columnNames) {
1✔
191
      prefixed.add(prefix + columnName);
1✔
192
    }
1✔
193
    return prefixed;
1✔
194
  }
195

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

© 2026 Coveralls, Inc