• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / mybatis-3 / 3081

26 Dec 2025 06:25PM UTC coverage: 87.396% (+0.01%) from 87.382%
3081

Pull #3579

github

web-flow
Merge 1da72b86e into 17d9f882a
Pull Request #3579: Reduce the memory usage of inline ResultMap

3845 of 4659 branches covered (82.53%)

20 of 20 new or added lines in 4 files covered. (100.0%)

9 existing lines in 3 files now uncovered.

9957 of 11393 relevant lines covered (87.4%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.04
/src/main/java/org/apache/ibatis/mapping/MappedStatement.java
1
/*
2
 *    Copyright 2009-2025 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

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.reflection.ParamNameResolver;
29
import org.apache.ibatis.scripting.LanguageDriver;
30
import org.apache.ibatis.session.Configuration;
31

32
/**
33
 * @author Clinton Begin
34
 */
35
public final class MappedStatement {
36

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

63
  MappedStatement() {
1✔
64
    // constructor disabled
65
  }
1✔
66

67
  public static class Builder {
1✔
68
    private final MappedStatement mappedStatement = new MappedStatement();
1✔
69

70
    public Builder(Configuration configuration, String id, SqlSource sqlSource, SqlCommandType sqlCommandType) {
1✔
71
      mappedStatement.configuration = configuration;
1✔
72
      mappedStatement.id = id;
1✔
73
      mappedStatement.sqlSource = sqlSource;
1✔
74
      mappedStatement.statementType = StatementType.PREPARED;
1✔
75
      mappedStatement.resultSetType = ResultSetType.DEFAULT;
1✔
76
      mappedStatement.parameterMap = new ParameterMap.Builder(configuration, "defaultParameterMap", null,
1✔
77
          new ArrayList<>()).build();
1✔
78
      mappedStatement.resultMaps = new ArrayList<>();
1✔
79
      mappedStatement.sqlCommandType = sqlCommandType;
1✔
80
      mappedStatement.keyGenerator = configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType)
1✔
81
          ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
1✔
82
      String logId = id;
1✔
83
      if (configuration.getLogPrefix() != null) {
1!
UNCOV
84
        logId = configuration.getLogPrefix() + id;
×
85
      }
86
      mappedStatement.statementLog = LogFactory.getLog(logId);
1✔
87
      mappedStatement.lang = configuration.getDefaultScriptingLanguageInstance();
1✔
88
    }
1✔
89

90
    public Builder resource(String resource) {
91
      mappedStatement.resource = resource;
1✔
92
      return this;
1✔
93
    }
94

95
    public String id() {
UNCOV
96
      return mappedStatement.id;
×
97
    }
98

99
    public Builder parameterMap(ParameterMap parameterMap) {
100
      mappedStatement.parameterMap = parameterMap;
1✔
101
      return this;
1✔
102
    }
103

104
    public Builder resultMaps(List<ResultMap> resultMaps) {
105
      mappedStatement.resultMaps = resultMaps;
1✔
106
      for (ResultMap resultMap : resultMaps) {
1✔
107
        mappedStatement.hasNestedResultMaps = mappedStatement.hasNestedResultMaps || resultMap.hasNestedResultMaps();
1!
108
      }
1✔
109
      return this;
1✔
110
    }
111

112
    public Builder fetchSize(Integer fetchSize) {
113
      mappedStatement.fetchSize = fetchSize;
1✔
114
      return this;
1✔
115
    }
116

117
    public Builder timeout(Integer timeout) {
118
      mappedStatement.timeout = timeout;
1✔
119
      return this;
1✔
120
    }
121

122
    public Builder statementType(StatementType statementType) {
123
      mappedStatement.statementType = statementType;
1✔
124
      return this;
1✔
125
    }
126

127
    public Builder resultSetType(ResultSetType resultSetType) {
128
      mappedStatement.resultSetType = resultSetType == null ? ResultSetType.DEFAULT : resultSetType;
1✔
129
      return this;
1✔
130
    }
131

132
    public Builder cache(Cache cache) {
133
      mappedStatement.cache = cache;
1✔
134
      return this;
1✔
135
    }
136

137
    public Builder flushCacheRequired(boolean flushCacheRequired) {
138
      mappedStatement.flushCacheRequired = flushCacheRequired;
1✔
139
      return this;
1✔
140
    }
141

142
    public Builder useCache(boolean useCache) {
143
      mappedStatement.useCache = useCache;
1✔
144
      return this;
1✔
145
    }
146

147
    public Builder resultOrdered(boolean resultOrdered) {
148
      mappedStatement.resultOrdered = resultOrdered;
1✔
149
      return this;
1✔
150
    }
151

152
    public Builder keyGenerator(KeyGenerator keyGenerator) {
153
      mappedStatement.keyGenerator = keyGenerator;
1✔
154
      return this;
1✔
155
    }
156

157
    public Builder keyProperty(String keyProperty) {
158
      mappedStatement.keyProperties = delimitedStringToArray(keyProperty);
1✔
159
      return this;
1✔
160
    }
161

162
    public Builder keyColumn(String keyColumn) {
163
      mappedStatement.keyColumns = delimitedStringToArray(keyColumn);
1✔
164
      return this;
1✔
165
    }
166

167
    public Builder databaseId(String databaseId) {
168
      mappedStatement.databaseId = databaseId;
1✔
169
      return this;
1✔
170
    }
171

172
    public Builder lang(LanguageDriver driver) {
173
      mappedStatement.lang = driver;
1✔
174
      return this;
1✔
175
    }
176

177
    public Builder resultSets(String resultSet) {
178
      mappedStatement.resultSets = delimitedStringToArray(resultSet);
1✔
179
      return this;
1✔
180
    }
181

182
    public Builder dirtySelect(boolean dirtySelect) {
183
      mappedStatement.dirtySelect = dirtySelect;
1✔
184
      return this;
1✔
185
    }
186

187
    public Builder paramNameResolver(ParamNameResolver paramNameResolver) {
188
      mappedStatement.paramNameResolver = paramNameResolver;
1✔
189
      return this;
1✔
190
    }
191

192
    /**
193
     * Resul sets.
194
     *
195
     * @param resultSet
196
     *          the result set
197
     *
198
     * @return the builder
199
     *
200
     * @deprecated Use {@link #resultSets}
201
     */
202
    @Deprecated
203
    public Builder resulSets(String resultSet) {
204
      mappedStatement.resultSets = delimitedStringToArray(resultSet);
×
UNCOV
205
      return this;
×
206
    }
207

208
    public MappedStatement build() {
209
      assert mappedStatement.configuration != null;
1!
210
      assert mappedStatement.id != null;
1!
211
      assert mappedStatement.sqlSource != null;
1!
212
      assert mappedStatement.lang != null;
1!
213
      mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
1✔
214
      return mappedStatement;
1✔
215
    }
216
  }
217

218
  public KeyGenerator getKeyGenerator() {
219
    return keyGenerator;
1✔
220
  }
221

222
  public SqlCommandType getSqlCommandType() {
223
    return sqlCommandType;
1✔
224
  }
225

226
  public String getResource() {
227
    return resource;
1✔
228
  }
229

230
  public Configuration getConfiguration() {
231
    return configuration;
1✔
232
  }
233

234
  public String getId() {
235
    return id;
1✔
236
  }
237

238
  public boolean hasNestedResultMaps() {
UNCOV
239
    return hasNestedResultMaps;
×
240
  }
241

242
  public Integer getFetchSize() {
243
    return fetchSize;
1✔
244
  }
245

246
  public Integer getTimeout() {
247
    return timeout;
1✔
248
  }
249

250
  public StatementType getStatementType() {
251
    return statementType;
1✔
252
  }
253

254
  public ResultSetType getResultSetType() {
255
    return resultSetType;
1✔
256
  }
257

258
  public SqlSource getSqlSource() {
259
    return sqlSource;
1✔
260
  }
261

262
  public ParameterMap getParameterMap() {
263
    return parameterMap;
1✔
264
  }
265

266
  public List<ResultMap> getResultMaps() {
267
    return resultMaps;
1✔
268
  }
269

270
  public Cache getCache() {
271
    return cache;
1✔
272
  }
273

274
  public boolean isFlushCacheRequired() {
275
    return flushCacheRequired;
1✔
276
  }
277

278
  public boolean isUseCache() {
279
    return useCache;
1✔
280
  }
281

282
  public boolean isResultOrdered() {
283
    return resultOrdered;
1✔
284
  }
285

286
  public String getDatabaseId() {
287
    return databaseId;
1✔
288
  }
289

290
  public String[] getKeyProperties() {
291
    return keyProperties;
1✔
292
  }
293

294
  public String[] getKeyColumns() {
295
    return keyColumns;
1✔
296
  }
297

298
  public Log getStatementLog() {
299
    return statementLog;
1✔
300
  }
301

302
  public LanguageDriver getLang() {
303
    return lang;
1✔
304
  }
305

306
  public String[] getResultSets() {
307
    return resultSets;
1✔
308
  }
309

310
  public boolean isDirtySelect() {
311
    return dirtySelect;
1✔
312
  }
313

314
  public ParamNameResolver getParamNameResolver() {
315
    return paramNameResolver;
1✔
316
  }
317

318
  /**
319
   * Gets the resul sets.
320
   *
321
   * @return the resul sets
322
   *
323
   * @deprecated Use {@link #getResultSets()}
324
   */
325
  @Deprecated
326
  public String[] getResulSets() {
UNCOV
327
    return resultSets;
×
328
  }
329

330
  public BoundSql getBoundSql(Object parameterObject) {
331
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
1✔
332
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
1✔
333
    if (parameterMappings == null || parameterMappings.isEmpty()) {
1✔
334
      boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
1✔
335
    }
336

337
    // check for nested result maps in parameter mappings (issue #30)
338
    for (ParameterMapping pm : boundSql.getParameterMappings()) {
1✔
339
      String rmId = pm.getResultMapId();
1✔
340
      if (rmId != null) {
1!
341
        ResultMap rm = configuration.getResultMap(rmId);
×
342
        if (rm != null) {
×
UNCOV
343
          hasNestedResultMaps |= rm.hasNestedResultMaps();
×
344
        }
345
      }
346
    }
1✔
347

348
    return boundSql;
1✔
349
  }
350

351
  private static String[] delimitedStringToArray(String in) {
352
    if (in == null || in.trim().length() == 0) {
1✔
353
      return null;
1✔
354
    }
355
    return in.split(",");
1✔
356
  }
357

358
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc