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

mybatis / ibatis-2 / #341

08 Sep 2023 11:16PM UTC coverage: 64.938% (+0.03%) from 64.913%
#341

push

github

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

fixes #174, update GHA, maven wrapper, fix EOL markers, do not use star imports

5047 of 7772 relevant lines covered (64.94%)

0.65 hits per line

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

85.61
/src/main/java/com/ibatis/sqlmap/engine/config/MappedStatementConfig.java
1
/*
2
 * Copyright 2004-2023 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 (int i = 0; i < additionalResultMapNames.length; i++) {
1✔
125
          statement.addResultMap(client.getDelegate().getResultMap(additionalResultMapNames[i]));
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 = (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 (int i = 0; i < additionalResultClasses.length; i++) {
1✔
170
          statement
1✔
171
              .addResultMap(buildAutoResultMap(allowRemapping, statement, additionalResultClasses[i], xmlResultName));
1✔
172
        }
173
      }
174

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

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

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

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

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

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

316
  }
1✔
317

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

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