如何将日期对象写入隐式转换器?

时间:2016-07-23 09:16:24

标签: postgresql date slick implicit-conversion slick-3.0

我有一个名为getUser的函数的scala类,它带有year& month。我将使用year&获取用户列表month基于birthday列,定义为Instant

我正在使用postgresql& slick。我写了一个sql,如: -

sql"SELECT * FROM user_info WHERE birthday::text LIKE '1990-07%'".as[Seq[User]]

对于上面的查询,我还声明了一个隐式转换器,如下所示: -

implicit val getUserResult = GetResult[User](r => User(r.<<, r.<<, r.<<, r.<<, r.<<))

我已经为上述查询编写了相应的代码

case class User(id: Option[Int],
                name: String,
                birthday: Instant,
                updated: Option[Instant]) extends GenericEntity {
       def this(name: String,
                birthday: Instant) = this(None, name, birthday, None, None)
}

class UserTable(tag: Tag) extends GenericTable[User](tag, "user_info") {
      override def id = column[Option[Int]]("id", O.PrimaryKey, O.AutoInc)
      def name = column[String]("name")
      def birthday = column[Instant]("birthday")

  def * = (id, name, birthday, created, updated) <> ((User.apply _).tupled, User.unapply)
}

object Users extends GenericService[User, UserTable] {
      override val table = TableQuery[UserTable]

      override def copyEntityFields(entity: User, id: Option[Int], created: Option[Instant], updated: Option[Instant]): User = {
        entity.copy(id = id, created = created, updated = updated)
      }

      implicit val getUserResult = GetResult[User](r => User(r.<<, r.<<, r.<<, r.<<, r.<<))

      def userTable = "user_info"

      def getUsers(year: Int, month: Int) = {
        var monthString = month.toString
        if (month < 10)
          monthString = "0" + month.toString
        val yearMonth = year + "-" + monthString
        val sql = sql"SELECT * FROM #$userTable WHERE birthday::text LIKE '#$yearMonth%'".as[User]
        db.run(sql)
      }
}

当我要执行查询时,它会引入一个错误(用^表示,这是在尝试转换birthday时会引入的问题): -

diverging implicit expansion for type slick.jdbc.GetResult[T1]
starting with object GetIntOption in object GetResult
implicit val getUserResult = GetResult[User](r => User(r.<<, r.<<, r.<<, r.<<, r.<<))
                                                                   ^

请告诉我,如何克服上述问题?

0 个答案:

没有答案