ParameterMapping.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.mapping;

  17. import java.sql.ResultSet;

  18. import org.apache.ibatis.session.Configuration;
  19. import org.apache.ibatis.type.JdbcType;
  20. import org.apache.ibatis.type.TypeHandler;
  21. import org.apache.ibatis.type.TypeHandlerRegistry;

  22. /**
  23.  * @author Clinton Begin
  24.  */
  25. public class ParameterMapping {

  26.   private Configuration configuration;

  27.   private String property;
  28.   private ParameterMode mode;
  29.   private Class<?> javaType = Object.class;
  30.   private JdbcType jdbcType;
  31.   private Integer numericScale;
  32.   private TypeHandler<?> typeHandler;
  33.   private String resultMapId;
  34.   private String jdbcTypeName;
  35.   private String expression;

  36.   private ParameterMapping() {
  37.   }

  38.   public static class Builder {
  39.     private final ParameterMapping parameterMapping = new ParameterMapping();

  40.     public Builder(Configuration configuration, String property, TypeHandler<?> typeHandler) {
  41.       parameterMapping.configuration = configuration;
  42.       parameterMapping.property = property;
  43.       parameterMapping.typeHandler = typeHandler;
  44.       parameterMapping.mode = ParameterMode.IN;
  45.     }

  46.     public Builder(Configuration configuration, String property, Class<?> javaType) {
  47.       parameterMapping.configuration = configuration;
  48.       parameterMapping.property = property;
  49.       parameterMapping.javaType = javaType;
  50.       parameterMapping.mode = ParameterMode.IN;
  51.     }

  52.     public Builder mode(ParameterMode mode) {
  53.       parameterMapping.mode = mode;
  54.       return this;
  55.     }

  56.     public Builder javaType(Class<?> javaType) {
  57.       parameterMapping.javaType = javaType;
  58.       return this;
  59.     }

  60.     public Builder jdbcType(JdbcType jdbcType) {
  61.       parameterMapping.jdbcType = jdbcType;
  62.       return this;
  63.     }

  64.     public Builder numericScale(Integer numericScale) {
  65.       parameterMapping.numericScale = numericScale;
  66.       return this;
  67.     }

  68.     public Builder resultMapId(String resultMapId) {
  69.       parameterMapping.resultMapId = resultMapId;
  70.       return this;
  71.     }

  72.     public Builder typeHandler(TypeHandler<?> typeHandler) {
  73.       parameterMapping.typeHandler = typeHandler;
  74.       return this;
  75.     }

  76.     public Builder jdbcTypeName(String jdbcTypeName) {
  77.       parameterMapping.jdbcTypeName = jdbcTypeName;
  78.       return this;
  79.     }

  80.     public Builder expression(String expression) {
  81.       parameterMapping.expression = expression;
  82.       return this;
  83.     }

  84.     public ParameterMapping build() {
  85.       resolveTypeHandler();
  86.       validate();
  87.       return parameterMapping;
  88.     }

  89.     private void validate() {
  90.       if (ResultSet.class.equals(parameterMapping.javaType)) {
  91.         if (parameterMapping.resultMapId == null) {
  92.           throw new IllegalStateException("Missing resultmap in property '" + parameterMapping.property + "'.  "
  93.               + "Parameters of type java.sql.ResultSet require a resultmap.");
  94.         }
  95.       } else if (parameterMapping.typeHandler == null) {
  96.         throw new IllegalStateException("Type handler was null on parameter mapping for property '"
  97.             + parameterMapping.property + "'. It was either not specified and/or could not be found for the javaType ("
  98.             + parameterMapping.javaType.getName() + ") : jdbcType (" + parameterMapping.jdbcType + ") combination.");
  99.       }
  100.     }

  101.     private void resolveTypeHandler() {
  102.       if (parameterMapping.typeHandler == null && parameterMapping.javaType != null) {
  103.         Configuration configuration = parameterMapping.configuration;
  104.         TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
  105.         parameterMapping.typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.javaType,
  106.             parameterMapping.jdbcType);
  107.       }
  108.     }

  109.   }

  110.   public String getProperty() {
  111.     return property;
  112.   }

  113.   /**
  114.    * Used for handling output of callable statements.
  115.    *
  116.    * @return the mode
  117.    */
  118.   public ParameterMode getMode() {
  119.     return mode;
  120.   }

  121.   /**
  122.    * Used for handling output of callable statements.
  123.    *
  124.    * @return the java type
  125.    */
  126.   public Class<?> getJavaType() {
  127.     return javaType;
  128.   }

  129.   /**
  130.    * Used in the UnknownTypeHandler in case there is no handler for the property type.
  131.    *
  132.    * @return the jdbc type
  133.    */
  134.   public JdbcType getJdbcType() {
  135.     return jdbcType;
  136.   }

  137.   /**
  138.    * Used for handling output of callable statements.
  139.    *
  140.    * @return the numeric scale
  141.    */
  142.   public Integer getNumericScale() {
  143.     return numericScale;
  144.   }

  145.   /**
  146.    * Used when setting parameters to the PreparedStatement.
  147.    *
  148.    * @return the type handler
  149.    */
  150.   public TypeHandler<?> getTypeHandler() {
  151.     return typeHandler;
  152.   }

  153.   /**
  154.    * Used for handling output of callable statements.
  155.    *
  156.    * @return the result map id
  157.    */
  158.   public String getResultMapId() {
  159.     return resultMapId;
  160.   }

  161.   /**
  162.    * Used for handling output of callable statements.
  163.    *
  164.    * @return the jdbc type name
  165.    */
  166.   public String getJdbcTypeName() {
  167.     return jdbcTypeName;
  168.   }

  169.   /**
  170.    * Expression 'Not used'.
  171.    *
  172.    * @return the expression
  173.    */
  174.   public String getExpression() {
  175.     return expression;
  176.   }

  177.   @Override
  178.   public String toString() {
  179.     final StringBuilder sb = new StringBuilder("ParameterMapping{");
  180.     // sb.append("configuration=").append(configuration); // configuration doesn't have a useful .toString()
  181.     sb.append("property='").append(property).append('\'');
  182.     sb.append(", mode=").append(mode);
  183.     sb.append(", javaType=").append(javaType);
  184.     sb.append(", jdbcType=").append(jdbcType);
  185.     sb.append(", numericScale=").append(numericScale);
  186.     // sb.append(", typeHandler=").append(typeHandler); // typeHandler also doesn't have a useful .toString()
  187.     sb.append(", resultMapId='").append(resultMapId).append('\'');
  188.     sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
  189.     sb.append(", expression='").append(expression).append('\'');
  190.     sb.append('}');
  191.     return sb.toString();
  192.   }
  193. }