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

mybatis / mybatis-3 / 2686

01 Feb 2025 09:55PM UTC coverage: 87.093% (-0.1%) from 87.217%
2686

Pull #3379

github

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

3825 of 4663 branches covered (82.03%)

515 of 579 new or added lines in 36 files covered. (88.95%)

28 existing lines in 6 files now uncovered.

9912 of 11381 relevant lines covered (87.09%)

0.87 hits per line

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

92.5
/src/main/java/org/apache/ibatis/executor/resultset/ResultSetWrapper.java
1
/*
2
 *    Copyright 2009-2025 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.TypeHandler;
35
import org.apache.ibatis.type.TypeHandlerRegistry;
36

37
/**
38
 * @author Iwao AVE!
39
 */
40
public class ResultSetWrapper {
41

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

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

63
  public ResultSet getResultSet() {
64
    return resultSet;
1✔
65
  }
66

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

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

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

79
  public JdbcType getJdbcType(String columnName) {
80
    int columnIndex = getColumnIndex(columnName);
1✔
81
    return columnIndex == -1 ? null : jdbcTypes.get(columnIndex);
1✔
82
  }
83

84
  /**
85
   * Gets the type handler to use when reading the result set. Tries to get from the TypeHandlerRegistry by searching
86
   * for the property type. If not found it gets the column JDBC type and tries to get a handler for it.
87
   *
88
   * @param propertyType
89
   *          the property type
90
   * @param columnName
91
   *          the column name
92
   *
93
   * @return the type handler
94
   */
95
  public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
96
    TypeHandler<?> handler = null;
1✔
97
    Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
1✔
98
    if (columnHandlers == null) {
1✔
99
      columnHandlers = new HashMap<>();
1✔
100
      typeHandlerMap.put(columnName, columnHandlers);
1✔
101
    } else {
102
      handler = columnHandlers.get(propertyType);
1✔
103
    }
104
    if (handler == null) {
1✔
105
      JdbcType jdbcType = getJdbcType(columnName);
1✔
106
      if (jdbcType != null && propertyType != null) {
1!
107
        handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
1✔
108
      } else if (propertyType != null) {
1!
NEW
109
        handler = typeHandlerRegistry.getTypeHandler(propertyType);
×
110
      } else if (jdbcType != null) {
1!
111
        handler = typeHandlerRegistry.getTypeHandler(jdbcType);
1✔
112
      }
113
      if (handler == null) {
1✔
114
        handler = TypeHandlerRegistry.OBJECT_TYPE_HANDLER;
1✔
115
      }
116
      columnHandlers.put(propertyType, handler);
1✔
117
    }
118
    return handler;
1✔
119
  }
120

121
  private int getColumnIndex(String columnName) {
122
    for (int i = 0; i < columnNames.size(); i++) {
1✔
123
      if (columnNames.get(i).equalsIgnoreCase(columnName)) {
1✔
124
        return i;
1✔
125
      }
126
    }
127
    return -1;
1✔
128
  }
129

130
  private Class<?> resolveClass(String className) {
131
    try {
132
      // #699 className could be null
UNCOV
133
      if (className != null) {
×
UNCOV
134
        return Resources.classForName(className);
×
135
      }
136
    } catch (ClassNotFoundException e) {
×
137
      // ignore
138
    }
×
139
    return null;
×
140
  }
141

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

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

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

177
  private String getMapKey(ResultMap resultMap, String columnPrefix) {
178
    return resultMap.getId() + ":" + columnPrefix;
1✔
179
  }
180

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

192
}
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

© 2025 Coveralls, Inc