2

We have a MySQL table containing several boolean columns which specify the roles a user may have. Is it possible with slick's lifted embedding to write a type mapper which combines & transforms these multiple columns to one field in the case class User like shown below?

case class User(id: Option[Int], nickname: String, role: Seq[Role.Role])

object Users extends Table[(User)]("ask_user") {
      def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
      def nickname = column[String]("nickname")
      def is_editor = column[Boolean]("is_editor")
      def is_moderator = column[Boolean]("is_moderator")
      def is_administrator = column[Boolean]("is_administrator")
      def is_usermoderator = column[Boolean]("is_usermoderator")
      def is_usermoderator2 = column[Boolean]("is_usermoderator2")
      def is_partner = column[Boolean]("is_partner")
      def is_premium_partner = column[Boolean]("is_premium_partner")
      def is_corporate_paid = column[Boolean]("is_corporate_paid") 
    }

2 Answers 2

5

You can provide your own constructor and extractor functions for User objects to Slick using the <> function on the * projection. Something like this:

class User extends Table[User](...){
  ...
  def * = col1 ~ col2 ~ col3 ~ col4 <> (constructUser, extractUser)
}

def constructUser( col1: T1, col2: T2, col3: T3, col4: T4 )
  = User(col1, Roles(col2, col3, col4))
def extractUser( user: User ) = user match{
  case User(col1, Roles(col2, col3, col4)) =>  (col1, col2, col3, col4)
}

object Roles{
  def apply( col2: T2, col3: T3, col4: T4 ) : Set[Role]  = ...
  def unapply( roles: Set[Role] ) : Option[(T2, T3, T4)] = ...
}

Also see http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#mapped-tables

1
  • Thanks, that works nice and is easy. One minor change I had to do was to return an Option[(col1,..)] from extractUser
    – Chris W.
    Sep 12, 2013 at 13:54
-1

This is possible, you can use an Enumeration or roles:

object Role extends Enumeration with BitmaskedEnumeration {
  type Role = Value
  val Editor, Moderator, Administrator, Usermoderator, Usermoderator2,
      Partner, PremiumPartner, CorporatePaid = Value
}
import Role._

A field of type Role can then be used in your Slick Table "as is", like any of the native types.
Here is the code for BitmaskedEnumeration:

import slick.jdbc.GetResult
import slick.lifted.{BaseTypeMapper, MappedTypeMapper}

/** Source: https://github.com/nafg/slick-additions. */
trait Bitmasked {
  type Value
  def bitFor: Value => Int
  def forBit: Int => Value
  def values: Iterable[Value]
  def longToSet: Long => Set[Value] =
    bm => values.toSeq.filter(v => 0 != (bm & (1 << bitFor(v)))).toSet
  def setToLong: Set[Value] => Long =
    _.foldLeft(0L){ (bm, v) => bm + (1L << bitFor(v)) }

  implicit lazy val enumTypeMapper: BaseTypeMapper[Value] =
    MappedTypeMapper.base[Value, Int](bitFor, forBit)
  implicit lazy val enumSetTypeMapper: BaseTypeMapper[Set[Value]] =
    MappedTypeMapper.base[Set[Value], Long](setToLong, longToSet)

  implicit lazy val getResult: GetResult[Value] =
    GetResult(r => forBit(r.nextInt))
  implicit lazy val getSetResult: GetResult[Set[Value]] =
    GetResult(r => longToSet(r.nextLong))
}

/** Mix this class into a subclass of Enumeration to have it usable as a
  * column type in a Slick Table. */
trait BitmaskedEnumeration extends Bitmasked { this: Enumeration =>
  def bitFor = _.id
  def forBit = apply(_)
}
1
  • If I've understood your example correctly this would store the roles as a bitmask in one column. But we have an existing schema, where there are already defined columns (Booleans) for each Role, see my code above. So my goal is to unify the several columns in one Set[Role] field in the mapped object with going through intermediate mapping steps/objects
    – Chris W.
    Sep 2, 2013 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.