View Javadoc
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.executor.resultset;
17  
18  import java.lang.reflect.Type;
19  import java.sql.ResultSet;
20  import java.sql.ResultSetMetaData;
21  import java.sql.SQLException;
22  import java.util.ArrayList;
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  
38  /**
39   * @author Iwao AVE!
40   */
41  public class ResultSetWrapper {
42  
43    private final ResultSet resultSet;
44    private final TypeHandlerRegistry typeHandlerRegistry;
45    private final List<String> columnNames = new ArrayList<>();
46    private final List<String> classNames = new ArrayList<>();
47    private final List<JdbcType> jdbcTypes = new ArrayList<>();
48    private final Map<String, Map<Type, TypeHandler<?>>> typeHandlerMap = new HashMap<>();
49    private final Map<String, Set<String>> mappedColumnNamesMap = new HashMap<>();
50    private final Map<String, List<String>> unMappedColumnNamesMap = new HashMap<>();
51  
52    public ResultSetWrapper(ResultSet rs, Configuration configuration) throws SQLException {
53      this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
54      this.resultSet = rs;
55      final ResultSetMetaData metaData = rs.getMetaData();
56      final int columnCount = metaData.getColumnCount();
57      for (int i = 1; i <= columnCount; i++) {
58        columnNames.add(configuration.isUseColumnLabel() ? metaData.getColumnLabel(i) : metaData.getColumnName(i));
59        jdbcTypes.add(JdbcType.forCode(metaData.getColumnType(i)));
60        classNames.add(metaData.getColumnClassName(i));
61      }
62    }
63  
64    public ResultSet getResultSet() {
65      return resultSet;
66    }
67  
68    public List<String> getColumnNames() {
69      return this.columnNames;
70    }
71  
72    public List<String> getClassNames() {
73      return List.copyOf(classNames);
74    }
75  
76    public List<JdbcType> getJdbcTypes() {
77      return jdbcTypes;
78    }
79  
80    public JdbcType getJdbcType(String columnName) {
81      int columnIndex = getColumnIndex(columnName);
82      return columnIndex == -1 ? null : jdbcTypes.get(columnIndex);
83    }
84  
85    /**
86     * Gets the type handler to use when reading the result set. Tries to get from the TypeHandlerRegistry by searching
87     * for the property type. If not found it gets the column JDBC type and tries to get a handler for it.
88     *
89     * @param propertyType
90     *          the property type
91     * @param columnName
92     *          the column name
93     *
94     * @return the type handler
95     */
96    public TypeHandler<?> getTypeHandler(Type propertyType, String columnName) {
97      return typeHandlerMap.computeIfAbsent(columnName, k -> new HashMap<>()).computeIfAbsent(propertyType, k -> {
98        int index = getColumnIndex(columnName);
99        if (index == -1) {
100         return ObjectTypeHandler.INSTANCE;
101       }
102 
103       JdbcType jdbcType = jdbcTypes.get(index);
104       TypeHandler<?> handler = typeHandlerRegistry.getTypeHandler(k, jdbcType, null);
105       if (handler != null) {
106         return handler;
107       }
108 
109       Class<?> javaType = resolveClass(classNames.get(index));
110       if (!(k instanceof Class && ((Class<?>) k).isAssignableFrom(javaType))) {
111         // Clearly incompatible
112         return null;
113       }
114 
115       handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType, null);
116       if (handler == null) {
117         handler = typeHandlerRegistry.getTypeHandler(jdbcType);
118       }
119       return handler == null ? ObjectTypeHandler.INSTANCE : handler;
120     });
121   }
122 
123   static Class<?> resolveClass(String className) {
124     try {
125       // #699 className could be null
126       if (className != null) {
127         return Resources.classForName(className);
128       }
129     } catch (ClassNotFoundException e) {
130       // ignore
131     }
132     return null;
133   }
134 
135   private int getColumnIndex(String columnName) {
136     for (int i = 0; i < columnNames.size(); i++) {
137       if (columnNames.get(i).equalsIgnoreCase(columnName)) {
138         return i;
139       }
140     }
141     return -1;
142   }
143 
144   private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
145     Set<String> mappedColumnNames = new HashSet<>();
146     List<String> unmappedColumnNames = new ArrayList<>();
147     final String upperColumnPrefix = columnPrefix == null ? null : columnPrefix.toUpperCase(Locale.ENGLISH);
148     final Set<String> mappedColumns = prependPrefixes(resultMap.getMappedColumns(), upperColumnPrefix);
149     for (String columnName : columnNames) {
150       final String upperColumnName = columnName.toUpperCase(Locale.ENGLISH);
151       if (mappedColumns.contains(upperColumnName)) {
152         mappedColumnNames.add(upperColumnName);
153       } else {
154         unmappedColumnNames.add(columnName);
155       }
156     }
157     mappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), mappedColumnNames);
158     unMappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), unmappedColumnNames);
159   }
160 
161   public Set<String> getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
162     Set<String> mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
163     if (mappedColumnNames == null) {
164       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
165       mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
166     }
167     return mappedColumnNames;
168   }
169 
170   public List<String> getUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
171     List<String> unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
172     if (unMappedColumnNames == null) {
173       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
174       unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
175     }
176     return unMappedColumnNames;
177   }
178 
179   private String getMapKey(ResultMap resultMap, String columnPrefix) {
180     return resultMap.getId() + ":" + columnPrefix;
181   }
182 
183   private Set<String> prependPrefixes(Set<String> columnNames, String prefix) {
184     if (columnNames == null || columnNames.isEmpty() || prefix == null || prefix.length() == 0) {
185       return columnNames;
186     }
187     final Set<String> prefixed = new HashSet<>();
188     for (String columnName : columnNames) {
189       prefixed.add(prefix + columnName);
190     }
191     return prefixed;
192   }
193 
194 }