DefaultParameterHandler.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.scripting.defaults;

  17. import java.sql.PreparedStatement;
  18. import java.sql.SQLException;
  19. import java.util.List;

  20. import org.apache.ibatis.executor.ErrorContext;
  21. import org.apache.ibatis.executor.parameter.ParameterHandler;
  22. import org.apache.ibatis.mapping.BoundSql;
  23. import org.apache.ibatis.mapping.MappedStatement;
  24. import org.apache.ibatis.mapping.ParameterMapping;
  25. import org.apache.ibatis.mapping.ParameterMode;
  26. import org.apache.ibatis.reflection.MetaObject;
  27. import org.apache.ibatis.session.Configuration;
  28. import org.apache.ibatis.type.JdbcType;
  29. import org.apache.ibatis.type.TypeException;
  30. import org.apache.ibatis.type.TypeHandler;
  31. import org.apache.ibatis.type.TypeHandlerRegistry;

  32. /**
  33.  * @author Clinton Begin
  34.  * @author Eduardo Macarron
  35.  */
  36. public class DefaultParameterHandler implements ParameterHandler {

  37.   private final TypeHandlerRegistry typeHandlerRegistry;

  38.   private final MappedStatement mappedStatement;
  39.   private final Object parameterObject;
  40.   private final BoundSql boundSql;
  41.   private final Configuration configuration;

  42.   public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
  43.     this.mappedStatement = mappedStatement;
  44.     this.configuration = mappedStatement.getConfiguration();
  45.     this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
  46.     this.parameterObject = parameterObject;
  47.     this.boundSql = boundSql;
  48.   }

  49.   @Override
  50.   public Object getParameterObject() {
  51.     return parameterObject;
  52.   }

  53.   @Override
  54.   public void setParameters(PreparedStatement ps) {
  55.     ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
  56.     List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  57.     if (parameterMappings != null) {
  58.       MetaObject metaObject = null;
  59.       for (int i = 0; i < parameterMappings.size(); i++) {
  60.         ParameterMapping parameterMapping = parameterMappings.get(i);
  61.         if (parameterMapping.getMode() != ParameterMode.OUT) {
  62.           Object value;
  63.           String propertyName = parameterMapping.getProperty();
  64.           if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
  65.             value = boundSql.getAdditionalParameter(propertyName);
  66.           } else if (parameterObject == null) {
  67.             value = null;
  68.           } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
  69.             value = parameterObject;
  70.           } else {
  71.             if (metaObject == null) {
  72.               metaObject = configuration.newMetaObject(parameterObject);
  73.             }
  74.             value = metaObject.getValue(propertyName);
  75.           }
  76.           TypeHandler typeHandler = parameterMapping.getTypeHandler();
  77.           JdbcType jdbcType = parameterMapping.getJdbcType();
  78.           if (value == null && jdbcType == null) {
  79.             jdbcType = configuration.getJdbcTypeForNull();
  80.           }
  81.           try {
  82.             typeHandler.setParameter(ps, i + 1, value, jdbcType);
  83.           } catch (TypeException | SQLException e) {
  84.             throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
  85.           }
  86.         }
  87.       }
  88.     }
  89.   }

  90. }