enum value slick table

427 views
Skip to first unread message

Diogo Longo

unread,
Oct 9, 2013, 9:40:26 AM10/9/13
to scala...@googlegroups.com
Hi,

I have the follow enum:

object LoginStatus extends Enumeration() with BitmaskedEnumeration {
  type LoginStatus = Value
  val Active = Value("A")
  val Inactive = Value("I")
}

I need to persist the value of the enum "A", but when the sql is generated the result is 0.
this is the table mapping:

object LoginTable extends Table[Login]("login") {
  def idLogin = column[Int]("idlogin", O.PrimaryKey, O.AutoInc)
  def cdLogin = column[String]("cdlogin", O.NotNull)
  def cdPass = column[String]("cdPass", O.NotNull)
  def stLogin = column[LoginStatus]("stlogin", O.NotNull, O.DBType("character(1)"))
}

how to persiste the enum value?

Ricardo Franco

unread,
Oct 9, 2013, 10:41:19 AM10/9/13
to scala...@googlegroups.com
I use this.

def enum2StringMapper(enum: Enumeration) = MappedTypeMapper.base[enum.Value, String](
    b => b.toString,
    i => enum.withName(i)
  )

def enum2IntTypeMapper(enum: Enumeration) = MappedTypeMapper.base[enum.Value, Int](
    b => b.id,
    i => enum.apply(i)
  )

def enum2BitmaskedMapper(enum:Enumeration) = MappedTypeMapper.base[Set[enum.Value], Long](
      b => b.foldLeft(0L)((bm, v) => bm + (1L << v.id)),
      i => StatusType.values.filter(v => 0 != (i & (1 << v.id)))
  )

How to use:
implicit val LoginStatusMapper = enum2StringMapper(LoginStatus)


--
 
---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Diogo Longo

unread,
Oct 9, 2013, 1:01:50 PM10/9/13
to scala...@googlegroups.com
Hi Ricardo,

I didn't found the import for type StatusType.

I have to change something in the code?

Ricardo Franco

unread,
Oct 9, 2013, 3:33:42 PM10/9/13
to scala...@googlegroups.com
Oh Sorry i copy from my code

This is generic way.

def enum2BitmaskedMapper(enum:Enumeration) = MappedTypeMapper.base[Set[enum.Value], Long](
      b => b.foldLeft(0L)((bm, v) => bm + (1L << v.id)),
      i => enum.values.filter(v => 0 != (i & (1 << v.id)))
  )

Diogo Longo

unread,
Oct 10, 2013, 9:16:23 AM10/10/13
to scala...@googlegroups.com
Tks Ricardo,

I changed the code, but slick still complain about the invalid value:

select x2."idlogin", x2."cdlogin", x2."cdpass", x2."stlogin" from "login" x2 where (x2."cdlogin" = ?) and (x2."stlogin" = ?)
ERROR: operator does not exist: character = integer

Diogo Longo

unread,
Oct 10, 2013, 9:34:26 AM10/10/13
to scala...@googlegroups.com
I implemented
implicit val charMapper = MappedTypeMapper.base[Char, String](
    b => b.toString(),
    i => i.charAt(0))

  implicit def enum2StringMapper(enum: Enumeration) = MappedTypeMapper.base[enum.Value, Char](
    b => b.toString.charAt(0),
    i => enum.withName(i.toString))


  implicit val LoginStatusMapper = enum2StringMapper(LoginStatus)
but result in:
[error] c.Login - Invalid value for type int : A

Ricardo Franco

unread,
Oct 10, 2013, 12:01:49 PM10/10/13
to scala...@googlegroups.com
It should work for you.

object LoginStatus extends Enumeration {
      type LoginStatus = Value
      val Active = Value("A")
      val Inactive = Value("I")
    }

    def enum2String(enum: Enumeration) = MappedTypeMapper.base[enum.Value, String](
      b => b.toString,
      i => enum.withName(i)
    )

    implicit val LoginStatusMapper = enum2String(LoginStatus)

Diogo Longo

unread,
Oct 10, 2013, 2:32:11 PM10/10/13
to scala...@googlegroups.com
Tks Ricardo, its works!

I have to move the code from the dao to enum class:

object LoginStatus extends Enumeration {


  def enum2StringMapper(enum: Enumeration) = MappedTypeMapper.base[enum.Value, String](
    b => b.toString,
    i => enum.withName(i))
implicit val LoginStatusMapper = enum2StringMapper(LoginStatus)

Christopher Vogt

unread,
Oct 12, 2013, 7:43:12 PM10/12/13
to scala...@googlegroups.com
PLEASE cross-link between Stackoverflow and Mailing List posts if you
post on both platforms.

Here is the link
http://stackoverflow.com/questions/19273805/how-to-persist-enum-value-in-slick
Reply all
Reply to author
Forward
0 new messages