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

  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;

  20. import org.apache.ibatis.reflection.MetaObject;
  21. import org.apache.ibatis.reflection.property.PropertyTokenizer;
  22. import org.apache.ibatis.session.Configuration;

  23. /**
  24.  * An actual SQL String got from an {@link SqlSource} after having processed any dynamic content. The SQL may have SQL
  25.  * placeholders "?" and a list (ordered) of a parameter mappings with the additional information for each parameter (at
  26.  * least the property name of the input object to read the value from).
  27.  * <p>
  28.  * Can also have additional parameters that are created by the dynamic language (for loops, bind...).
  29.  *
  30.  * @author Clinton Begin
  31.  */
  32. public class BoundSql {

  33.   private final String sql;
  34.   private final List<ParameterMapping> parameterMappings;
  35.   private final Object parameterObject;
  36.   private final Map<String, Object> additionalParameters;
  37.   private final MetaObject metaParameters;

  38.   public BoundSql(Configuration configuration, String sql, List<ParameterMapping> parameterMappings,
  39.       Object parameterObject) {
  40.     this.sql = sql;
  41.     this.parameterMappings = parameterMappings;
  42.     this.parameterObject = parameterObject;
  43.     this.additionalParameters = new HashMap<>();
  44.     this.metaParameters = configuration.newMetaObject(additionalParameters);
  45.   }

  46.   public String getSql() {
  47.     return sql;
  48.   }

  49.   public List<ParameterMapping> getParameterMappings() {
  50.     return parameterMappings;
  51.   }

  52.   public Object getParameterObject() {
  53.     return parameterObject;
  54.   }

  55.   public boolean hasAdditionalParameter(String name) {
  56.     String paramName = new PropertyTokenizer(name).getName();
  57.     return additionalParameters.containsKey(paramName);
  58.   }

  59.   public void setAdditionalParameter(String name, Object value) {
  60.     metaParameters.setValue(name, value);
  61.   }

  62.   public Object getAdditionalParameter(String name) {
  63.     return metaParameters.getValue(name);
  64.   }

  65.   public Map<String, Object> getAdditionalParameters() {
  66.     return additionalParameters;
  67.   }
  68. }