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

mybatis / ibatis-2 / 736

29 Dec 2025 12:23AM UTC coverage: 65.571% (-0.03%) from 65.597%
736

push

github

web-flow
Merge pull request #339 from hazendaz/master

Key set cleanup to use size directly increasing speed for same result

1598 of 2797 branches covered (57.13%)

4 of 4 new or added lines in 1 file covered. (100.0%)

149 existing lines in 7 files now uncovered.

5047 of 7697 relevant lines covered (65.57%)

0.66 hits per line

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

84.09
/src/main/java/com/ibatis/sqlmap/engine/config/MappedStatementConfig.java
1
/*
2
 * Copyright 2004-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 com.ibatis.sqlmap.engine.config;
17

18
import com.ibatis.common.beans.Probe;
19
import com.ibatis.common.beans.ProbeFactory;
20
import com.ibatis.common.resources.Resources;
21
import com.ibatis.sqlmap.client.SqlMapException;
22
import com.ibatis.sqlmap.engine.cache.CacheModel;
23
import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
24
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
25
import com.ibatis.sqlmap.engine.mapping.parameter.InlineParameterMapParser;
26
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
27
import com.ibatis.sqlmap.engine.mapping.result.AutoResultMap;
28
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
29
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
30
import com.ibatis.sqlmap.engine.mapping.sql.SqlText;
31
import com.ibatis.sqlmap.engine.mapping.sql.dynamic.DynamicSql;
32
import com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql;
33
import com.ibatis.sqlmap.engine.mapping.sql.stat.StaticSql;
34
import com.ibatis.sqlmap.engine.mapping.statement.CachingStatement;
35
import com.ibatis.sqlmap.engine.mapping.statement.InsertStatement;
36
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
37
import com.ibatis.sqlmap.engine.mapping.statement.SelectKeyStatement;
38
import com.ibatis.sqlmap.engine.scope.ErrorContext;
39
import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
40

41
import java.sql.ResultSet;
42
import java.util.Arrays;
43
import java.util.List;
44

45
/**
46
 * The Class MappedStatementConfig.
47
 */
48
public class MappedStatementConfig {
49

50
  /** The Constant PROBE. */
51
  private static final Probe PROBE = ProbeFactory.getProbe();
1✔
52

53
  /** The Constant PARAM_PARSER. */
54
  private static final InlineParameterMapParser PARAM_PARSER = new InlineParameterMapParser();
1✔
55

56
  /** The error context. */
57
  private ErrorContext errorContext;
58

59
  /** The client. */
60
  private SqlMapClientImpl client;
61

62
  /** The type handler factory. */
63
  private TypeHandlerFactory typeHandlerFactory;
64

65
  /** The mapped statement. */
66
  private MappedStatement mappedStatement;
67

68
  /** The root statement. */
69
  private MappedStatement rootStatement;
70

71
  /**
72
   * Instantiates a new mapped statement config.
73
   *
74
   * @param config
75
   *          the config
76
   * @param id
77
   *          the id
78
   * @param statement
79
   *          the statement
80
   * @param processor
81
   *          the processor
82
   * @param parameterMapName
83
   *          the parameter map name
84
   * @param parameterClass
85
   *          the parameter class
86
   * @param resultMapName
87
   *          the result map name
88
   * @param additionalResultMapNames
89
   *          the additional result map names
90
   * @param resultClass
91
   *          the result class
92
   * @param additionalResultClasses
93
   *          the additional result classes
94
   * @param cacheModelName
95
   *          the cache model name
96
   * @param resultSetType
97
   *          the result set type
98
   * @param fetchSize
99
   *          the fetch size
100
   * @param allowRemapping
101
   *          the allow remapping
102
   * @param timeout
103
   *          the timeout
104
   * @param defaultStatementTimeout
105
   *          the default statement timeout
106
   * @param xmlResultName
107
   *          the xml result name
108
   */
109
  MappedStatementConfig(SqlMapConfiguration config, String id, MappedStatement statement, SqlSource processor,
110
      String parameterMapName, Class parameterClass, String resultMapName, String[] additionalResultMapNames,
111
      Class resultClass, Class[] additionalResultClasses, String cacheModelName, String resultSetType,
112
      Integer fetchSize, boolean allowRemapping, Integer timeout, Integer defaultStatementTimeout,
113
      String xmlResultName) {
1✔
114
    this.errorContext = config.getErrorContext();
1✔
115
    this.client = config.getClient();
1✔
116
    SqlMapExecutorDelegate delegate = client.getDelegate();
1✔
117
    this.typeHandlerFactory = config.getTypeHandlerFactory();
1✔
118
    errorContext.setActivity("parsing a mapped statement");
1✔
119
    errorContext.setObjectId(id + " statement");
1✔
120
    errorContext.setMoreInfo("Check the result map name.");
1✔
121
    if (resultMapName != null) {
1✔
122
      statement.setResultMap(client.getDelegate().getResultMap(resultMapName));
1✔
123
      if (additionalResultMapNames != null) {
1!
124
        for (String additionalResultMapName : additionalResultMapNames) {
1✔
125
          statement.addResultMap(client.getDelegate().getResultMap(additionalResultMapName));
1✔
126
        }
127
      }
128
    }
129
    errorContext.setMoreInfo("Check the parameter map name.");
1✔
130
    if (parameterMapName != null) {
1✔
131
      statement.setParameterMap(client.getDelegate().getParameterMap(parameterMapName));
1✔
132
    }
133
    statement.setId(id);
1✔
134
    statement.setResource(errorContext.getResource());
1✔
135
    if (resultSetType != null) {
1!
136
      if (resultSetType != null) {
×
137
        switch (resultSetType) {
×
138
          case "FORWARD_ONLY":
139
            statement.setResultSetType(Integer.valueOf(ResultSet.TYPE_FORWARD_ONLY));
×
140
            break;
×
141
          case "SCROLL_INSENSITIVE":
UNCOV
142
            statement.setResultSetType(Integer.valueOf(ResultSet.TYPE_SCROLL_INSENSITIVE));
×
UNCOV
143
            break;
×
144
          case "SCROLL_SENSITIVE":
145
            statement.setResultSetType(Integer.valueOf(ResultSet.TYPE_SCROLL_SENSITIVE));
×
UNCOV
146
            break;
×
147
          default:
148
            break;
149
        }
150
      }
151
    }
152
    if (fetchSize != null) {
1!
UNCOV
153
      statement.setFetchSize(fetchSize);
×
154
    }
155

156
    // set parameter class either from attribute or from map (make sure to match)
157
    ParameterMap parameterMap = statement.getParameterMap();
1✔
158
    if (parameterMap == null) {
1✔
159
      statement.setParameterClass(parameterClass);
1✔
160
    } else {
161
      statement.setParameterClass(parameterMap.getParameterClass());
1✔
162
    }
163

164
    // process SQL statement, including inline parameter maps
165
    errorContext.setMoreInfo("Check the SQL statement.");
1✔
166
    Sql sql = processor.getSql();
1✔
167
    setSqlForStatement(statement, sql);
1✔
168

169
    // set up either null result map or automatic result mapping
170
    ResultMap resultMap = statement.getResultMap();
1✔
171
    if (resultMap == null && resultClass == null) {
1✔
172
      statement.setResultMap(null);
1✔
173
    } else if (resultMap == null) {
1✔
174
      resultMap = buildAutoResultMap(allowRemapping, statement, resultClass, xmlResultName);
1✔
175
      statement.setResultMap(resultMap);
1✔
176
      if (additionalResultClasses != null) {
1!
177
        for (Class additionalResultClass : additionalResultClasses) {
1✔
178
          statement.addResultMap(buildAutoResultMap(allowRemapping, statement, additionalResultClass, xmlResultName));
1✔
179
        }
180
      }
181

182
    }
183
    statement.setTimeout(defaultStatementTimeout);
1✔
184
    if (timeout != null) {
1!
185
      try {
UNCOV
186
        statement.setTimeout(timeout);
×
UNCOV
187
      } catch (NumberFormatException e) {
×
UNCOV
188
        throw new SqlMapException(
×
UNCOV
189
            "Specified timeout value for statement " + statement.getId() + " is not a valid integer");
×
UNCOV
190
      }
×
191
    }
192
    errorContext.setMoreInfo(null);
1✔
193
    errorContext.setObjectId(null);
1✔
194
    statement.setSqlMapClient(client);
1✔
195
    if (cacheModelName != null && !cacheModelName.isEmpty() && client.getDelegate().isCacheModelsEnabled()) {
1!
196
      CacheModel cacheModel = client.getDelegate().getCacheModel(cacheModelName);
1✔
197
      mappedStatement = new CachingStatement(statement, cacheModel);
1✔
198
    } else {
1✔
199
      mappedStatement = statement;
1✔
200
    }
201
    rootStatement = statement;
1✔
202
    delegate.addMappedStatement(mappedStatement);
1✔
203
  }
1✔
204

205
  /**
206
   * Sets the select key statement.
207
   *
208
   * @param processor
209
   *          the processor
210
   * @param resultClassName
211
   *          the result class name
212
   * @param keyPropName
213
   *          the key prop name
214
   * @param runAfterSQL
215
   *          the run after SQL
216
   * @param type
217
   *          the type
218
   */
219
  public void setSelectKeyStatement(SqlSource processor, String resultClassName, String keyPropName,
220
      boolean runAfterSQL, String type) {
221
    if (!(rootStatement instanceof InsertStatement)) {
1!
UNCOV
222
      throw new SqlMapException("You cant set a select key statement on statement named " + rootStatement.getId()
×
223
          + " because it is not an InsertStatement.");
224
    }
225
    InsertStatement insertStatement = ((InsertStatement) rootStatement);
1✔
226
    Class parameterClass = insertStatement.getParameterClass();
1✔
227
    errorContext.setActivity("parsing a select key");
1✔
228
    SelectKeyStatement selectKeyStatement = new SelectKeyStatement();
1✔
229
    resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
1✔
230
    Class resultClass = null;
1✔
231

232
    // get parameter and result maps
233
    selectKeyStatement.setSqlMapClient(client);
1✔
234
    selectKeyStatement.setId(insertStatement.getId() + "-SelectKey");
1✔
235
    selectKeyStatement.setResource(errorContext.getResource());
1✔
236
    selectKeyStatement.setKeyProperty(keyPropName);
1✔
237
    selectKeyStatement.setRunAfterSQL(runAfterSQL);
1✔
238
    // process the type (pre or post) attribute
239
    if (type != null) {
1✔
240
      selectKeyStatement.setRunAfterSQL("post".equals(type));
1✔
241
    }
242
    try {
243
      if (resultClassName != null) {
1!
244
        errorContext.setMoreInfo("Check the select key result class.");
1✔
245
        resultClass = Resources.classForName(resultClassName);
1✔
UNCOV
246
      } else if (keyPropName != null && parameterClass != null) {
×
UNCOV
247
        resultClass = PROBE.getPropertyTypeForSetter(parameterClass, selectKeyStatement.getKeyProperty());
×
248
      }
UNCOV
249
    } catch (ClassNotFoundException e) {
×
UNCOV
250
      throw new SqlMapException("Error.  Could not set result class.  Cause: " + e, e);
×
251
    }
1✔
252
    if (resultClass == null) {
1!
UNCOV
253
      resultClass = Object.class;
×
254
    }
255

256
    // process SQL statement, including inline parameter maps
257
    errorContext.setMoreInfo("Check the select key SQL statement.");
1✔
258
    Sql sql = processor.getSql();
1✔
259
    setSqlForStatement(selectKeyStatement, sql);
1✔
260
    ResultMap resultMap = new AutoResultMap(client.getDelegate(), false);
1✔
261
    resultMap.setId(selectKeyStatement.getId() + "-AutoResultMap");
1✔
262
    resultMap.setResultClass(resultClass);
1✔
263
    resultMap.setResource(selectKeyStatement.getResource());
1✔
264
    selectKeyStatement.setResultMap(resultMap);
1✔
265
    errorContext.setMoreInfo(null);
1✔
266
    insertStatement.setSelectKeyStatement(selectKeyStatement);
1✔
267
  }
1✔
268

269
  /**
270
   * Sets the sql for statement.
271
   *
272
   * @param statement
273
   *          the statement
274
   * @param sql
275
   *          the sql
276
   */
277
  private void setSqlForStatement(MappedStatement statement, Sql sql) {
278
    if (sql instanceof DynamicSql) {
1✔
279
      statement.setSql(sql);
1✔
280
    } else {
281
      applyInlineParameterMap(statement, sql.getSql(null, null));
1✔
282
    }
283
  }
1✔
284

285
  /**
286
   * Apply inline parameter map.
287
   *
288
   * @param statement
289
   *          the statement
290
   * @param sqlStatement
291
   *          the sql statement
292
   */
293
  private void applyInlineParameterMap(MappedStatement statement, String sqlStatement) {
294
    String newSql = sqlStatement;
1✔
295
    errorContext.setActivity("building an inline parameter map");
1✔
296
    ParameterMap parameterMap = statement.getParameterMap();
1✔
297
    errorContext.setMoreInfo("Check the inline parameters.");
1✔
298
    if (parameterMap == null) {
1✔
299
      ParameterMap map;
300
      map = new ParameterMap(client.getDelegate());
1✔
301
      map.setId(statement.getId() + "-InlineParameterMap");
1✔
302
      map.setParameterClass(statement.getParameterClass());
1✔
303
      map.setResource(statement.getResource());
1✔
304
      statement.setParameterMap(map);
1✔
305
      SqlText sqlText = PARAM_PARSER.parseInlineParameterMap(client.getDelegate().getTypeHandlerFactory(), newSql,
1✔
306
          statement.getParameterClass());
1✔
307
      newSql = sqlText.getText();
1✔
308
      List mappingList = Arrays.asList(sqlText.getParameterMappings());
1✔
309
      map.setParameterMappingList(mappingList);
1✔
310
    }
311
    Sql sql;
312
    if (SimpleDynamicSql.isSimpleDynamicSql(newSql)) {
1✔
313
      sql = new SimpleDynamicSql(client.getDelegate(), newSql);
1✔
314
    } else {
315
      sql = new StaticSql(newSql);
1✔
316
    }
317
    statement.setSql(sql);
1✔
318

319
  }
1✔
320

321
  /**
322
   * Builds the auto result map.
323
   *
324
   * @param allowRemapping
325
   *          the allow remapping
326
   * @param statement
327
   *          the statement
328
   * @param firstResultClass
329
   *          the first result class
330
   * @param xmlResultName
331
   *          the xml result name
332
   *
333
   * @return the result map
334
   */
335
  private ResultMap buildAutoResultMap(boolean allowRemapping, MappedStatement statement, Class firstResultClass,
336
      String xmlResultName) {
337
    ResultMap resultMap = new AutoResultMap(client.getDelegate(), allowRemapping);
1✔
338
    resultMap.setId(statement.getId() + "-AutoResultMap");
1✔
339
    resultMap.setResultClass(firstResultClass);
1✔
340
    resultMap.setXmlName(xmlResultName);
1✔
341
    resultMap.setResource(statement.getResource());
1✔
342
    return resultMap;
1✔
343
  }
344

345
  /**
346
   * Gets the mapped statement.
347
   *
348
   * @return the mapped statement
349
   */
350
  public MappedStatement getMappedStatement() {
UNCOV
351
    return mappedStatement;
×
352
  }
353
}
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