View Javadoc
1   /**
2    *    Copyright 2009-2019 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    *       http://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  import org.apache.ibatis.io.Resources;
30  import org.apache.ibatis.mapping.ResultMap;
31  import org.apache.ibatis.session.Configuration;
32  import org.apache.ibatis.type.JdbcType;
33  import org.apache.ibatis.type.ObjectTypeHandler;
34  import org.apache.ibatis.type.TypeHandler;
35  import org.apache.ibatis.type.TypeHandlerRegistry;
36  import org.apache.ibatis.type.UnknownTypeHandler;
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<Class<?>, TypeHandler<?>>> typeHandlerMap = new HashMap<>();
49    private final Map<String, List<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      super();
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.
92     * Tries to get from the TypeHandlerRegistry by searching for the property type.
93     * If not found it gets the column JDBC type and tries to get a handler for it.
94     *
95     * @param propertyType
96     * @param columnName
97     * @return
98     */
99    public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
100     TypeHandler<?> handler = null;
101     Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
102     if (columnHandlers == null) {
103       columnHandlers = new HashMap<>();
104       typeHandlerMap.put(columnName, columnHandlers);
105     } else {
106       handler = columnHandlers.get(propertyType);
107     }
108     if (handler == null) {
109       JdbcType jdbcType = getJdbcType(columnName);
110       handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
111       // Replicate logic of UnknownTypeHandler#resolveTypeHandler
112       // See issue #59 comment 10
113       if (handler == null || handler instanceof UnknownTypeHandler) {
114         final int index = columnNames.indexOf(columnName);
115         final Class<?> javaType = resolveClass(classNames.get(index));
116         if (javaType != null && jdbcType != null) {
117           handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
118         } else if (javaType != null) {
119           handler = typeHandlerRegistry.getTypeHandler(javaType);
120         } else if (jdbcType != null) {
121           handler = typeHandlerRegistry.getTypeHandler(jdbcType);
122         }
123       }
124       if (handler == null || handler instanceof UnknownTypeHandler) {
125         handler = new ObjectTypeHandler();
126       }
127       columnHandlers.put(propertyType, handler);
128     }
129     return handler;
130   }
131 
132   private Class<?> resolveClass(String className) {
133     try {
134       // #699 className could be null
135       if (className != null) {
136         return Resources.classForName(className);
137       }
138     } catch (ClassNotFoundException e) {
139       // ignore
140     }
141     return null;
142   }
143 
144   private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
145     List<String> mappedColumnNames = new ArrayList<>();
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 List<String> getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
162     List<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 }