ResultSetWrapper.java

  1. /*
  2.  *    Copyright 2009-2024 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. 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, Set<String>> mappedColumnNamesMap = new HashMap<>();
  47.   private final Map<String, List<String>> unMappedColumnNamesMap = new HashMap<>();

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

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

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

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

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

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

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

  122.   private Class<?> resolveClass(String className) {
  123.     try {
  124.       // #699 className could be null
  125.       if (className != null) {
  126.         return Resources.classForName(className);
  127.       }
  128.     } catch (ClassNotFoundException e) {
  129.       // ignore
  130.     }
  131.     return null;
  132.   }

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

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

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

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

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

  178. }