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

mybatis / scala / 444

29 Mar 2025 10:33PM CUT coverage: 0.0%. Remained the same
444

push

github

web-flow
Update ci.yaml

drop jdk 22 and 23-ea, add jdk 24

0 of 476 branches covered (0.0%)

0 of 996 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/mybatis-scala-core/src/main/scala/org/mybatis/scala/config/Configuration.scala
1
/*
2
 *    Copyright 2011-2015 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.mybatis.scala.config
17

18
import org.apache.ibatis.session.{ Configuration => MBConfig, SqlSessionFactoryBuilder }
19
import org.apache.ibatis.builder.xml.XMLConfigBuilder
20
import java.io.Reader
21
import org.apache.ibatis.io.Resources
22
import org.mybatis.scala.session.SessionManager
23
import java.util.Properties
24
import org.mybatis.scala.mapping.Statement
25
import org.mybatis.scala.mapping.T
26
import org.mybatis.scala.cache._
27
import org.apache.ibatis.`type`.TypeHandler
28

29
/**
30
 * Mybatis Configuration
31
 * @constructor Creates a new Configuration with a wrapped myBatis Configuration.
32
 * @param configuration A myBatis Configuration instance.
33
 */
34
sealed class Configuration(private val configuration: MBConfig) {
×
35

36
  if (configuration.getObjectFactory().getClass == classOf[org.apache.ibatis.reflection.factory.DefaultObjectFactory]) {
×
37
    configuration.setObjectFactory(new DefaultObjectFactory())
×
38
  }
39

40
  if (configuration.getObjectWrapperFactory.getClass == classOf[org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory]) {
×
41
    configuration.setObjectWrapperFactory(new DefaultObjectWrapperFactory())
×
42
  }
43

44
  registerCommonOptionTypeHandlers
×
45

46
  lazy val defaultSpace = new ConfigurationSpace(configuration, "_DEFAULT_")
×
47

48
  /**
49
   * Creates a new space of mapped statements.
50
   * @param name A Speca name (a.k.a. namespace)
51
   * @param f A configuration block.
52
   */
53
  def addSpace(name: String)(f: (ConfigurationSpace => Unit)): this.type = {
54
    val space = new ConfigurationSpace(configuration, name)
×
55
    f(space)
×
56
    this
×
57
  }
58

59
  /** Adds a statement to the default space */
60
  def +=(s: Statement) = defaultSpace += s
×
61

62
  /** Adds a sequence of statements to the default space */
63
  def ++=(ss: Seq[Statement]) = defaultSpace ++= ss
×
64

65
  /** Adds a mapper to the space */
66
  def ++=(mapper: { def bind: Seq[Statement] }) = defaultSpace ++= mapper
×
67

68
  /**
69
   * Adds cache support to this space.
70
   * @param impl Cache implementation class
71
   * @param eviction cache eviction policy (LRU,FIFO,WEAK,SOFT)
72
   * @param flushInterval any positive integer in milliseconds.
73
   *        The default is not set, thus no flush interval is used and the cache is only flushed by calls to statements.
74
   * @param size max number of objects that can live in the cache. Default is 1024
75
   * @param readWrite A read-only cache will return the same instance of the cached object to all callers.
76
   *        Thus such objects should not be modified.  This offers a significant performance advantage though.
77
   *        A read-write cache will return a copy (via serialization) of the cached object,
78
   *        this is slower, but safer, and thus the default is true.
79
   * @param props implementation specific properties.
80
   */
81
  def cache(
82
    impl: T[_ <: Cache] = DefaultCache,
×
83
    eviction: T[_ <: Cache] = Eviction.LRU,
×
84
    flushInterval: Long = -1,
×
85
    size: Int = -1,
×
86
    readWrite: Boolean = true,
×
87
    blocking : Boolean = false,
×
88
    props: Properties = null) =
×
89
    defaultSpace.cache(impl, eviction, flushInterval, size, readWrite, blocking, props)
×
90

91
  /** Reference to an external Cache */
92
  def cacheRef(that: ConfigurationSpace) = defaultSpace.cacheRef(that)
×
93

94
  /** Builds a Session Manager */
95
  def createPersistenceContext = {
96
    val builder = new SqlSessionFactoryBuilder
×
97
    new SessionManager(builder.build(configuration))
×
98
  }
99

100
  private def registerOptionTypeHandler[T <: Option[_]](h: TypeHandler[T], jdbcTypes: Seq[org.apache.ibatis.`type`.JdbcType]) = {
101
    import org.mybatis.scala.mapping.OptionTypeHandler
102
    val registry = configuration.getTypeHandlerRegistry
×
103
    val cls = classOf[Option[_]]
×
104
    for (jdbcType <- jdbcTypes) {
×
105
      registry.register(cls, jdbcType, h)
×
106
    }
107
  }
108

109
  private def registerCommonOptionTypeHandlers = {
110
    import org.mybatis.scala.mapping.OptionTypeHandler
111
    import org.mybatis.scala.mapping.TypeHandlers._
112
    import org.apache.ibatis.`type`._
113
    import org.apache.ibatis.`type`.JdbcType._
114
    registerOptionTypeHandler(new OptBooleanTypeHandler(), Seq(BOOLEAN, BIT))
×
115
    registerOptionTypeHandler(new OptByteTypeHandler(), Seq(TINYINT))
×
116
    registerOptionTypeHandler(new OptShortTypeHandler(), Seq(SMALLINT))
×
117
    registerOptionTypeHandler(new OptIntegerTypeHandler(), Seq(INTEGER))
×
118
    registerOptionTypeHandler(new OptFloatTypeHandler(), Seq(FLOAT))
×
119
    registerOptionTypeHandler(new OptDoubleTypeHandler(), Seq(DOUBLE))
×
120
    registerOptionTypeHandler(new OptLongTypeHandler(), Seq(BIGINT))
×
121
    registerOptionTypeHandler(new OptStringTypeHandler(), Seq(VARCHAR, CHAR))
×
122
    registerOptionTypeHandler(new OptClobTypeHandler(), Seq(CLOB, LONGVARCHAR))
×
123
    registerOptionTypeHandler(new OptNStringTypeHandler(), Seq(NVARCHAR, NCHAR))
×
124
    registerOptionTypeHandler(new OptNClobTypeHandler(), Seq(NCLOB))
×
125
    registerOptionTypeHandler(new OptBigDecimalTypeHandler(), Seq(REAL, DECIMAL, NUMERIC))
×
126
    registerOptionTypeHandler(new OptBlobTypeHandler(), Seq(BLOB, LONGVARBINARY))
×
127
    registerOptionTypeHandler(new OptDateTypeHandler(), Seq(DATE))
×
128
    registerOptionTypeHandler(new OptTimeTypeHandler(), Seq(TIME))
×
129
    registerOptionTypeHandler(new OptTimestampTypeHandler(), Seq(TIMESTAMP))
×
130
    registerOptionTypeHandler(new OptionTypeHandler(new UnknownTypeHandler(configuration.getTypeHandlerRegistry)), Seq(OTHER))
×
131
  }
132

133
}
134

