View Javadoc
1   /*
2    *    Copyright 2009-2023 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<>();
47    private final List<String> classNames = new ArrayList<>();
48    private final List<JdbcType> jdbcTypes = new ArrayList<>();
49    private final Map<String, Map<Class<?>, TypeHandler<?>>> typeHandlerMap = new HashMap<>();
50    private final Map<String, Set<String>> mappedColumnNamesMap = new HashMap<>();
51    private final Map<String, List<String>> unMappedColumnNamesMap = new HashMap<>();
52  
53    public ResultSetWrapper(ResultSet rs, Configuration configuration) throws SQLException {
54      this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
55      this.resultSet = rs;
56      final ResultSetMetaData metaData = rs.getMetaData();
57      final int columnCount = metaData.getColumnCount();
58      for (int i = 1; i <= columnCount; i++) {
59        columnNames.add(configuration.isUseColumnLabel() ? metaData.getColumnLabel(i) : metaData.getColumnName(i));
60        jdbcTypes.add(JdbcType.forCode(metaData.getColumnType(i)));
61        classNames.add(metaData.getColumnClassName(i));
62      }
63    }
64  
65    public ResultSet getResultSet() {
66      return resultSet;
67    }
68  
69    public List<String> getColumnNames() {
70      return this.columnNames;
71    }
72  
73    public List<String> getClassNames() {
74      return Collections.unmodifiableList(classNames);
75    }
76  
77    public List<JdbcType> getJdbcTypes() {
78      return jdbcTypes;
79    }
80  
81    public JdbcType getJdbcType(String columnName) {
82      for (int i = 0; i < columnNames.size(); i++) {
83        if (columnNames.get(i).equalsIgnoreCase(columnName)) {
84          return jdbcTypes.get(i);
85        }
86      }
87      return null;
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;
103     Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
104     if (columnHandlers == null) {
105       columnHandlers = new HashMap<>();
106       typeHandlerMap.put(columnName, columnHandlers);
107     } else {
108       handler = columnHandlers.get(propertyType);
109     }
110     if (handler == null) {
111       JdbcType jdbcType = getJdbcType(columnName);
112       handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
113       // Replicate logic of UnknownTypeHandler#resolveTypeHandler
114       // See issue #59 comment 10
115       if (handler == null || handler instanceof UnknownTypeHandler) {
116         final int index = columnNames.indexOf(columnName);
117         final Class<?> javaType = resolveClass(classNames.get(index));
118         if (javaType != null && jdbcType != null) {
119           handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
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) {
127         handler = new ObjectTypeHandler();
128       }
129       columnHandlers.put(propertyType, handler);
130     }
131     return handler;
132   }
133 
134   private Class<?> resolveClass(String className) {
135     try {
136       // #699 className could be null
137       if (className != null) {
138         return Resources.classForName(className);
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<>();
148     List<String> unmappedColumnNames = new ArrayList<>();
149     final String upperColumnPrefix = columnPrefix == null ? null : columnPrefix.toUpperCase(Locale.ENGLISH);
150     final Set<String> mappedColumns = prependPrefixes(resultMap.getMappedColumns(), upperColumnPrefix);
151     for (String columnName : columnNames) {
152       final String upperColumnName = columnName.toUpperCase(Locale.ENGLISH);
153       if (mappedColumns.contains(upperColumnName)) {
154         mappedColumnNames.add(upperColumnName);
155       } else {
156         unmappedColumnNames.add(columnName);
157       }
158     }
159     mappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), mappedColumnNames);
160     unMappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), unmappedColumnNames);
161   }
162 
163   public Set<String> getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
164     Set<String> mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
165     if (mappedColumnNames == null) {
166       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
167       mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
168     }
169     return mappedColumnNames;
170   }
171 
172   public List<String> getUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
173     List<String> unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
174     if (unMappedColumnNames == null) {
175       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
176       unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
177     }
178     return unMappedColumnNames;
179   }
180 
181   private String getMapKey(ResultMap resultMap, String columnPrefix) {
182     return resultMap.getId() + ":" + columnPrefix;
183   }
184 
185   private Set<String> prependPrefixes(Set<String> columnNames, String prefix) {
186     if (columnNames == null || columnNames.isEmpty() || prefix == null || prefix.length() == 0) {
187       return columnNames;
188     }
189     final Set<String> prefixed = new HashSet<>();
190     for (String columnName : columnNames) {
191       prefixed.add(prefix + columnName);
192     }
193     return prefixed;
194   }
195 
196 }