View Javadoc
1   /**
2    *    Copyright 2009-2015 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  class ResultSetWrapper {
42  
43    private final ResultSet resultSet;
44    private final TypeHandlerRegistry typeHandlerRegistry;
45    private final List<String> columnNames = new ArrayList<String>();
46    private final List<String> classNames = new ArrayList<String>();
47    private final List<JdbcType> jdbcTypes = new ArrayList<JdbcType>();
48    private final Map<String, Map<Class<?>, TypeHandler<?>>> typeHandlerMap = new HashMap<String, Map<Class<?>, TypeHandler<?>>>();
49    private Map<String, List<String>> mappedColumnNamesMap = new HashMap<String, List<String>>();
50    private Map<String, List<String>> unMappedColumnNamesMap = new HashMap<String, List<String>>();
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    /**
78     * Gets the type handler to use when reading the result set.
79     * Tries to get from the TypeHandlerRegistry by searching for the property type.
80     * If not found it gets the column JDBC type and tries to get a handler for it.
81     * 
82     * @param propertyType
83     * @param columnName
84     * @return
85     */
86    public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
87      TypeHandler<?> handler = null;
88      Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
89      if (columnHandlers == null) {
90        columnHandlers = new HashMap<Class<?>, TypeHandler<?>>();
91        typeHandlerMap.put(columnName, columnHandlers);
92      } else {
93        handler = columnHandlers.get(propertyType);
94      }
95      if (handler == null) {
96        handler = typeHandlerRegistry.getTypeHandler(propertyType);
97        // Replicate logic of UnknownTypeHandler#resolveTypeHandler
98        // See issue #59 comment 10
99        if (handler == null || handler instanceof UnknownTypeHandler) {
100         final int index = columnNames.indexOf(columnName);
101         final JdbcType jdbcType = jdbcTypes.get(index);
102         final Class<?> javaType = resolveClass(classNames.get(index));
103         if (javaType != null && jdbcType != null) {
104           handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
105         } else if (javaType != null) {
106           handler = typeHandlerRegistry.getTypeHandler(javaType);
107         } else if (jdbcType != null) {
108           handler = typeHandlerRegistry.getTypeHandler(jdbcType);
109         }
110       }
111       if (handler == null || handler instanceof UnknownTypeHandler) {
112         handler = new ObjectTypeHandler();
113       }
114       columnHandlers.put(propertyType, handler);
115     }
116     return handler;
117   }
118 
119   private Class<?> resolveClass(String className) {
120     try {
121       return Resources.classForName(className);
122     } catch (ClassNotFoundException e) {
123       return null;
124     }
125   }
126 
127   private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
128     List<String> mappedColumnNames = new ArrayList<String>();
129     List<String> unmappedColumnNames = new ArrayList<String>();
130     final String upperColumnPrefix = columnPrefix == null ? null : columnPrefix.toUpperCase(Locale.ENGLISH);
131     final Set<String> mappedColumns = prependPrefixes(resultMap.getMappedColumns(), upperColumnPrefix);
132     for (String columnName : columnNames) {
133       final String upperColumnName = columnName.toUpperCase(Locale.ENGLISH);
134       if (mappedColumns.contains(upperColumnName)) {
135         mappedColumnNames.add(upperColumnName);
136       } else {
137         unmappedColumnNames.add(columnName);
138       }
139     }
140     mappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), mappedColumnNames);
141     unMappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), unmappedColumnNames);
142   }
143 
144   public List<String> getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
145     List<String> mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
146     if (mappedColumnNames == null) {
147       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
148       mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
149     }
150     return mappedColumnNames;
151   }
152 
153   public List<String> getUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
154     List<String> unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
155     if (unMappedColumnNames == null) {
156       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
157       unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
158     }
159     return unMappedColumnNames;
160   }
161 
162   private String getMapKey(ResultMap resultMap, String columnPrefix) {
163     return resultMap.getId() + ":" + columnPrefix;
164   }
165 
166   private Set<String> prependPrefixes(Set<String> columnNames, String prefix) {
167     if (columnNames == null || columnNames.isEmpty() || prefix == null || prefix.length() == 0) {
168       return columnNames;
169     }
170     final Set<String> prefixed = new HashSet<String>();
171     for (String columnName : columnNames) {
172       prefixed.add(prefix + columnName);
173     }
174     return prefixed;
175   }
176   
177 }