BaseTypeHandler.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.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.  *
  30.  * @author Clinton Begin
  31.  * @author Simone Tripodi
  32.  * @author Kzuki Shimizu
  33.  */
  34. public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {

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

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

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

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

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

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

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

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

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

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

  120. }