1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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.timeout = configuration.getDefaultStatementTimeout();
75 mappedStatement.sqlCommandType = sqlCommandType;
76 mappedStatement.keyGenerator = configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType) ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
77 String logId = id;
78 if (configuration.getLogPrefix() != null) {
79 logId = configuration.getLogPrefix() + id;
80 }
81 mappedStatement.statementLog = LogFactory.getLog(logId);
82 mappedStatement.lang = configuration.getDefaultScriptingLanuageInstance();
83 }
84
85 public Builder resource(String resource) {
86 mappedStatement.resource = resource;
87 return this;
88 }
89
90 public String id() {
91 return mappedStatement.id;
92 }
93
94 public Builder parameterMap(ParameterMap parameterMap) {
95 mappedStatement.parameterMap = parameterMap;
96 return this;
97 }
98
99 public Builder resultMaps(List<ResultMap> resultMaps) {
100 mappedStatement.resultMaps = resultMaps;
101 for (ResultMap resultMap : resultMaps) {
102 mappedStatement.hasNestedResultMaps = mappedStatement.hasNestedResultMaps || resultMap.hasNestedResultMaps();
103 }
104 return this;
105 }
106
107 public Builder fetchSize(Integer fetchSize) {
108 mappedStatement.fetchSize = fetchSize;
109 return this;
110 }
111
112 public Builder timeout(Integer timeout) {
113 mappedStatement.timeout = timeout;
114 return this;
115 }
116
117 public Builder statementType(StatementType statementType) {
118 mappedStatement.statementType = statementType;
119 return this;
120 }
121
122 public Builder resultSetType(ResultSetType resultSetType) {
123 mappedStatement.resultSetType = resultSetType;
124 return this;
125 }
126
127 public Builder cache(Cache cache) {
128 mappedStatement.cache = cache;
129 return this;
130 }
131
132 public Builder flushCacheRequired(boolean flushCacheRequired) {
133 mappedStatement.flushCacheRequired = flushCacheRequired;
134 return this;
135 }
136
137 public Builder useCache(boolean useCache) {
138 mappedStatement.useCache = useCache;
139 return this;
140 }
141
142 public Builder resultOrdered(boolean resultOrdered) {
143 mappedStatement.resultOrdered = resultOrdered;
144 return this;
145 }
146
147 public Builder keyGenerator(KeyGenerator keyGenerator) {
148 mappedStatement.keyGenerator = keyGenerator;
149 return this;
150 }
151
152 public Builder keyProperty(String keyProperty) {
153 mappedStatement.keyProperties = delimitedStringtoArray(keyProperty);
154 return this;
155 }
156
157 public Builder keyColumn(String keyColumn) {
158 mappedStatement.keyColumns = delimitedStringtoArray(keyColumn);
159 return this;
160 }
161
162 public Builder databaseId(String databaseId) {
163 mappedStatement.databaseId = databaseId;
164 return this;
165 }
166
167 public Builder lang(LanguageDriver driver) {
168 mappedStatement.lang = driver;
169 return this;
170 }
171
172 public Builder resulSets(String resultSet) {
173 mappedStatement.resultSets = delimitedStringtoArray(resultSet);
174 return this;
175 }
176
177 public MappedStatement build() {
178 assert mappedStatement.configuration != null;
179 assert mappedStatement.id != null;
180 assert mappedStatement.sqlSource != null;
181 assert mappedStatement.lang != null;
182 mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
183 return mappedStatement;
184 }
185 }
186
187 public KeyGenerator getKeyGenerator() {
188 return keyGenerator;
189 }
190
191 public SqlCommandType getSqlCommandType() {
192 return sqlCommandType;
193 }
194
195 public String getResource() {
196 return resource;
197 }
198
199 public Configuration getConfiguration() {
200 return configuration;
201 }
202
203 public String getId() {
204 return id;
205 }
206
207 public boolean hasNestedResultMaps() {
208 return hasNestedResultMaps;
209 }
210
211 public Integer getFetchSize() {
212 return fetchSize;
213 }
214
215 public Integer getTimeout() {
216 return timeout;
217 }
218
219 public StatementType getStatementType() {
220 return statementType;
221 }
222
223 public ResultSetType getResultSetType() {
224 return resultSetType;
225 }
226
227 public SqlSource getSqlSource() {
228 return sqlSource;
229 }
230
231 public ParameterMap getParameterMap() {
232 return parameterMap;
233 }
234
235 public List<ResultMap> getResultMaps() {
236 return resultMaps;
237 }
238
239 public Cache getCache() {
240 return cache;
241 }
242
243 public boolean isFlushCacheRequired() {
244 return flushCacheRequired;
245 }
246
247 public boolean isUseCache() {
248 return useCache;
249 }
250
251 public boolean isResultOrdered() {
252 return resultOrdered;
253 }
254
255 public String getDatabaseId() {
256 return databaseId;
257 }
258
259 public String[] getKeyProperties() {
260 return keyProperties;
261 }
262
263 public String[] getKeyColumns() {
264 return keyColumns;
265 }
266
267 public Log getStatementLog() {
268 return statementLog;
269 }
270
271 public LanguageDriver getLang() {
272 return lang;
273 }
274
275 public String[] getResulSets() {
276 return resultSets;
277 }
278
279 public BoundSql getBoundSql(Object parameterObject) {
280 BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
281 List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
282 if (parameterMappings == null || parameterMappings.isEmpty()) {
283 boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
284 }
285
286
287 for (ParameterMapping pm : boundSql.getParameterMappings()) {
288 String rmId = pm.getResultMapId();
289 if (rmId != null) {
290 ResultMap rm = configuration.getResultMap(rmId);
291 if (rm != null) {
292 hasNestedResultMaps |= rm.hasNestedResultMaps();
293 }
294 }
295 }
296
297 return boundSql;
298 }
299
300 private static String[] delimitedStringtoArray(String in) {
301 if (in == null || in.trim().length() == 0) {
302 return null;
303 } else {
304 return in.split(",");
305 }
306 }
307
308 }