Skip to content

Commit

Permalink
Merge pull request #3 from eyalfa/enumTypeHandler__2.9.2
Browse files Browse the repository at this point in the history
Enum type handler  2.9.2
  • Loading branch information
mnesarco committed Jul 15, 2013
2 parents 06c76a3 + 32cb9f1 commit c268490
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,7 @@
/project/nbproject
/project/catalog.xml
/project/.Build.scala.swp
.settings
.project
.classpath

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.mybatis.scala.mapping

import org.apache.ibatis.`type`.BaseTypeHandler
import java.sql.ResultSet
import java.sql.PreparedStatement
import java.sql.CallableStatement
import org.apache.ibatis.`type`.StringTypeHandler
import org.apache.ibatis.`type`.IntegerTypeHandler

trait EnumTypeHandlerBase[I] extends BaseTypeHandler[Enumeration#Value] {

final def setNonNullParameter(ps: PreparedStatement, i: Int, parameter: Enumeration#Value, jdbcType: org.apache.ibatis.`type`.JdbcType) = {
val x = toIntermediateRepr(parameter)
intermediateTypeHandler.setParameter(ps, i, x, jdbcType)
}

final def getNullableResult(rs: ResultSet, columnName: String) = {
Option(intermediateTypeHandler.getResult(rs, columnName)).map(fromIntermediateRepr).orNull
}

final def getNullableResult(rs: ResultSet, columnIndex: Int) = {
Option(intermediateTypeHandler.getResult(rs, columnIndex)).map(fromIntermediateRepr).orNull
}

final def getNullableResult(cs: CallableStatement, columnIndex: Int) = {
Option(intermediateTypeHandler.getResult(cs, columnIndex)).map(fromIntermediateRepr).orNull
}
protected def enumObj: Enumeration
protected def intermediateTypeHandler: TypeHandler[I]
protected def toIntermediateRepr(notNull: Enumeration#Value): I
protected def fromIntermediateRepr(notNull: I): Enumeration#Value
}

trait EnumStrTypeHandler extends EnumTypeHandlerBase[String] {
final protected val intermediateTypeHandler = new StringTypeHandler
final protected def toIntermediateRepr(notNull: Enumeration#Value) = notNull.toString
final protected def fromIntermediateRepr(notNull: String) = enumObj.withName(notNull)
}

trait EnumIntTypeHandler extends EnumTypeHandlerBase[Integer] {
final protected val intermediateTypeHandler = new IntegerTypeHandler
final protected def toIntermediateRepr(notNull: Enumeration#Value) = notNull.id
final protected def fromIntermediateRepr(notNull: Integer) = enumObj(notNull.intValue)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.mybatis.scala.samples.select

import org.mybatis.scala.mapping.EnumStrTypeHandler
import org.mybatis.scala.mapping.EnumIntTypeHandler
import org.mybatis.scala.mapping.SelectList
import org.mybatis.scala.mapping.ResultMap
import org.mybatis.scala.mapping._
import org.mybatis.scala.samples.util.DBSchema
import org.mybatis.scala.samples.util.DBSampleData

/*
* sample enum
*/
object GroupEnum extends Enumeration {
val Customers = Value(1, "Customers")
val Suppliers = Value(2, "Suppliers")
val Employees = Value(3, "Employees")
}

/*
* sample case class to represent the selected records
*/
case class Group(val id: Int, val name: String, val group: GroupEnum.Value)

/*
* string based handler for GroupEnum
*/
class GroupEnumStrTypeHandler extends EnumStrTypeHandler {
def enumObj = GroupEnum
}

/*
* int based handler for GroupEnum
*/
class GroupEnumIntTypeHandler extends EnumIntTypeHandler {
def enumObj = GroupEnum
}

/*
* the queries
*/
object GroupQueries {
class Query(colName: String, typeHandler: T[_ <: TypeHandler[Enumeration#Value]]) extends SelectList[Group] {
resultMap = new ResultMap[Group] {
arg("id_", javaType = T[Int])
arg("name_", javaType = T[String])
/*
* an enum field, javaType is mandatory in this case since the typeHandler val is typed erased (or something...)
*/
arg(colName, typeHandler = typeHandler, javaType = T[Enumeration#Value])
}
def xsql = """
SELECT
id_, name_
FROM
people_group
"""
}
/*
* the actual queries
*/
val selectEnumViaString = new Query("name_", T[GroupEnumStrTypeHandler])
val selectEnumViaInteger = new Query("id_", T[GroupEnumIntTypeHandler])

}

object SelectEnumSample extends App {
// Do the Magic ...
override def main(args: Array[String]): Unit = {
/*
* this is a bit tricky, getting to register the statements before actually creating the Configuration object
*/
CDB.ConfigurationSpec.statements(GroupQueries.selectEnumViaString, GroupQueries.selectEnumViaInteger)
CDB.context.transaction { implicit s =>

// Create database and populate it with sample data
DBSchema.create
DBSampleData.populate

for (q <- Seq(GroupQueries.selectEnumViaString, GroupQueries.selectEnumViaInteger)) {
// Query
GroupQueries.selectEnumViaString().foreach(x => {
println(x)
//some basic tests
assert(x.group != null)
assert(x.group.id == x.id)
assert(x.group.toString == x.name)
assert(GroupEnum(x.id) == x.group)
assert(GroupEnum.withName(x.name) == x.group)
})
}
}
}
}

0 comments on commit c268490

Please sign in to comment.