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">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.resultSetType = ResultSetType.DEFAULT;
73 mappedStatement.parameterMap = new ParameterMap.Builder(configuration, "defaultParameterMap", null, new ArrayList<>()).build();
74 mappedStatement.resultMaps = new ArrayList<>();
75 mappedStatement.sqlCommandType = sqlCommandType;
76 mappedStatement.keyGenerator = configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType) ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
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.getDefaultScriptingLanguageInstance();
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 == null ? ResultSetType.DEFAULT : 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 resultSets(String resultSet) {
173 mappedStatement.resultSets = delimitedStringToArray(resultSet);
174 return this;
175 }
176
177
178
179
180 @Deprecated
181 public Builder resulSets(String resultSet) {
182 mappedStatement.resultSets = delimitedStringToArray(resultSet);
183 return this;
184 }
185
186 public MappedStatement build() {
187 assert mappedStatement.configuration != null;
188 assert mappedStatement.id != null;
189 assert mappedStatement.sqlSource != null;
190 assert mappedStatement.lang != null;
191 mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
192 return mappedStatement;
193 }
194 }
195
196 public KeyGenerator getKeyGenerator() {
197 return keyGenerator;
198 }
199
200 public SqlCommandType getSqlCommandType() {
201 return sqlCommandType;
202 }
203
204 public String getResource() {
205 return resource;
206 }
207
208 public Configuration getConfiguration() {
209 return configuration;
210 }
211
212 public String getId() {
213 return id;
214 }
215
216 public boolean hasNestedResultMaps() {
217 return hasNestedResultMaps;
218 }
219
220 public Integer getFetchSize() {
221 return fetchSize;
222 }
223
224 public Integer getTimeout() {
225 return timeout;
226 }
227
228 public StatementType getStatementType() {
229 return statementType;
230 }
231
232 public ResultSetType getResultSetType() {
233 return resultSetType;
234 }
235
236 public SqlSource getSqlSource() {
237 return sqlSource;
238 }
239
240 public ParameterMap getParameterMap() {
241 return parameterMap;
242 }
243
244 public List<ResultMap> getResultMaps() {
245 return resultMaps;
246 }
247
248 public Cache getCache() {
249 return cache;
250 }
251
252 public boolean isFlushCacheRequired() {
253 return flushCacheRequired;
254 }
255
256 public boolean isUseCache() {
257 return useCache;
258 }
259
260 public boolean isResultOrdered() {
261 return resultOrdered;
262 }
263
264 public String getDatabaseId() {
265 return databaseId;
266 }
267
268 public String[] getKeyProperties() {
269 return keyProperties;
270 }
271
272 public String[] getKeyColumns() {
273 return keyColumns;
274 }
275
276 public Log getStatementLog() {
277 return statementLog;
278 }
279
280 public LanguageDriver getLang() {
281 return lang;
282 }
283
284 public String[] getResultSets() {
285 return resultSets;
286 }
287
288
289
290
291 @Deprecated
292 public String[] getResulSets() {
293 return resultSets;
294 }
295
296 public BoundSql getBoundSql(Object parameterObject) {
297 BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
298 List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
299 if (parameterMappings == null || parameterMappings.isEmpty()) {
300 boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
301 }
302
303
304 for (ParameterMapping pm : boundSql.getParameterMappings()) {
305 String rmId = pm.getResultMapId();
306 if (rmId != null) {
307 ResultMap rm = configuration.getResultMap(rmId);
308 if (rm != null) {
309 hasNestedResultMaps |= rm.hasNestedResultMaps();
310 }
311 }
312 }
313
314 return boundSql;
315 }
316
317 private static String[] delimitedStringToArray(String in) {
318 if (in == null || in.trim().length() == 0) {
319 return null;
320 } else {
321 return in.split(",");
322 }
323 }
324
325 }