View Javadoc
1   /**
2    *    Copyright 2009-2015 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  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.ibatis.cache.Cache;
23  import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
24  import org.apache.ibatis.executor.keygen.KeyGenerator;
25  import org.apache.ibatis.executor.keygen.NoKeyGenerator;
26  import org.apache.ibatis.logging.Log;
27  import org.apache.ibatis.logging.LogFactory;
28  import org.apache.ibatis.scripting.LanguageDriver;
29  import org.apache.ibatis.session.Configuration;
30  
31  /**
32   * @author Clinton Begin
33   */
34  public final class MappedStatement {
35  
36    private String resource;
37    private Configuration configuration;
38    private String id;
39    private Integer fetchSize;
40    private Integer timeout;
41    private StatementType statementType;
42    private ResultSetType resultSetType;
43    private SqlSource sqlSource;
44    private Cache cache;
45    private ParameterMap parameterMap;
46    private List<ResultMap> resultMaps;
47    private boolean flushCacheRequired;
48    private boolean useCache;
49    private boolean resultOrdered;
50    private SqlCommandType sqlCommandType;
51    private KeyGenerator keyGenerator;
52    private String[] keyProperties;
53    private String[] keyColumns;
54    private boolean hasNestedResultMaps;
55    private String databaseId;
56    private Log statementLog;
57    private LanguageDriver lang;
58    private String[] resultSets;
59  
60    MappedStatement() {
61      // constructor disabled
62    }
63  
64    public static class Builder {
65      private MappedStatement mappedStatement = new MappedStatement();
66  
67      public Builder(Configuration configuration, String id, SqlSource sqlSource, SqlCommandType sqlCommandType) {
68        mappedStatement.configuration = configuration;
69        mappedStatement.id = id;
70        mappedStatement.sqlSource = sqlSource;
71        mappedStatement.statementType = StatementType.PREPARED;
72        mappedStatement.parameterMap = new ParameterMap.Builder(configuration, "defaultParameterMap", null, new ArrayList<ParameterMapping>()).build();
73        mappedStatement.resultMaps = new ArrayList<ResultMap>();
74        mappedStatement.sqlCommandType = sqlCommandType;
75        mappedStatement.keyGenerator = configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType) ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
76        String logId = id;
77        if (configuration.getLogPrefix() != null) {
78          logId = configuration.getLogPrefix() + id;
79        }
80        mappedStatement.statementLog = LogFactory.getLog(logId);
81        mappedStatement.lang = configuration.getDefaultScriptingLanuageInstance();
82      }
83  
84      public Builder resource(String resource) {
85        mappedStatement.resource = resource;
86        return this;
87      }
88  
89      public String id() {
90        return mappedStatement.id;
91      }
92  
93      public Builder parameterMap(ParameterMap parameterMap) {
94        mappedStatement.parameterMap = parameterMap;
95        return this;
96      }
97  
98      public Builder resultMaps(List<ResultMap> resultMaps) {
99        mappedStatement.resultMaps = resultMaps;
100       for (ResultMap resultMap : resultMaps) {
101         mappedStatement.hasNestedResultMaps = mappedStatement.hasNestedResultMaps || resultMap.hasNestedResultMaps();
102       }
103       return this;
104     }
105 
106     public Builder fetchSize(Integer fetchSize) {
107       mappedStatement.fetchSize = fetchSize;
108       return this;
109     }
110 
111     public Builder timeout(Integer timeout) {
112       mappedStatement.timeout = timeout;
113       return this;
114     }
115 
116     public Builder statementType(StatementType statementType) {
117       mappedStatement.statementType = statementType;
118       return this;
119     }
120 
121     public Builder resultSetType(ResultSetType resultSetType) {
122       mappedStatement.resultSetType = resultSetType;
123       return this;
124     }
125 
126     public Builder cache(Cache cache) {
127       mappedStatement.cache = cache;
128       return this;
129     }
130 
131     public Builder flushCacheRequired(boolean flushCacheRequired) {
132       mappedStatement.flushCacheRequired = flushCacheRequired;
133       return this;
134     }
135 
136     public Builder useCache(boolean useCache) {
137       mappedStatement.useCache = useCache;
138       return this;
139     }
140 
141     public Builder resultOrdered(boolean resultOrdered) {
142       mappedStatement.resultOrdered = resultOrdered;
143       return this;
144     }
145 
146     public Builder keyGenerator(KeyGenerator keyGenerator) {
147       mappedStatement.keyGenerator = keyGenerator;
148       return this;
149     }
150 
151     public Builder keyProperty(String keyProperty) {
152       mappedStatement.keyProperties = delimitedStringtoArray(keyProperty);
153       return this;
154     }
155 
156     public Builder keyColumn(String keyColumn) {
157       mappedStatement.keyColumns = delimitedStringtoArray(keyColumn);
158       return this;
159     }
160 
161     public Builder databaseId(String databaseId) {
162       mappedStatement.databaseId = databaseId;
163       return this;
164     }
165 
166     public Builder lang(LanguageDriver driver) {
167       mappedStatement.lang = driver;
168       return this;
169     }
170 
171     public Builder resulSets(String resultSet) {
172       mappedStatement.resultSets = delimitedStringtoArray(resultSet);
173       return this;
174     }
175     
176     public MappedStatement build() {
177       assert mappedStatement.configuration != null;
178       assert mappedStatement.id != null;
179       assert mappedStatement.sqlSource != null;
180       assert mappedStatement.lang != null;
181       mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
182       return mappedStatement;
183     }
184   }
185 
186   public KeyGenerator getKeyGenerator() {
187     return keyGenerator;
188   }
189 
190   public SqlCommandType getSqlCommandType() {
191     return sqlCommandType;
192   }
193 
194   public String getResource() {
195     return resource;
196   }
197 
198   public Configuration getConfiguration() {
199     return configuration;
200   }
201 
202   public String getId() {
203     return id;
204   }
205 
206   public boolean hasNestedResultMaps() {
207     return hasNestedResultMaps;
208   }
209 
210   public Integer getFetchSize() {
211     return fetchSize;
212   }
213 
214   public Integer getTimeout() {
215     return timeout;
216   }
217 
218   public StatementType getStatementType() {
219     return statementType;
220   }
221 
222   public ResultSetType getResultSetType() {
223     return resultSetType;
224   }
225 
226   public SqlSource getSqlSource() {
227     return sqlSource;
228   }
229 
230   public ParameterMap getParameterMap() {
231     return parameterMap;
232   }
233 
234   public List<ResultMap> getResultMaps() {
235     return resultMaps;
236   }
237 
238   public Cache getCache() {
239     return cache;
240   }
241 
242   public boolean isFlushCacheRequired() {
243     return flushCacheRequired;
244   }
245 
246   public boolean isUseCache() {
247     return useCache;
248   }
249 
250   public boolean isResultOrdered() {
251     return resultOrdered;
252   }
253 
254   public String getDatabaseId() {
255     return databaseId;
256   }
257 
258   public String[] getKeyProperties() {
259     return keyProperties;
260   }
261 
262   public String[] getKeyColumns() {
263     return keyColumns;
264   }
265 
266   public Log getStatementLog() {
267     return statementLog;
268   }
269 
270   public LanguageDriver getLang() {
271     return lang;
272   }
273 
274   public String[] getResulSets() {
275     return resultSets;
276   }
277   
278   public BoundSql getBoundSql(Object parameterObject) {
279     BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
280     List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
281     if (parameterMappings == null || parameterMappings.isEmpty()) {
282       boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
283     }
284 
285     // check for nested result maps in parameter mappings (issue #30)
286     for (ParameterMapping pm : boundSql.getParameterMappings()) {
287       String rmId = pm.getResultMapId();
288       if (rmId != null) {
289         ResultMap rm = configuration.getResultMap(rmId);
290         if (rm != null) {
291           hasNestedResultMaps |= rm.hasNestedResultMaps();
292         }
293       }
294     }
295 
296     return boundSql;
297   }
298 
299   private static String[] delimitedStringtoArray(String in) {
300     if (in == null || in.trim().length() == 0) {
301       return null;
302     } else {
303       return in.split(",");
304     }
305   }
306 
307 }