• 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

89.15
/src/main/java/com/ibatis/sqlmap/engine/config/SqlMapConfiguration.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.ClassInfo;
19
import com.ibatis.common.beans.Probe;
20
import com.ibatis.common.beans.ProbeFactory;
21
import com.ibatis.common.resources.Resources;
22
import com.ibatis.sqlmap.client.SqlMapException;
23
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
24
import com.ibatis.sqlmap.engine.accessplan.AccessPlanFactory;
25
import com.ibatis.sqlmap.engine.cache.CacheController;
26
import com.ibatis.sqlmap.engine.cache.CacheModel;
27
import com.ibatis.sqlmap.engine.cache.fifo.FifoCacheController;
28
import com.ibatis.sqlmap.engine.cache.lru.LruCacheController;
29
import com.ibatis.sqlmap.engine.cache.memory.MemoryCacheController;
30
import com.ibatis.sqlmap.engine.datasource.DbcpDataSourceFactory;
31
import com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory;
32
import com.ibatis.sqlmap.engine.datasource.SimpleDataSourceFactory;
33
import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
34
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
35
import com.ibatis.sqlmap.engine.mapping.result.Discriminator;
36
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
37
import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
38
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
39
import com.ibatis.sqlmap.engine.scope.ErrorContext;
40
import com.ibatis.sqlmap.engine.transaction.TransactionManager;
41
import com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig;
42
import com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransactionConfig;
43
import com.ibatis.sqlmap.engine.transaction.jta.JtaTransactionConfig;
44
import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
45
import com.ibatis.sqlmap.engine.type.DomCollectionTypeMarker;
46
import com.ibatis.sqlmap.engine.type.DomTypeMarker;
47
import com.ibatis.sqlmap.engine.type.TypeHandler;
48
import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
49
import com.ibatis.sqlmap.engine.type.XmlCollectionTypeMarker;
50
import com.ibatis.sqlmap.engine.type.XmlTypeMarker;
51

52
import java.util.Iterator;
53

54
/**
55
 * The Class SqlMapConfiguration.
56
 */
