-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from eyalfa/enumTypeHandler__2.9.2
Enum type handler 2.9.2
- Loading branch information
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,3 +212,7 @@ | |
/project/nbproject | ||
/project/catalog.xml | ||
/project/.Build.scala.swp | ||
.settings | ||
.project | ||
.classpath | ||
|
44 changes: 44 additions & 0 deletions
44
mybatis-scala-core/src/main/scala/org/mybatis/scala/mapping/EnumTypeHandlerBase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
94 changes: 94 additions & 0 deletions
94
mybatis-scala-samples/src/main/scala/org/mybatis/scala/samples/select/SelectEnumSample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} | ||
} | ||
} |