首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用UnApply上的映射表列类型转换

应用UnApply上的映射表列类型转换
EN

Stack Overflow用户
提问于 2021-04-21 08:08:15
回答 1查看 21关注 0票数 0

我有一个类ReportTemplate,它有字段过滤器。现在,ReportTemplateRow是代表ReportTemplate的db类。我想要的是JsonAST.JValue类型的过滤器在ReportTemplate中,但是字符串在ReportTemplateRow中(当保存在db上时).我希望ReportTemplate和ReportTemplateRow之间的转换能够自动发生。

下面的代码是工作的,但显然没有任何conversion...so,我必须显式地将ReportTemplate转换为ReportTemplateResponse,每次我需要在代码中使用ReportTemplate .

我知道我应该超越应用和不应用的方法,但我无法确切地弄清楚.我用不同的关键字搜索了这个问题,但找不到结论性的答案。

代码语言:javascript
复制
case class ReportTemplate(id: Long, reportId: Long, name: String, group : Option[String], filter : String, created : DateTime)

class ReportTemplateRow(tag: Tag) extends Table[ReportTemplate](tag, "ReportTemplate"){
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def reportId = column[Long]("reportId")
  def name = column[String]("name")
  def group = column[Option[String]]("group")
  def filter = column[String]("filter", O.SqlType("text"))
  def created = column[DateTime]("created",O.SqlType("timestamp not null default CURRENT_TIMESTAMP"))

  def * = (id, reportId, name, group, filter, created) <> (ReportTemplate.tupled, ReportTemplate.unapply)

我希望得到的是类似于以下内容的内容:

代码语言:javascript
复制
case class ReportTemplate(id: Long, reportId: Long, name: String, group : Option[String], filter : JsonAST.JValue, created : DateTime)
object ReportTemplate {
  def unapply(r: ReportTemplate) : (Long, Long, String, Option[String], String, DateTime) = (r.id, r.name, r.group, JsonAST.compactRender(r.filter), r.created)
  def apply(id: Long, reportId: Long, name: String, group : Option[String], filter : String, created : DateTime) : ReportTemplate =
    ReportTemplate(id, reportId, name, group, parse(filter), created)
}

class ReportTemplateRow(tag: Tag) extends Table[ReportTemplate](tag, "ReportTemplate"){
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def reportId = column[Long]("reportId")
  def name = column[String]("name")
  def group = column[Option[String]]("group")
  def filter = column[String]("filter", O.SqlType("text"))
  def created = column[DateTime]("created",O.SqlType("timestamp not null default CURRENT_TIMESTAMP"))

  def * = (id, reportId, name, group, filter, created) <> (ReportTemplate.apply, ReportTemplate.unapply)
EN

回答 1

Stack Overflow用户

发布于 2021-04-22 09:04:16

正如我理解您的问题一样,您有一个文本数据库列(filter),您希望在Scala中使用它的不同类型。

Slick为此提供了一种基于列的机制,如MappedColumnType.base

这样做的目的是:

  • 在数据库行类中,键入要使用的列(本例中为JSON)。
  • 您还提供了从该类型(JSON)到数据库类型(String)的隐式转换。

细节在手册中,也在必要的光滑教程,第五章中。

在大纲中,对于您的情况,可能是这样的(nb: won编译;只是一个草图):

代码语言:javascript
复制
implicit val filterTypeMapping =
    MappedColumnType.base[Json, String](
      json => json.toString, // or however you do this for the JSON you're using
      txt  => json.parse(txt).get /// or however you do this in the JSON lib you're using
    )

在这个范围内,Slick将“学习”如何将JSON数据类型转换为对数据库友好的类型。

请注意,将文本解析为JSON可能会失败。这可能是一个运行时错误,或者您可以选择将其表示为不同的类型,如Try[Json]或类似类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67191733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档