如何在 Scala Slick 中使用枚举?
想要将 MySQL INT 位掩码映射到 Slick.
Want to map MySQL INT bitmask to Slick.
我找到了这个,但是如何使用它没有什么问题
I've found this but have little problem how to use it
https://github.com/nafg/slick-additions/blob/master/src/main/scala/scala/slick/additions/Enum.scala
任何帮助我应该如何定义对象,即
Any help how should I define object for i.e.
mysql 列 INT(3) 带有包含 3 个值的 Enum:让我们在此处将它们命名为 a、b、c.
mysql column INT(3) with Enum containing 3 values: lets name them a,b,c here.
推荐答案
我通过以下方式解决了 Enums 的问题(以您的值为例):
I solved the problem with Enums in the following way (taking your values for an example):
import play.api.db.slick.DB
import play.api.db.slick.Config.driver.simple._
sealed trait MyEnum
case object MyEnumA extends MyEnum
case object MyEnumB extends MyEnum
case object MyEnumC extends MyEnum
object MyEnumMapper {
val string_enum_mapping:Map[String,MyEnum] = Map(
"a" -> MyEnumA,
"b" -> MyEnumB,
"c" -> MyEnumC
)
val enum_string_mapping:Map[MyEnum,String] = string_enum_mapping.map(_.swap)
implicit val myEnumStringMapper = MappedTypeMapper.base[MyEnum,String](
e => enum_string_mapping(e),
s => string_enum_mapping(s)
)
}
import MyEnumMapper._
case class MyData(
......
enumValue: MyEnum,
.....
)
................
object MyDataTable extends Table[MyData]("<table_name>") {
......
def enumValue = column[MyEnum]("<field_name>")
.....
.... /* whatever lifted or direct slick calls you want */
}
如果在 Play 2.1 和 Play 2.2、Slick 1.0.0 和 MariaDB 5.5(与 MySQL 相同)中都对我有用
If works for me in both Play 2.1 and Play 2.2, Slick 1.0.0 and MariaDB 5.5 (same as MySQL)
相关文章