Tuesday, 15 March 2011

mysql - Joda-Date Mapper for Slick - MappedColumnTyoe -



mysql - Joda-Date Mapper for Slick - MappedColumnTyoe -

my problem close question, error different.

customer type mapper slick sql

this utility class jdate , mapper defined

package org.mydomain.utils import slick.driver.mysqldriver.simple._ import org.joda.time.datetime import java.sql.date import org.joda.time.datetime import java.sql.timestamp sealed trait jdate object dateutils { implicit def jdatecolumntype = mappedcolumntype.base[datetime, timestamp]( dt => new timestamp(dt.getmillis), ts => new datetime(ts.gettime) ) }

the domain object user.scala follows

case class userrow(id: long, birthday: jdate) class user(tag: tag) extends table[userrow](tag, "user") { def id = column[long]("id", o.primarykey, o.autoinc) def birthday = column[jdate]("birthday") def * = (id, birthday) <> (userrow.tupled, userrow.unapply) }

error: not plenty arguments method column: (implicit tm: scala.slick.ast.typedtype[org.mydomain.utils.jdate])scala.slick.lifted.column[org.mydomain.utils.jdate]. unspecified value parameter tm.

how pass tm here ? apologies noob question. thanks

there 2 problems here:

you don't need jdate - don't want utilize anywhere, want able translate between datetime , timestamp instances. utilize datetime column type instead (e. g. column[datetime]("birthday") rather column[jdate]("birthday"))

you never import implicit datetime <> timestamp converter, never picked slick's column. either:

// import implicit in constructor class user(tag: tag) extends table[userrow](tag, "user") { import org.mydomain.utils.dateutils.jdatecolumntype // code here }

or

// explicitly pass implicit converter in class user(tag: tag) extends table[userrow](tag, "user") { // ... snip ... def birthday = column[datetime]("birthday")(dateutils.jdatecolumntype) // ... snip ... }

or (best option), create own driver extending mysql driver custom implicit in trait:

package org.mydomain.db import scala.slick.driver.{mysqldriver, jdbcdriver} trait dateutils { implicit def jdatecolumntype = mappedcolumntype.base[datetime, timestamp]( dt => new timestamp(dt.getmillis), ts => new datetime(ts.gettime) ) } trait custommysqldriver extends jdbcdriver mysqldriver dateutils object custommysqldriver extends custommysqldriver

and can utilize this:

import org.mydomain.db.custommysqldriver.simple._ class user(tag: tag) extends table[userrow](tag, "user") { // ... snip ... def birthday = column[datetime]("birthday") // ... snip ... }

mysql scala slick

No comments:

Post a Comment