比较快速查询中的类型映射值 [英] Comparing type mapped values in Slick queries

查看:0
本文介绍了比较快速查询中的类型映射值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的收藏夹表对象,我们想要编写一个查询来按类型(定义如下)查找收藏夹。我们还定义了一个Typemapper,将FavoriteType映射到数据库的字符串

import scala.slick.driver.PostgresDriver.simple._
//Other imports have been omitted in this question

object Favorites extends Table[Favorite]("favorites") {

  // Convert the favoriteTypes to strings for the database
  implicit val favoriteMapping: TypeMapper[FavorietType] = MappedTypeMapper.base[FavorietType, String](
    favType => FavorietType.values.find(_ == favType).get.mapping,
    mapping => FavorietType.values.find(_.mapping == mapping).get
  )

  def favoriteType = column[FavoriteType]("type")
  //other columns here

这是我要编写的查询(但是它无法编译)

  def queryByFavoriteType(ftype : FavoriteType)(implicit s: Session) = {
    for(
      f <- Favorieten if  f.favoriteType === ftype
    ) yield f
  }
}

在这里我定义了不同的FavoriteType对象(这在Favorieten对象之外)

sealed case class FavorietType(mapping: String) {
  override def toString = mapping.capitalize
}
object FavoriteType {
  object Exam extends FavoriteType("examen")
  object Topic extends FavoriteType("onderwerp")
  object Paper extends FavoriteType("profielwerkstuk")

  val values = Seq(Exam , Topic , Paper )
}
我这里遇到的问题是查询不能编译: value === is not a member of scala.slick.lifted.Column[models.gebruiker.FavorietType]

似乎=不能用于比较用户定义的类型,这是真的吗?是否有其他方法可以做到这一点?

编辑

相关问题:在我没有显式类型的TypeMapper之前,它被定义为implicit val favoriteMapping = MappedTypeMapper.base[FavorietType, String]( ...

当我要编写一个查询来比较FavoriteType.Exam(例如)时,例如

  def queryByFavoriteExam()(implicit s: Session) = {
    for(f <- Favorieten if f.favorietType === FavorietType.Exam) yield f
  }
这将导致错误could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[models.gebruiker.FavorietType.Exam.type] 此问题的解决方案与下面提供的解决方案相同

推荐答案

怀疑Slick时,go check out the unit tests。在阅读了他们关于在自定义类型中映射的文档,然后查看了他们的单元测试之后,我通过将查询代码更改为:

来获得要编译的查询代码
def queryByFavoriteType(ftype : FavoriteType)(implicit s: Session) = {
  for(f <- Favorites if f.favoriteType === (ftype:FavoriteType)) yield f
}

另外,我导入H2Driver只是为了编译(import scala.slick.driver.H2Driver.simple._)。我假设您也已经为数据库导入了所需的任何驱动程序。

编辑

我的完整代码示例如下:

import scala.slick.driver.PostgresDriver.simple._
import scala.slick.session.Session

sealed case class FavoriteType(mapping: String) {
  override def toString = mapping.capitalize
}

case class Favorite(ft:FavoriteType, foo:String)
object FavoriteType {
  object Exam extends FavoriteType("examen")
  object Topic extends FavoriteType("onderwerp")
  object Paper extends FavoriteType("profielwerkstuk")

  val values = Seq(Exam , Topic , Paper )
}

object Favorites extends Table[Favorite]("favorites") {
  // Convert the favoriteTypes to strings for the database
  implicit val favoriteMapping = MappedTypeMapper.base[FavoriteType, String](
    {favType => FavoriteType.values.find(_ == favType).get.mapping},
    {mapping => FavoriteType.values.find(_.mapping == mapping).get}
  )

  def favoriteType = column[FavoriteType]("type")
  def foo = column[String]("foo")

  def * = favoriteType ~ foo <> (Favorite.apply _, Favorite.unapply _)

  def queryByFavoriteType(ftype : FavoriteType)(implicit s: Session) = {
    for(f <- Favorites if f.favoriteType === (ftype:FavoriteType)) yield f
  }   
}

这篇关于比较快速查询中的类型映射值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