ResultSetWrapper.java

  1. /*
  2.  *    Copyright 2009-2021 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. import java.sql.ResultSet;
  18. import java.sql.ResultSetMetaData;
  19. import java.sql.SQLException;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Locale;
  26. import java.util.Map;
  27. import java.util.Set;

  28. import org.apache.ibatis.io.Resources;
  29. import org.apache.ibatis.mapping.ResultMap;
  30. import org.apache.ibatis.session.Configuration;
  31. import org.apache.ibatis.type.JdbcType;
  32. import org.apache.ibatis.type.ObjectTypeHandler;
  33. import org.apache.ibatis.type.TypeHandler;
  34. import org.apache.ibatis.type.TypeHandlerRegistry;
  35. import org.apache.ibatis.type.UnknownTypeHandler;

  36. /**
  37.  * @author Iwao AVE!
  38.  */
  39. public class ResultSetWrapper {

  40.   private final ResultSet resultSet;
  41.   private final TypeHandlerRegistry typeHandlerRegistry;
  42.   private final List<String> columnNames = new ArrayList<>();
  43.   private final List<String> classNames = new ArrayList<>();
  44.   private final List<JdbcType> jdbcTypes = new ArrayList<>();
  45.   private final Map<String, Map<Class<?>, TypeHandler<?>>> typeHandlerMap = new HashMap<>();
  46.   private final Map<String, List<String>> mappedColumnNamesMap = new HashMap<>();
  47.   private final Map<String, List<String>> unMappedColumnNamesMap = new HashMap<>();

  48.   public ResultSetWrapper(ResultSet rs, Configuration configuration) throws SQLException {
  49.     super();
  50.     this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
  51.     this.resultSet = rs;
  52.     final ResultSetMetaData metaData = rs.getMetaData();
  53.     final int columnCount = metaData.getColumnCount();
  54.     for (int i = 1; i <= columnCount; i++) {
  55.       columnNames.add(configuration.isUseColumnLabel() ? metaData.getColumnLabel(i) : metaData.getColumnName(i));
  56.       jdbcTypes.add(JdbcType.forCode(metaData.getColumnType(i)));
  57.       classNames.add(metaData.getColumnClassName(i));
  58.     }
  59.   }

  60.   public ResultSet getResultSet() {
  61.     return resultSet;
  62.   }

  63.   public List<String> getColumnNames() {
  64.     return this.columnNames;
  65.   }

  66.   public List<String> getClassNames() {
  67.     return Collections.unmodifiableList(classNames);
  68.   }

  69.   public List<JdbcType> getJdbcTypes() {
  70.     return jdbcTypes;
  71.   }

  72.   public JdbcType getJdbcType(String columnName) {
  73.     for (int i = 0; i < columnNames.size(); i++) {
  74.       if (columnNames.get(i).equalsIgnoreCase(columnName)) {
  75.         return jdbcTypes.get(i);
  76.       }
  77.     }
  78.     return null;
  79.   }

  80.   /**
  81.    * Gets the type handler to use when reading the result set.
  82.    * Tries to get from the TypeHandlerRegistry by searching for the property type.
  83.    * If not found it gets the column JDBC type and tries to get a handler for it.
  84.    *
  85.    * @param propertyType
  86.    *          the property type
  87.    * @param columnName
  88.    *          the column name
  89.    * @return the type handler
  90.    */
  91.   public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
  92.     TypeHandler<?> handler = null;
  93.     Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
  94.     if (columnHandlers == null) {
  95.       columnHandlers = new HashMap<>();
  96.       typeHandlerMap.put(columnName, columnHandlers);
  97.     } else {
  98.       handler = columnHandlers.get(propertyType);
  99.     }
  100.     if (handler == null) {
  101.       JdbcType jdbcType = getJdbcType(columnName);
  102.       handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
  103.       // Replicate logic of UnknownTypeHandler#resolveTypeHandler
  104.       // See issue #59 comment 10
  105.       if (handler == null || handler instanceof UnknownTypeHandler) {
  106.         final int index = columnNames.indexOf(columnName);
  107.         final Class<?> javaType = resolveClass(classNames.get(index));
  108.         if (javaType != null && jdbcType != null) {
  109.           handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
  110.         } else if (javaType != null) {
  111.           handler = typeHandlerRegistry.getTypeHandler(javaType);
  112.         } else if (jdbcType != null) {
  113.           handler = typeHandlerRegistry.getTypeHandler(jdbcType);
  114.         }
  115.       }
  116.       if (handler == null || handler instanceof UnknownTypeHandler) {
  117.         handler = new ObjectTypeHandler();
  118.       }
  119.       columnHandlers.put(propertyType, handler);
  120.     }
  121.     return handler;
  122.   }

  123.   private 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.   private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
  135.     List<String> mappedColumnNames = new ArrayList<>();
  136.     List<String> unmappedColumnNames = new ArrayList<>();
  137.     final String upperColumnPrefix = columnPrefix == null ? null : columnPrefix.toUpperCase(Locale.ENGLISH);
  138.     final Set<String> mappedColumns = prependPrefixes(resultMap.getMappedColumns(), upperColumnPrefix);
  139.     for (String columnName : columnNames) {
  140.       final String upperColumnName = columnName.toUpperCase(Locale.ENGLISH);
  141.       if (mappedColumns.contains(upperColumnName)) {
  142.         mappedColumnNames.add(upperColumnName);
  143.       } else {
  144.         unmappedColumnNames.add(columnName);
  145.       }
  146.     }
  147.     mappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), mappedColumnNames);
  148.     unMappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), unmappedColumnNames);
  149.   }

  150.   public List<String> getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
  151.     List<String> mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
  152.     if (mappedColumnNames == null) {
  153.       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
  154.       mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
  155.     }
  156.     return mappedColumnNames;
  157.   }

  158.   public List<String> getUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
  159.     List<String> unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
  160.     if (unMappedColumnNames == null) {
  161.       loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
  162.       unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
  163.     }
  164.     return unMappedColumnNames;
  165.   }

  166.   private String getMapKey(ResultMap resultMap, String columnPrefix) {
  167.     return resultMap.getId() + ":" + columnPrefix;
  168.   }

  169.   private Set<String> prependPrefixes(Set<String> columnNames, String prefix) {
  170.     if (columnNames == null || columnNames.isEmpty() || prefix == null || prefix.length() == 0) {
  171.       return columnNames;
  172.     }
  173.     final Set<String> prefixed = new HashSet<>();
  174.     for (String columnName : columnNames) {
  175.       prefixed.add(prefix + columnName);
  176.     }
  177.     return prefixed;
  178.   }

  179. }