BaseTypeHandler.java

  1. /*
  2.  *    Copyright 2009-2023 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.type;

  17. import java.sql.CallableStatement;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;

  21. import org.apache.ibatis.executor.result.ResultMapException;
  22. import org.apache.ibatis.session.Configuration;

  23. /**
  24.  * The base {@link TypeHandler} for references a generic type.
  25.  * <p>
  26.  * Important: Since 3.5.0, This class never call the {@link ResultSet#wasNull()} and {@link CallableStatement#wasNull()}
  27.  * method for handling the SQL {@code NULL} value. In other words, {@code null} value handling should be performed on
  28.  * subclass.
  29.  * </p>
  30.  *
  31.  * @author Clinton Begin
  32.  * @author Simone Tripodi
  33.  * @author Kzuki Shimizu
  34.  */
  35. public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {

  36.   /**
  37.    * @deprecated Since 3.5.0 - See https://github.com/mybatis/mybatis-3/issues/1203. This field will remove future.
  38.    */
  39.   @Deprecated
  40.   protected Configuration configuration;

  41.   /**
  42.    * Sets the configuration.
  43.    *
  44.    * @param c
  45.    *          the new configuration
  46.    *
  47.    * @deprecated Since 3.5.0 - See https://github.com/mybatis/mybatis-3/issues/1203. This property will remove future.
  48.    */
  49.   @Deprecated
  50.   public void setConfiguration(Configuration c) {
  51.     this.configuration = c;
  52.   }

  53.   @Override
  54.   public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
  55.     if (parameter == null) {
  56.       if (jdbcType == null) {
  57.         throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
  58.       }
  59.       try {
  60.         ps.setNull(i, jdbcType.TYPE_CODE);
  61.       } catch (SQLException e) {
  62.         throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
  63.             + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
  64.             + "Cause: " + e, e);
  65.       }
  66.     } else {
  67.       try {
  68.         setNonNullParameter(ps, i, parameter, jdbcType);
  69.       } catch (Exception e) {
  70.         throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . "
  71.             + "Try setting a different JdbcType for this parameter or a different configuration property. " + "Cause: "
  72.             + e, e);
  73.       }
  74.     }
  75.   }

  76.   @Override
  77.   public T getResult(ResultSet rs, String columnName) throws SQLException {
  78.     try {
  79.       return getNullableResult(rs, columnName);
  80.     } catch (Exception e) {
  81.       throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set.  Cause: " + e,
  82.           e);
  83.     }
  84.   }

  85.   @Override
  86.   public T getResult(ResultSet rs, int columnIndex) throws SQLException {
  87.     try {
  88.       return getNullableResult(rs, columnIndex);
  89.     } catch (Exception e) {
  90.       throw new ResultMapException("Error attempting to get column #" + columnIndex + " from result set.  Cause: " + e,
  91.           e);
  92.     }
  93.   }

  94.   @Override
  95.   public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
  96.     try {
  97.       return getNullableResult(cs, columnIndex);
  98.     } catch (Exception e) {
  99.       throw new ResultMapException(
  100.           "Error attempting to get column #" + columnIndex + " from callable statement.  Cause: " + e, e);
  101.     }
  102.   }

  103.   public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType)
  104.       throws SQLException;

  105.   /**
  106.    * Gets the nullable result.
  107.    *
  108.    * @param rs
  109.    *          the rs
  110.    * @param columnName
  111.    *          Column name, when configuration <code>useColumnLabel</code> is <code>false</code>
  112.    *
  113.    * @return the nullable result
  114.    *
  115.    * @throws SQLException
  116.    *           the SQL exception
  117.    */
  118.   public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;

  119.   public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;

  120.   public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;

  121. }