135
/** A factory of [[org.mybatis.scala.config.Configuration]] instances. */
136
object Configuration {
×
137

138
  /**
139
   * Creates a Configuration built from a reader.
140
   * @param reader Reader over a myBatis main configuration XML
141
   */
142
  def apply(reader: Reader): Configuration = {
143
    val builder = new XMLConfigBuilder(reader)
×
144
    new Configuration(builder.parse)
×
145
  }
146

147
  /**
148
   * Creates a Configuration built from a reader.
149
   * @param reader Reader over a myBatis main configuration XML
150
   * @param env Environment name
151
   */
152
  def apply(reader: Reader, env: String): Configuration = {
153
    val builder = new XMLConfigBuilder(reader, env)
×
154
    new Configuration(builder.parse)
×
155
  }
156

157
  /**
158
   * Creates a Configuration built from a reader.
159
   * @param reader Reader over a myBatis main configuration XML
160
   * @param env Environment name
161
   * @param properties Properties
162
   */
163
  def apply(reader: Reader, env: String, properties: Properties): Configuration = {
164
    val builder = new XMLConfigBuilder(reader, env, properties)
×
165
    new Configuration(builder.parse)
×
166
  }
167

168
  /**
169
   * Creates a Configuration built from a classpath resource.
170
   * @param path Classpath resource with main configuration XML
171
   */
172
  def apply(path: String): Configuration = {
173
    apply(Resources.getResourceAsReader(path))
×
174
  }
175

176
  /**
177
   * Creates a Configuration built from a classpath resource.
178
   * @param path Classpath resource with main configuration XML
179
   * @param env Environment name
180
   */
181
  def apply(path: String, env: String): Configuration = {
182
    apply(Resources.getResourceAsReader(path), env)
×
183
  }
184

185
  /**
186
   * Creates a Configuration built from a classpath resource.
187
   * @param path Classpath resource with main configuration XML
188
   * @param env Environment name
189
   * @param properties Properties
190
   */
191
  def apply(path: String, env: String, properties: Properties): Configuration = {
192
    apply(Resources.getResourceAsReader(path), env, properties)
×
193
  }
194

195
  /**
196
   * Creates a Configuration built from an environment
197
   * @param env Environment
198
   */
199
  def apply(env: Environment): Configuration = {
200
    new Configuration(new MBConfig(env.unwrap))
×
201
  }
202

203
  /**
204
   * Creates a Configuration built from a custom builder
205
   * @param builder Builder => Unit
206
   */
207
  def apply(builder: Builder): Configuration = builder.build()
×
208

209
  /** Configuration builder */
210
  class Builder {
×
211

212
    import org.mybatis.scala.mapping.JdbcType
213
    import org.apache.ibatis.plugin.Interceptor
214
    import scala.collection.mutable.ArrayBuffer
215
    import org.mybatis.scala.session.ExecutorType
216
    import scala.jdk.CollectionConverters._
217
    import org.apache.ibatis.mapping.Environment
218

219
    /** Reference to self. Support for operational notation. */
220
    protected val >> = this
×
221

222
    /** Mutable hidden state, discarded after construction */
223
    private val pre = new ArrayBuffer[ConfigElem[MBConfig]]()
×
224

225
    /** Mutable hidden state, discarded after construction */
226
    private val pos = new ArrayBuffer[ConfigElem[Configuration]]()
×
227

228
    /** Configuration Element */
229
    private abstract class ConfigElem[T] {
×
230
      val index: Int
231
      def set(config: T): Unit
232
    }
233

234
    /** Ordered deferred setter */
235
    private def set[A](i: Int, e: ArrayBuffer[ConfigElem[A]])(f: A => Unit): Unit = {
×
236
      e += new ConfigElem[A] {
×
237
        val index = i
×
238
        def set(c: A) = f(c)
×
239
      }
240
    }
241

242
    /** Builds the configuration object */
243
    private[Configuration] def build(): Configuration = {
244
      val preConfig = new MBConfig()
×
245
      pre.sortWith((a, b) => a.index < b.index).foreach(_.set(preConfig))
×
246
      val config = new Configuration(preConfig)
×
247
      pos.sortWith((a, b) => a.index < b.index).foreach(_.set(config))
×
248
      config
×
249
    }
250

251
    // Pre ====================================================================
252

253
    def properties(props: (String, String)*) =
254
      set(0, pre) { _.getVariables.asScala ++= Map(props: _*) }
×
255

256
    def properties(props: Properties) =
257
      set(1, pre) { _.getVariables.asScala ++= props.asScala }
×
258

259
    def properties(resource: String) =
260
      set(2, pre) { _.getVariables.asScala ++= Resources.getResourceAsProperties(resource).asScala }
×
261

262
    def propertiesFromUrl(url: String) =
263
      set(3, pre) { _.getVariables.asScala ++= Resources.getUrlAsProperties(url).asScala }
×
264

265
    def plugin(plugin: Interceptor) =
266
      set(4, pre) { _.addInterceptor(plugin) }
×
267

268
    def objectFactory(factory: ObjectFactory) =
269
      set(5, pre) { _.setObjectFactory(factory) }
×
270

271
    def objectWrapperFactory(factory: ObjectWrapperFactory) =
272
      set(6, pre) { _.setObjectWrapperFactory(factory) }
×
273

274
    // Settings ===============
275

276
    def autoMappingBehavior(behavior: AutoMappingBehavior) =
277
      set(8, pre) { _.setAutoMappingBehavior(behavior.unwrap) }
×
278

279
    def cacheSupport(enabled: Boolean) =
280
      set(9, pre) { _.setCacheEnabled(enabled) }
×
281

282
    def lazyLoadingSupport(enabled: Boolean) =
283
      set(10, pre) { _.setLazyLoadingEnabled(enabled) }
×
284

285
    def aggressiveLazyLoading(enabled: Boolean) =
286
      set(11, pre) { _.setAggressiveLazyLoading(enabled) }
×
287

288
    def multipleResultSetsSupport(enabled: Boolean) =
289
      set(12, pre) { _.setMultipleResultSetsEnabled(enabled) }
×
290

291
    def useColumnLabel(enabled: Boolean) =
292
      set(13, pre) { _.setUseColumnLabel(enabled) }
×
293

294
    def useGeneratedKeys(enabled: Boolean) =
295
      set(14, pre) { _.setUseGeneratedKeys(enabled) }
×
296

297
    def defaultExecutorType(executorType: ExecutorType) =
298
      set(15, pre) { _.setDefaultExecutorType(executorType.unwrap) }
×
299

300
    def defaultStatementTimeout(timeout: Int) =
301
      set(16, pre) { _.setDefaultStatementTimeout(timeout) }
×
302

303
    def mapUnderscoreToCamelCase(enabled: Boolean) =
304
      set(17, pre) { _.setMapUnderscoreToCamelCase(enabled) }
×
305

306
    def safeRowBoundsSupport(enabled: Boolean) =
307
      set(18, pre) { _.setSafeRowBoundsEnabled(enabled) }
×
308

309
    def localCacheScope(localCacheScope: LocalCacheScope) =
310
      set(19, pre) { _.setLocalCacheScope(localCacheScope.unwrap) }
×
311

312
    def jdbcTypeForNull(jdbcType: JdbcType) =
313
      set(20, pre) { _.setJdbcTypeForNull(jdbcType.unwrap) }
×
314

315
    def lazyLoadTriggerMethods(names: Set[String]) =
316
      set(21, pre) { _.setLazyLoadTriggerMethods(names.asJava) }
×
317

318
    def environment(id: String, transactionFactory: TransactionFactory, dataSource: javax.sql.DataSource) =
319
      set(24, pre) { _.setEnvironment(new Environment(id, transactionFactory, dataSource)) }
×
320

321
    def databaseIdProvider(provider: DatabaseIdProvider) =
322
      set(25, pre) { c => c.setDatabaseId(provider.getDatabaseId(c.getEnvironment.getDataSource)) }
×
323

324
    def typeHandler(jdbcType: JdbcType, handler: (T[_], TypeHandler[_])) =
325
      set(26, pre) { _.getTypeHandlerRegistry.register(handler._1.raw, jdbcType.unwrap, handler._2) }
×
326

327
    def typeHandler(handler: (T[_], TypeHandler[_])) =
328
      set(26, pre) { _.getTypeHandlerRegistry.register(handler._1.raw, handler._2) }
×
329

330
    def typeHandler(handler: TypeHandler[_]) =
331
      set(26, pre) { _.getTypeHandlerRegistry.register(handler) }
×
332

333
    // Pos ===========================================================
334

335
    def namespace(name: String)(f: (ConfigurationSpace => Unit)) =
336
      set(0, pos) { c => f(new ConfigurationSpace(c.configuration, name)) }
×
337

338
    def statements(s: Statement*) =
339
      set(1, pos) { _ ++= s }
×
340

341
    def mappers(mappers: { def bind: Seq[Statement] }*) =
342
      set(1, pos) { c => mappers.foreach(c ++= _) }
×
343

344
    def cacheRef(that: ConfigurationSpace) =
345
      set(2, pos) { _.cacheRef(that) }
×
346

347
    def cache(
348
      impl: T[_ <: Cache] = DefaultCache,
×
349
      eviction: T[_ <: Cache] = Eviction.LRU,
×
350
      flushInterval: Long = -1,
×
351
      size: Int = -1,
×
352
      readWrite: Boolean = true,
×
353
      blocking : Boolean = false,
×
354
      props: Properties = null) =
×
355
        set(2, pos) { _.cache(impl, eviction, flushInterval, size, readWrite, blocking, props) }
×
356

357
    // PENDING FOR mybatis 3.1.1+ ==================================================
358

359
    // TODO (3.1.1) def proxyFactory(factory: ProxyFactory) = set( 7, pre) { _.setProxyFactory(factory) }
360
    // TODO (3.1.1) def safeResultHandlerSupport(enabled : Boolean) = set(22, pre) { _.setSafeResultHandlerEnabled(enabled) }
361
    // TODO (3.1.1) def defaultScriptingLanguage(driver : T[_]) = set(23, pre) { _.setDefaultScriptingLanguage(driver) }
362

363
  }
364

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