57
public class SqlMapConfiguration {
58

59
  /** The Constant PROBE. */
60
  private static final Probe PROBE = ProbeFactory.getProbe();
1✔
61

62
  /** The error context. */
63
  private ErrorContext errorContext;
64

65
  /** The delegate. */
66
  private SqlMapExecutorDelegate delegate;
67

68
  /** The type handler factory. */
69
  private TypeHandlerFactory typeHandlerFactory;
70

71
  /** The client. */
72
  private SqlMapClientImpl client;
73

74
  /** The default statement timeout. */
75
  private Integer defaultStatementTimeout;
76

77
  /**
78
   * Instantiates a new sql map configuration.
79
   */
80
  public SqlMapConfiguration() {
1✔
81
    errorContext = new ErrorContext();
1✔
82
    delegate = new SqlMapExecutorDelegate();
1✔
83
    typeHandlerFactory = delegate.getTypeHandlerFactory();
1✔
84
    client = new SqlMapClientImpl(delegate);
1✔
85
    registerDefaultTypeAliases();
1✔
86
  }
1✔
87

88
  /**
89
   * Gets the type handler factory.
90
   *
91
   * @return the type handler factory
92
   */
93
  public TypeHandlerFactory getTypeHandlerFactory() {
94
    return typeHandlerFactory;
1✔
95
  }
96

97
  /**
98
   * Gets the error context.
99
   *
100
   * @return the error context
101
   */
102
  public ErrorContext getErrorContext() {
103
    return errorContext;
1✔
104
  }
105

106
  /**
107
   * Gets the client.
108
   *
109
   * @return the client
110
   */
111
  public SqlMapClientImpl getClient() {
112
    return client;
1✔
113
  }
114

115
  /**
116
   * Gets the delegate.
117
   *
118
   * @return the delegate
119
   */
120
  public SqlMapExecutorDelegate getDelegate() {
121
    return delegate;
1✔
122
  }
123

124
  /**
125
   * Sets the class info cache enabled.
126
   *
127
   * @param classInfoCacheEnabled
128
   *          the new class info cache enabled
129
   */
130
  public void setClassInfoCacheEnabled(boolean classInfoCacheEnabled) {
131
    errorContext.setActivity("setting class info cache enabled/disabled");
1✔
132
    ClassInfo.setCacheEnabled(classInfoCacheEnabled);
1✔
133
  }
1✔
134

135
  /**
136
   * Sets the lazy loading enabled.
137
   *
138
   * @param lazyLoadingEnabled
139
   *          the new lazy loading enabled
140
   */
141
  public void setLazyLoadingEnabled(boolean lazyLoadingEnabled) {
142
    errorContext.setActivity("setting lazy loading enabled/disabled");
1✔
143
    client.getDelegate().setLazyLoadingEnabled(lazyLoadingEnabled);
1✔
144
  }
1✔
145

146
  /**
147
   * Sets the statement caching enabled.
148
   *
149
   * @param statementCachingEnabled
150
   *          the new statement caching enabled
151
   */
152
  public void setStatementCachingEnabled(boolean statementCachingEnabled) {
153
    errorContext.setActivity("setting statement caching enabled/disabled");
1✔
154
    client.getDelegate().setStatementCacheEnabled(statementCachingEnabled);
1✔
155
  }
1✔
156

157
  /**
158
   * Sets the cache models enabled.
159
   *
160
   * @param cacheModelsEnabled
161
   *          the new cache models enabled
162
   */
163
  public void setCacheModelsEnabled(boolean cacheModelsEnabled) {
164
    errorContext.setActivity("setting cache models enabled/disabled");
1✔
165
    client.getDelegate().setCacheModelsEnabled(cacheModelsEnabled);
1✔
166
  }
1✔
167

168
  /**
169
   * Sets the enhancement enabled.
170
   *
171
   * @param enhancementEnabled
172
   *          the new enhancement enabled
173
   */
174
  public void setEnhancementEnabled(boolean enhancementEnabled) {
175
    errorContext.setActivity("setting enhancement enabled/disabled");
1✔
176
    try {
177
      enhancementEnabled = enhancementEnabled && Resources.classForName("net.sf.cglib.proxy.InvocationHandler") != null;
1!
178
    } catch (ClassNotFoundException e) {
×
179
      enhancementEnabled = false;
×
180
    }
1✔
181
    client.getDelegate().setEnhancementEnabled(enhancementEnabled);
1✔
182
    AccessPlanFactory.setBytecodeEnhancementEnabled(enhancementEnabled);
1✔
183
  }
1✔
184

185
  /**
186
   * Sets the use column label.
187
   *
188
   * @param useColumnLabel
189
   *          the new use column label
190
   */
191
  public void setUseColumnLabel(boolean useColumnLabel) {
192
    client.getDelegate().setUseColumnLabel(useColumnLabel);
1✔
193
  }
1✔
194

195
  /**
196
   * Sets the force multiple result set support.
197
   *
198
   * @param forceMultipleResultSetSupport
199
   *          the new force multiple result set support
200
   */
201
  public void setForceMultipleResultSetSupport(boolean forceMultipleResultSetSupport) {
202
    client.getDelegate().setForceMultipleResultSetSupport(forceMultipleResultSetSupport);
1✔
203
  }
1✔
204

205
  /**
206
   * Sets the default statement timeout.
207
   *
208
   * @param defaultTimeout
209
   *          the new default statement timeout
210
   */
211
  public void setDefaultStatementTimeout(Integer defaultTimeout) {
212
    errorContext.setActivity("setting default timeout");
1✔
213
    if (defaultTimeout != null) {
1!
214
      try {
215
        defaultStatementTimeout = defaultTimeout;
×
216
      } catch (NumberFormatException e) {
×
217
        throw new SqlMapException("Specified defaultStatementTimeout is not a valid integer");
×
218
      }
×
219
    }
220
  }
1✔
221

222
  /**
223
   * Sets the transaction manager.
224
   *
225
   * @param txManager
226
   *          the new transaction manager
227
   */
228
  public void setTransactionManager(TransactionManager txManager) {
229
    delegate.setTxManager(txManager);
1✔
230
  }
1✔
231

232
  /**
233
   * Sets the result object factory.
234
   *
235
   * @param rof
236
   *          the new result object factory
237
   */
238
  public void setResultObjectFactory(ResultObjectFactory rof) {
239
    delegate.setResultObjectFactory(rof);
1✔
240
  }
1✔
241

242
  /**
243
   * New type handler.
244
   *
245
   * @param javaType
246
   *          the java type
247
   * @param jdbcType
248
   *          the jdbc type
249
   * @param callback
250
   *          the callback
251
   */
252
  public void newTypeHandler(Class javaType, String jdbcType, Object callback) {
253
    try {
254
      errorContext.setActivity("building a building custom type handler");
1✔
255
      TypeHandlerFactory typeHandlerFactory = client.getDelegate().getTypeHandlerFactory();
1✔
256
      TypeHandler typeHandler;
257
      if (callback instanceof TypeHandlerCallback) {
1!
258
        typeHandler = new CustomTypeHandler((TypeHandlerCallback) callback);
1✔
259
      } else if (callback instanceof TypeHandler) {
×
260
        typeHandler = (TypeHandler) callback;
×
261
      } else {
262
        throw new RuntimeException(
×
263
            "The object '" + callback + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
264
      }
265
      errorContext.setMoreInfo("Check the javaType attribute '" + javaType + "' (must be a classname) or the jdbcType '"
1✔
266
          + jdbcType + "' (must be a JDBC type name).");
267
      if (jdbcType != null && jdbcType.length() > 0) {
1!
268
        typeHandlerFactory.register(javaType, jdbcType, typeHandler);
1✔
269
      } else {
270
        typeHandlerFactory.register(javaType, typeHandler);
×
271
      }
272
    } catch (Exception e) {
×
273
      throw new SqlMapException("Error registering occurred.  Cause: " + e, e);
×
274
    }
1✔
275
    errorContext.setMoreInfo(null);
1✔
276
    errorContext.setObjectId(null);
1✔
277
  }
1✔
278

279
  /**
280
   * New cache model config.
281
   *
282
   * @param id
283
   *          the id
284
   * @param controller
285
   *          the controller
286
   * @param readOnly
287
   *          the read only
288
   * @param serialize
289
   *          the serialize
290
   *
291
   * @return the cache model config
292
   */
293
  public CacheModelConfig newCacheModelConfig(String id, CacheController controller, boolean readOnly,
294
      boolean serialize) {
295
    return new CacheModelConfig(this, id, controller, readOnly, serialize);
1✔
296
  }
297

298
  /**
299
   * New parameter map config.
300
   *
301
   * @param id
302
   *          the id
303
   * @param parameterClass
304
   *          the parameter class
305
   *
306
   * @return the parameter map config
307
   */
308
  public ParameterMapConfig newParameterMapConfig(String id, Class parameterClass) {
309
    return new ParameterMapConfig(this, id, parameterClass);
1✔
310
  }
311

312
  /**
313
   * New result map config.
314
   *
315
   * @param id
316
   *          the id
317
   * @param resultClass
318
   *          the result class
319
   * @param groupBy
320
   *          the group by
321
   * @param extended
322
   *          the extended
323
   * @param xmlName
324
   *          the xml name
325
   *
326
   * @return the result map config
327
   */
328
  public ResultMapConfig newResultMapConfig(String id, Class resultClass, String groupBy, String extended,
329
      String xmlName) {
330
    return new ResultMapConfig(this, id, resultClass, groupBy, extended, xmlName);
1✔
331
  }
332

333
  /**
334
   * New mapped statement config.
335
   *
336
   * @param id
337
   *          the id
338
   * @param statement
339
   *          the statement
340
   * @param processor
341
   *          the processor
342
   * @param parameterMapName
343
   *          the parameter map name
344
   * @param parameterClass
345
   *          the parameter class
346
   * @param resultMapName
347
   *          the result map name
348
   * @param additionalResultMapNames
349
   *          the additional result map names
350
   * @param resultClass
351
   *          the result class
352
   * @param additionalResultClasses
353
   *          the additional result classes
354
   * @param resultSetType
355
   *          the result set type
356
   * @param fetchSize
357
   *          the fetch size
358
   * @param allowRemapping
359
   *          the allow remapping
360
   * @param timeout
361
   *          the timeout
362
   * @param cacheModelName
363
   *          the cache model name
364
   * @param xmlResultName
365
   *          the xml result name
366
   *
367
   * @return the mapped statement config
368
   */
369
  public MappedStatementConfig newMappedStatementConfig(String id, MappedStatement statement, SqlSource processor,
370
      String parameterMapName, Class parameterClass, String resultMapName, String[] additionalResultMapNames,
371
      Class resultClass, Class[] additionalResultClasses, String resultSetType, Integer fetchSize,
372
      boolean allowRemapping, Integer timeout, String cacheModelName, String xmlResultName) {
373
    return new MappedStatementConfig(this, id, statement, processor, parameterMapName, parameterClass, resultMapName,
1✔
374
        additionalResultMapNames, resultClass, additionalResultClasses, cacheModelName, resultSetType, fetchSize,
375
        allowRemapping, timeout, defaultStatementTimeout, xmlResultName);
376
  }
377

378
  /**
379
   * Finalize sql map config.
380
   */
381
  public void finalizeSqlMapConfig() {
382
    wireUpCacheModels();
1✔
383
    bindResultMapDiscriminators();
1✔
384
  }
1✔
385

386
  /**
387
   * Resolve type handler.
388
   *
389
   * @param typeHandlerFactory
390
   *          the type handler factory
391
   * @param clazz
392
   *          the clazz
393
   * @param propertyName
394
   *          the property name
395
   * @param javaType
396
   *          the java type
397
   * @param jdbcType
398
   *          the jdbc type
399
   *
400
   * @return the type handler
401
   */
402
  TypeHandler resolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Class clazz, String propertyName,
403
      Class javaType, String jdbcType) {
404
    return resolveTypeHandler(typeHandlerFactory, clazz, propertyName, javaType, jdbcType, false);
1✔
405
  }
406

407
  /**
408
   * Resolve type handler.
409
   *
410
   * @param typeHandlerFactory
411
   *          the type handler factory
412
   * @param clazz
413
   *          the clazz
414
   * @param propertyName
415
   *          the property name
416
   * @param javaType
417
   *          the java type
418
   * @param jdbcType
419
   *          the jdbc type
420
   * @param useSetterToResolve
421
   *          the use setter to resolve
422
   *
423
   * @return the type handler
424
   */
425
  TypeHandler resolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Class clazz, String propertyName,
426
      Class javaType, String jdbcType, boolean useSetterToResolve) {
427
    TypeHandler handler;
428
    if (clazz == null) {
1!
429
      // Unknown
430
      handler = typeHandlerFactory.getUnkownTypeHandler();
×
431
    } else if (DomTypeMarker.class.isAssignableFrom(clazz)) {
1✔
432
      // DOM
433
      handler = typeHandlerFactory.getTypeHandler(String.class, jdbcType);
1✔
434
    } else if (java.util.Map.class.isAssignableFrom(clazz)) {
1✔
435
      // Map
436
      if (javaType == null) {
1✔
437
        handler = typeHandlerFactory.getUnkownTypeHandler();
1✔
438
        // BUG 1012591 - typeHandlerFactory.getTypeHandler(java.lang.Object.class, jdbcType);
439
      } else {
440
        handler = typeHandlerFactory.getTypeHandler(javaType, jdbcType);
1✔
441
      }
442
    } else if (typeHandlerFactory.getTypeHandler(clazz, jdbcType) != null) {
1✔
443
      // Primitive
444
      handler = typeHandlerFactory.getTypeHandler(clazz, jdbcType);
1✔
445
    } else {
446
      // JavaBean
447
      if (javaType == null) {
1✔
448
        if (useSetterToResolve) {
1✔
449
          Class type = PROBE.getPropertyTypeForSetter(clazz, propertyName);
1✔
450
          handler = typeHandlerFactory.getTypeHandler(type, jdbcType);
1✔
451
        } else {
1✔
452
          Class type = PROBE.getPropertyTypeForGetter(clazz, propertyName);
1✔
453
          handler = typeHandlerFactory.getTypeHandler(type, jdbcType);
1✔
454
        }
1✔
455
      } else {
456
        handler = typeHandlerFactory.getTypeHandler(javaType, jdbcType);
1✔
457
      }
458
    }
459
    return handler;
1✔
460
  }
