Reputation: 40
I want to retrieve data of a column with type DateTime and I am using jodatime for it. Even though I have a custom TypeMapper, I am getting type mismatch error.
[error] C:\sample\modules\test\com\samp\user.scala:55: type mismatch;
[error] found : java.sql.Timestamp
[error] required: org.joda.time.DateTime
[error] result.nextTimestamp,
[error] ^
[error] one error found
Here is the code
import org.specs2.mutable.Specification
import scala.slick.session.Database
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
import Q.interpolation
import org.joda.time.DateTime
import scala.slick.lifted.{MappedTypeMapper,TypeMapper}
import java.sql.Timestamp
class UserSpec
extends Specification {
"User tests " should {
"get all the user data in db" in
new WithServer() {
val db = Database.forURL(url = "jdbc:h2:/path/to/db-file", driver = "com.mysql.jdbc.Driver")
implicit def dateTimeTypeMapper = MappedTypeMapper.base[DateTime, Timestamp] (
dateTime => new Timestamp(dateTime.getMillis()),
timeStamp => new DateTime(timeStamp)
)
case class user(
id: String,
name: String,
joinedAt: DateTime,
description: Option[String]
)
implicit val getUserResult: GetResult[user] =
GetResult( result =>
user(
result.nextString,
result.nextString,
result.nextTimestamp,
result.nextStringOption)
)
db withSession {
val usr = Q.queryNA[user]("SELECT * FROM user").list()
usr foreach println
}
}
}
}
I am not sure why this is not working. Any help or pointers is greatly appreciated.
Upvotes: 1
Views: 2416
Reputation: 3140
The problem here is that result.nextTimestamp
returns a java.sql.Timestamp
(result
is the JDBC ResultSet
). If you want to turn it into a DateTime
, I think that there's a constructor for it. So you'd write:
implicit val getUserResult: GetResult[user] =
GetResult( result =>
user(
result.nextString,
result.nextString,
new DateTime(result.nextTimestamp),
result.nextStringOption)
)
Upvotes: 2