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

mybatis / ibatis-2 / 730

28 Dec 2025 10:16PM UTC coverage: 65.615% (+0.5%) from 65.146%
730

push

github

web-flow
Update README.md

1602 of 2802 branches covered (57.17%)

5053 of 7701 relevant lines covered (65.61%)

0.66 hits per line

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

85.5
/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 ("FORWARD_ONLY".equals(resultSetType)) {
×
137
        statement.setResultSetType(Integer.valueOf(ResultSet.TYPE_FORWARD_ONLY));
×
138
      } else if ("SCROLL_INSENSITIVE".equals(resultSetType)) {
×
139
        statement.setResultSetType(Integer.valueOf(ResultSet.TYPE_SCROLL_INSENSITIVE));
×
140
      } else if ("SCROLL_SENSITIVE".equals(resultSetType)) {
×
141
        statement.setResultSetType(Integer.valueOf(ResultSet.TYPE_SCROLL_SENSITIVE));
×
142
      }
143
    }
144
    if (fetchSize != null) {
1!
145
      statement.setFetchSize(fetchSize);
×
146
    }
147

148
    // set parameter class either from attribute or from map (make sure to match)
149
    ParameterMap parameterMap = statement.getParameterMap();
1✔
150
    if (parameterMap == null) {
1✔
151
      statement.setParameterClass(parameterClass);
1✔
152
    } else {
153
      statement.setParameterClass(parameterMap.getParameterClass());
1✔
154
    }
155

156
    // process SQL statement, including inline parameter maps
157
    errorContext.setMoreInfo("Check the SQL statement.");
1✔
158
    Sql sql = processor.getSql();
1✔
159
    setSqlForStatement(statement, sql);
1✔
160

161
    // set up either null result map or automatic result mapping
162
    ResultMap resultMap = statement.getResultMap();
1✔
163
    if (resultMap == null && resultClass == null) {
1✔
164
      statement.setResultMap(null);
1✔
165
    } else if (resultMap == null) {
1✔
166
      resultMap = buildAutoResultMap(allowRemapping, statement, resultClass, xmlResultName);
1✔
167
      statement.setResultMap(resultMap);
1✔
168
      if (additionalResultClasses != null) {
1!
169
        for (Class additionalResultClass : additionalResultClasses) {
1✔
170
          statement.addResultMap(buildAutoResultMap(allowRemapping, statement, additionalResultClass, xmlResultName));
1✔
171
        }
172
      }
173

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

197
  /**
198
   * Sets the select key statement.
199
   *
200
   * @param processor
201
   *          the processor
202
   * @param resultClassName
203
   *          the result class name
204
   * @param keyPropName
205
   *          the key prop name
206
   * @param runAfterSQL
207
   *          the run after SQL
208
   * @param type
209
   *          the type
210
   */
211
  public void setSelectKeyStatement(SqlSource processor, String resultClassName, String keyPropName,
212
      boolean runAfterSQL, String type) {
213
    if (rootStatement instanceof InsertStatement) {
1!
214
      InsertStatement insertStatement = ((InsertStatement) rootStatement);
1✔
215
      Class parameterClass = insertStatement.getParameterClass();
1✔
216
      errorContext.setActivity("parsing a select key");
1✔
217
      SelectKeyStatement selectKeyStatement = new SelectKeyStatement();
1✔
218
      resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
1✔
219
      Class resultClass = null;
1✔
220

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

247
      // process SQL statement, including inline parameter maps
248
      errorContext.setMoreInfo("Check the select key SQL statement.");
1✔
249
      Sql sql = processor.getSql();
1✔
250
      setSqlForStatement(selectKeyStatement, sql);
1✔
251
      ResultMap resultMap;
252
      resultMap = new AutoResultMap(client.getDelegate(), false);
1✔
253
      resultMap.setId(selectKeyStatement.getId() + "-AutoResultMap");
1✔
254
      resultMap.setResultClass(resultClass);
1✔
255
      resultMap.setResource(selectKeyStatement.getResource());
1✔
256
      selectKeyStatement.setResultMap(resultMap);
1✔
257
      errorContext.setMoreInfo(null);
1✔
258
      insertStatement.setSelectKeyStatement(selectKeyStatement);
1✔
259
    } else {
1✔
260
      throw new SqlMapException("You cant set a select key statement on statement named " + rootStatement.getId()
×
261
          + " because it is not an InsertStatement.");
262
    }
263
  }
1✔
264

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

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

315
  }
1✔
316

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

341
  /**
342
   * Gets the mapped statement.
343
   *
344
   * @return the mapped statement
345
   */
346
  public MappedStatement getMappedStatement() {
347
    return mappedStatement;
×
348
  }
349
}
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