461

462
  /**
463
   * Register default type aliases.
464
   */
465
  private void registerDefaultTypeAliases() {
466
    // TRANSACTION ALIASES
467
    typeHandlerFactory.putTypeAlias("JDBC", JdbcTransactionConfig.class.getName());
1✔
468
    typeHandlerFactory.putTypeAlias("JTA", JtaTransactionConfig.class.getName());
1✔
469
    typeHandlerFactory.putTypeAlias("EXTERNAL", ExternalTransactionConfig.class.getName());
1✔
470

471
    // DATA SOURCE ALIASES
472
    typeHandlerFactory.putTypeAlias("SIMPLE", SimpleDataSourceFactory.class.getName());
1✔
473
    typeHandlerFactory.putTypeAlias("DBCP", DbcpDataSourceFactory.class.getName());
1✔
474
    typeHandlerFactory.putTypeAlias("JNDI", JndiDataSourceFactory.class.getName());
1✔
475

476
    // CACHE ALIASES
477
    typeHandlerFactory.putTypeAlias("FIFO", FifoCacheController.class.getName());
1✔
478
    typeHandlerFactory.putTypeAlias("LRU", LruCacheController.class.getName());
1✔
479
    typeHandlerFactory.putTypeAlias("MEMORY", MemoryCacheController.class.getName());
1✔
480

481
    // TYPE ALIASEs
482
    typeHandlerFactory.putTypeAlias("dom", DomTypeMarker.class.getName());
1✔
483
    typeHandlerFactory.putTypeAlias("domCollection", DomCollectionTypeMarker.class.getName());
1✔
484
    typeHandlerFactory.putTypeAlias("xml", XmlTypeMarker.class.getName());
1✔
485
    typeHandlerFactory.putTypeAlias("xmlCollection", XmlCollectionTypeMarker.class.getName());
1✔
486
  }
