MappedStatement.java

  1. /*
  2.  *    Copyright 2009-2021 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.  *       http://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.   MappedStatement() {
  56.     // constructor disabled
  57.   }

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

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

  77.     public Builder resource(String resource) {
  78.       mappedStatement.resource = resource;
  79.       return this;
  80.     }

  81.     public String id() {
  82.       return mappedStatement.id;
  83.     }

  84.     public Builder parameterMap(ParameterMap parameterMap) {
  85.       mappedStatement.parameterMap = parameterMap;
  86.       return this;
  87.     }

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

  95.     public Builder fetchSize(Integer fetchSize) {
  96.       mappedStatement.fetchSize = fetchSize;
  97.       return this;
  98.     }

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

  103.     public Builder statementType(StatementType statementType) {
  104.       mappedStatement.statementType = statementType;
  105.       return this;
  106.     }

  107.     public Builder resultSetType(ResultSetType resultSetType) {
  108.       mappedStatement.resultSetType = resultSetType == null ? ResultSetType.DEFAULT : resultSetType;
  109.       return this;
  110.     }

  111.     public Builder cache(Cache cache) {
  112.       mappedStatement.cache = cache;
  113.       return this;
  114.     }

  115.     public Builder flushCacheRequired(boolean flushCacheRequired) {
  116.       mappedStatement.flushCacheRequired = flushCacheRequired;
  117.       return this;
  118.     }

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

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

  127.     public Builder keyGenerator(KeyGenerator keyGenerator) {
  128.       mappedStatement.keyGenerator = keyGenerator;
  129.       return this;
  130.     }

  131.     public Builder keyProperty(String keyProperty) {
  132.       mappedStatement.keyProperties = delimitedStringToArray(keyProperty);
  133.       return this;
  134.     }

  135.     public Builder keyColumn(String keyColumn) {
  136.       mappedStatement.keyColumns = delimitedStringToArray(keyColumn);
  137.       return this;
  138.     }

  139.     public Builder databaseId(String databaseId) {
  140.       mappedStatement.databaseId = databaseId;
  141.       return this;
  142.     }

  143.     public Builder lang(LanguageDriver driver) {
  144.       mappedStatement.lang = driver;
  145.       return this;
  146.     }

  147.     public Builder resultSets(String resultSet) {
  148.       mappedStatement.resultSets = delimitedStringToArray(resultSet);
  149.       return this;
  150.     }

  151.     /**
  152.      * Resul sets.
  153.      *
  154.      * @param resultSet
  155.      *          the result set
  156.      * @return the builder
  157.      * @deprecated Use {@link #resultSets}
  158.      */
  159.     @Deprecated
  160.     public Builder resulSets(String resultSet) {
  161.       mappedStatement.resultSets = delimitedStringToArray(resultSet);
  162.       return this;
  163.     }

  164.     public MappedStatement build() {
  165.       assert mappedStatement.configuration != null;
  166.       assert mappedStatement.id != null;
  167.       assert mappedStatement.sqlSource != null;
  168.       assert mappedStatement.lang != null;
  169.       mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
  170.       return mappedStatement;
  171.     }
  172.   }

  173.   public KeyGenerator getKeyGenerator() {
  174.     return keyGenerator;
  175.   }

  176.   public SqlCommandType getSqlCommandType() {
  177.     return sqlCommandType;
  178.   }

  179.   public String getResource() {
  180.     return resource;
  181.   }

  182.   public Configuration getConfiguration() {
  183.     return configuration;
  184.   }

  185.   public String getId() {
  186.     return id;
  187.   }

  188.   public boolean hasNestedResultMaps() {
  189.     return hasNestedResultMaps;
  190.   }

  191.   public Integer getFetchSize() {
  192.     return fetchSize;
  193.   }

  194.   public Integer getTimeout() {
  195.     return timeout;
  196.   }

  197.   public StatementType getStatementType() {
  198.     return statementType;
  199.   }

  200.   public ResultSetType getResultSetType() {
  201.     return resultSetType;
  202.   }

  203.   public SqlSource getSqlSource() {
  204.     return sqlSource;
  205.   }

  206.   public ParameterMap getParameterMap() {
  207.     return parameterMap;
  208.   }

  209.   public List<ResultMap> getResultMaps() {
  210.     return resultMaps;
  211.   }

  212.   public Cache getCache() {
  213.     return cache;
  214.   }

  215.   public boolean isFlushCacheRequired() {
  216.     return flushCacheRequired;
  217.   }

  218.   public boolean isUseCache() {
  219.     return useCache;
  220.   }

  221.   public boolean isResultOrdered() {
  222.     return resultOrdered;
  223.   }

  224.   public String getDatabaseId() {
  225.     return databaseId;
  226.   }

  227.   public String[] getKeyProperties() {
  228.     return keyProperties;
  229.   }

  230.   public String[] getKeyColumns() {
  231.     return keyColumns;
  232.   }

  233.   public Log getStatementLog() {
  234.     return statementLog;
  235.   }

  236.   public LanguageDriver getLang() {
  237.     return lang;
  238.   }

  239.   public String[] getResultSets() {
  240.     return resultSets;
  241.   }

  242.   /**
  243.    * Gets the resul sets.
  244.    *
  245.    * @return the resul sets
  246.    * @deprecated Use {@link #getResultSets()}
  247.    */
  248.   @Deprecated
  249.   public String[] getResulSets() {
  250.     return resultSets;
  251.   }

  252.   public BoundSql getBoundSql(Object parameterObject) {
  253.     BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
  254.     List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  255.     if (parameterMappings == null || parameterMappings.isEmpty()) {
  256.       boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
  257.     }

  258.     // check for nested result maps in parameter mappings (issue #30)
  259.     for (ParameterMapping pm : boundSql.getParameterMappings()) {
  260.       String rmId = pm.getResultMapId();
  261.       if (rmId != null) {
  262.         ResultMap rm = configuration.getResultMap(rmId);
  263.         if (rm != null) {
  264.           hasNestedResultMaps |= rm.hasNestedResultMaps();
  265.         }
  266.       }
  267.     }

  268.     return boundSql;
  269.   }

  270.   private static String[] delimitedStringToArray(String in) {
  271.     if (in == null || in.trim().length() == 0) {
  272.       return null;
  273.     } else {
  274.       return in.split(",");
  275.     }
  276.   }

  277. }