1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
92
93
94
95
96
97
98
99
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
114
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
137 if (className != null) {
138 return Resources.classForName(className);
139 }
140 } catch (ClassNotFoundException e) {
141
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 }