Skip to content

Instantly share code, notes, and snippets.

@lJoublanc
Created January 11, 2017 08:54
Show Gist options
  • Save lJoublanc/d479a904386cf50ffa3c531ace23fd5a to your computer and use it in GitHub Desktop.
Save lJoublanc/d479a904386cf50ffa3c531ace23fd5a to your computer and use it in GitHub Desktop.
Example of how to use Slick's MappedJdbcType in practice
// This does the conversion from enum to char and vice-versa.
abstract class JDBCCharEnumeration extends Enumeration {
import slick.jdbc.HsqldbProfile.api._ //from what I recall changing this to JDBCProfile does *not* work. Must use specific driver.
import slick.jdbc.HsqldbProfile.MappedJdbcType
implicit def enumMapper = MappedJdbcType.base[Value,Char](_.id.toChar, this(_))
}
// The enumeration
object OrderExpiry extends JDBCCharEnumeration {
val Day = Value('0', "Good for Day")
val Open = Value('2', "At the Opening")
val IoC = Value('3', "Immediate or Cancel")
val FoK = Value('4', "Fill or Kill")
val Close = Value('7', "At the Close")
}
// The case class
case class Order(expiryType: OrderExpiry)
// The TableQuery definition
class OrderRow(_tableTag: Tag) extends profile.api.Table[Order](_tableTag, Some("REXDB"), "BUS_ORDER") {
/** Database column EXPIRY_TYPE SqlType(REXDB.ORDER_EXPIRY) which is a user-defined-type as CHAR in sql*/
val expiryType: Rep[OrderExpiry] = column[OrderExpiry]("EXPIRY_TYPE")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment