8

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 column INT(3) with Enum containing 3 values: lets name them a,b,c here.

  • related to http://stackoverflow.com/questions/19273805/how-to-persist-enum-value-in-slick – cvogt Oct 12 '13 at 23:45

1 Answers1

9

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 */ 

}

If works for me in both Play 2.1 and Play 2.2, Slick 1.0.0 and MariaDB 5.5 (same as MySQL)

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • 1
    BTW MappedTypeMapper does not exists. It's renamed to MappedColumnType. Here's a discussion: https://groups.google.com/forum/#!msg/scalaquery/4Ns_J_8wbqQ/0SGiJL4O8A8J – Sayat Satybald Aug 01 '14 at 11:02
  • Yes, it's now renamed. This answer was made when Slick 1 was still the current version :) – Ashalynd Aug 01 '14 at 11:11