1✔
487

488
  /**
489
   * Wire up cache models.
490
   */
491
  private void wireUpCacheModels() {
492
    // Wire Up Cache Models
493
    Iterator cacheNames = client.getDelegate().getCacheModelNames();
1✔
494
    while (cacheNames.hasNext()) {
1✔
495
      String cacheName = (String) cacheNames.next();
1✔
496
      CacheModel cacheModel = client.getDelegate().getCacheModel(cacheName);
1✔
497
      Iterator statementNames = cacheModel.getFlushTriggerStatementNames();
1✔
498
      while (statementNames.hasNext()) {
1✔
499
        String statementName = (String) statementNames.next();
1✔
500
        MappedStatement statement = client.getDelegate().getMappedStatement(statementName);
1✔
501
        if (statement == null) {
1!
502
          throw new RuntimeException("Could not find statement named '" + statementName
×
503
              + "' for use as a flush trigger for the cache model named '" + cacheName + "'.");
504
        }
505
        statement.addExecuteListener(cacheModel);
1✔
506
      }
1✔
507
    }
1✔
508
  }
1✔
509

510
  /**
511
   * Bind result map discriminators.
512
   */
513
  private void bindResultMapDiscriminators() {
514
    // Bind discriminators
515
    Iterator names = delegate.getResultMapNames();
1✔
516
    while (names.hasNext()) {
1✔
517
      String name = (String) names.next();
1✔
518
      ResultMap rm = delegate.getResultMap(name);
1✔
519
      Discriminator disc = rm.getDiscriminator();
1✔
520
      if (disc != null) {
1✔
521
        disc.bindSubMaps();
1✔
522
      }
523
    }
1✔
524
  }
1✔
525

526
}
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