MappedStatement.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.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;

  20. import org.apache.ibatis.cache.Cache;
  21. import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
  22. import org.apache.ibatis.executor.keygen.KeyGenerator;
  23. import org.apache.ibatis.executor.keygen.NoKeyGenerator;
  24. import org.apache.ibatis.logging.Log;
  25. import org.apache.ibatis.logging.LogFactory;
  26. import org.apache.ibatis.scripting.LanguageDriver;
  27. import org.apache.ibatis.session.Configuration;

  28. /**
  29.  * @author Clinton Begin
  30.  */
  31. public final class MappedStatement {

  32.   private String resource;
  33.   private Configuration configuration;
  34.   private String id;
  35.   private Integer fetchSize;
  36.   private Integer timeout;
  37.   private StatementType statementType;
  38.   private ResultSetType resultSetType;
  39.   private SqlSource sqlSource;
  40.   private Cache cache;
  41.   private ParameterMap parameterMap;
  42.   private List<ResultMap> resultMaps;
  43.   private boolean flushCacheRequired;
  44.   private boolean useCache;
  45.   private boolean resultOrdered;
  46.   private SqlCommandType sqlCommandType;
  47.   private KeyGenerator keyGenerator;
  48.   private String[] keyProperties;
  49.   private String[] keyColumns;
  50.   private boolean hasNestedResultMaps;
  51.   private String databaseId;
  52.   private Log statementLog;
  53.   private LanguageDriver lang;
  54.   private String[] resultSets;
  55.   private boolean dirtySelect;

  56.   MappedStatement() {
  57.     // constructor disabled
  58.   }

  59.   public static class Builder {
  60.     private final MappedStatement mappedStatement = new MappedStatement();

  61.     public Builder(Configuration configuration, String id, SqlSource sqlSource, SqlCommandType sqlCommandType) {
  62.       mappedStatement.configuration = configuration;
  63.       mappedStatement.id = id;
  64.       mappedStatement.sqlSource = sqlSource;
  65.       mappedStatement.statementType = StatementType.PREPARED;
  66.       mappedStatement.resultSetType = ResultSetType.DEFAULT;
  67.       mappedStatement.parameterMap = new ParameterMap.Builder(configuration, "defaultParameterMap", null,
  68.           new ArrayList<>()).build();
  69.       mappedStatement.resultMaps = new ArrayList<>();
  70.       mappedStatement.sqlCommandType = sqlCommandType;
  71.       mappedStatement.keyGenerator = configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType)
  72.           ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
  73.       String logId = id;
  74.       if (configuration.getLogPrefix() != null) {
  75.         logId = configuration.getLogPrefix() + id;
  76.       }
  77.       mappedStatement.statementLog = LogFactory.getLog(logId);
  78.       mappedStatement.lang = configuration.getDefaultScriptingLanguageInstance();
  79.     }

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

  84.     public String id() {
  85.       return mappedStatement.id;
  86.     }

  87.     public Builder parameterMap(ParameterMap parameterMap) {
  88.       mappedStatement.parameterMap = parameterMap;
  89.       return this;
  90.     }

  91.     public Builder resultMaps(List<ResultMap> resultMaps) {
  92.       mappedStatement.resultMaps = resultMaps;
  93.       for (ResultMap resultMap : resultMaps) {
  94.         mappedStatement.hasNestedResultMaps = mappedStatement.hasNestedResultMaps || resultMap.hasNestedResultMaps();
  95.       }
  96.       return this;
  97.     }

  98.     public Builder fetchSize(Integer fetchSize) {
  99.       mappedStatement.fetchSize = fetchSize;
  100.       return this;
  101.     }

  102.     public Builder timeout(Integer timeout) {
  103.       mappedStatement.timeout = timeout;
  104.       return this;
  105.     }

  106.     public Builder statementType(StatementType statementType) {
  107.       mappedStatement.statementType = statementType;
  108.       return this;
  109.     }

  110.     public Builder resultSetType(ResultSetType resultSetType) {
  111.       mappedStatement.resultSetType = resultSetType == null ? ResultSetType.DEFAULT : resultSetType;
  112.       return this;
  113.     }

  114.     public Builder cache(Cache cache) {
  115.       mappedStatement.cache = cache;
  116.       return this;
  117.     }

  118.     public Builder flushCacheRequired(boolean flushCacheRequired) {
  119.       mappedStatement.flushCacheRequired = flushCacheRequired;
  120.       return this;
  121.     }

  122.     public Builder useCache(boolean useCache) {
  123.       mappedStatement.useCache = useCache;
  124.       return this;
  125.     }

  126.     public Builder resultOrdered(boolean resultOrdered) {
  127.       mappedStatement.resultOrdered = resultOrdered;
  128.       return this;
  129.     }

  130.     public Builder keyGenerator(KeyGenerator keyGenerator) {
  131.       mappedStatement.keyGenerator = keyGenerator;
  132.       return this;
  133.     }

  134.     public Builder keyProperty(String keyProperty) {
  135.       mappedStatement.keyProperties = delimitedStringToArray(keyProperty);
  136.       return this;
  137.     }

  138.     public Builder keyColumn(String keyColumn) {
  139.       mappedStatement.keyColumns = delimitedStringToArray(keyColumn);
  140.       return this;
  141.     }

  142.     public Builder databaseId(String databaseId) {
  143.       mappedStatement.databaseId = databaseId;
  144.       return this;
  145.     }

  146.     public Builder lang(LanguageDriver driver) {
  147.       mappedStatement.lang = driver;
  148.       return this;
  149.     }

  150.     public Builder resultSets(String resultSet) {
  151.       mappedStatement.resultSets = delimitedStringToArray(resultSet);
  152.       return this;
  153.     }

  154.     public Builder dirtySelect(boolean dirtySelect) {
  155.       mappedStatement.dirtySelect = dirtySelect;
  156.       return this;
  157.     }

  158.     /**
  159.      * Resul sets.
  160.      *
  161.      * @param resultSet
  162.      *          the result set
  163.      *
  164.      * @return the builder
  165.      *
  166.      * @deprecated Use {@link #resultSets}
  167.      */
  168.     @Deprecated
  169.     public Builder resulSets(String resultSet) {
  170.       mappedStatement.resultSets = delimitedStringToArray(resultSet);
  171.       return this;
  172.     }

  173.     public MappedStatement build() {
  174.       assert mappedStatement.configuration != null;
  175.       assert mappedStatement.id != null;
  176.       assert mappedStatement.sqlSource != null;
  177.       assert mappedStatement.lang != null;
  178.       mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
  179.       return mappedStatement;
  180.     }
  181.   }

  182.   public KeyGenerator getKeyGenerator() {
  183.     return keyGenerator;
  184.   }

  185.   public SqlCommandType getSqlCommandType() {
  186.     return sqlCommandType;
  187.   }

  188.   public String getResource() {
  189.     return resource;
  190.   }

  191.   public Configuration getConfiguration() {
  192.     return configuration;
  193.   }

  194.   public String getId() {
  195.     return id;
  196.   }

  197.   public boolean hasNestedResultMaps() {
  198.     return hasNestedResultMaps;
  199.   }

  200.   public Integer getFetchSize() {
  201.     return fetchSize;
  202.   }

  203.   public Integer getTimeout() {
  204.     return timeout;
  205.   }

  206.   public StatementType getStatementType() {
  207.     return statementType;
  208.   }

  209.   public ResultSetType getResultSetType() {
  210.     return resultSetType;
  211.   }

  212.   public SqlSource getSqlSource() {
  213.     return sqlSource;
  214.   }

  215.   public ParameterMap getParameterMap() {
  216.     return parameterMap;
  217.   }

  218.   public List<ResultMap> getResultMaps() {
  219.     return resultMaps;
  220.   }

  221.   public Cache getCache() {
  222.     return cache;
  223.   }

  224.   public boolean isFlushCacheRequired() {
  225.     return flushCacheRequired;
  226.   }

  227.   public boolean isUseCache() {
  228.     return useCache;
  229.   }

  230.   public boolean isResultOrdered() {
  231.     return resultOrdered;
  232.   }

  233.   public String getDatabaseId() {
  234.     return databaseId;
  235.   }

  236.   public String[] getKeyProperties() {
  237.     return keyProperties;
  238.   }

  239.   public String[] getKeyColumns() {
  240.     return keyColumns;
  241.   }

  242.   public Log getStatementLog() {
  243.     return statementLog;
  244.   }

  245.   public LanguageDriver getLang() {
  246.     return lang;
  247.   }

  248.   public String[] getResultSets() {
  249.     return resultSets;
  250.   }

  251.   public boolean isDirtySelect() {
  252.     return dirtySelect;
  253.   }

  254.   /**
  255.    * Gets the resul sets.
  256.    *
  257.    * @return the resul sets
  258.    *
  259.    * @deprecated Use {@link #getResultSets()}
  260.    */
  261.   @Deprecated
  262.   public String[] getResulSets() {
  263.     return resultSets;
  264.   }

  265.   public BoundSql getBoundSql(Object parameterObject) {
  266.     BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
  267.     List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  268.     if (parameterMappings == null || parameterMappings.isEmpty()) {
  269.       boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
  270.     }

  271.     // check for nested result maps in parameter mappings (issue #30)
  272.     for (ParameterMapping pm : boundSql.getParameterMappings()) {
  273.       String rmId = pm.getResultMapId();
  274.       if (rmId != null) {
  275.         ResultMap rm = configuration.getResultMap(rmId);
  276.         if (rm != null) {
  277.           hasNestedResultMaps |= rm.hasNestedResultMaps();
  278.         }
  279.       }
  280.     }

  281.     return boundSql;
  282.   }

  283.   private static String[] delimitedStringToArray(String in) {
  284.     if (in == null || in.trim().length() == 0) {
  285.       return null;
  286.     }
  287.     return in.split(",");
  288.   }

  289. }