如何使用新的 Slick 2.0 HList 来克服 22 列的限制? [英] How can I use the new Slick 2.0 HList to overcome 22 column limit?

查看:37
本文介绍了如何使用新的 Slick 2.0 HList 来克服 22 列的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写 Slick 代码以针对具有两个 > 22 列的表的旧模式.我如何使用新的 HList 代码?我有 2.0-M3 在 Scala 2.10.3 下的其他方面工作正常.这是我目前在案例类/元组中使用的语法.我将如何使用文档中提到的新 HList?>

I'm currently writing Slick code to target an old schema with two tables > 22 columns. How do I use the new HList code? I've got 2.0-M3 working fine in other respects under Scala 2.10.3. Here's the syntax I'm currently using with case classes / tuples. What would I do to use the new HLists mentioned in the docs?

  case class Joiner(
      id: Int,
      name: Option[String],
      contact: Option[String]
  )

  class Joiners(tag: Tag) extends Table[Joiner](tag, "joiner") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc, O.DBType("int(11)"))
    def name = column[Option[String]]("name", O.DBType("varchar(255)"))
    def contact = column[Option[String]]("contact", O.DBType("text"))
    def * = (id, name.?, contact.?) <> (Joiner.tupled, Joiner.unapply)
  }
  val joiners = TableQuery[Joiners]

我在示例中没有看到任何内容,仅在新更新的文档中简要提及.我是 Scala 和 Slick 的新手.

I don't see any in the examples and only a brief mention in the newly updated docs. I'm new to Scala as well as Slick.

推荐答案

定义

使用 Scala >= 2.10.4-RC2(也由 Slick 2.0.0 代码生成器发出):

With Scala >= 2.10.4-RC2 (also emitted by the Slick 2.0.0 code generator):

import scala.slick.collection.heterogenous._
import syntax._
class Joiners(tag: Tag) extends Table[
    Int :: Option[String] :: Option[String] :: HNil
](tag, "joiner") {
  ...
  def * = id :: name :: contact :: HNil
}

以上导致 Scala 2.10.3/2.10.4-RC1 的编译时间呈指数级增长.由于编译时间过长,对于超过 26 列不可行.

The above leads to exponential compilation times in Scala 2.10.3 / 2.10.4-RC1. Not feasible for more than 26 columns due to extremely long compilation.

Scala <= 2.10.3/2.10.4-RC1 的解决方法(也由 Slick 2.0.1 代码生成器发出)

Workaround for Scala <= 2.10.3 / 2.10.4-RC1 (also emitted by the Slick 2.0.1 code generator)

import scala.slick.collection.heterogenous._
import syntax._
class Joiners(tag: Tag) extends Table[
    HCons[Int, HCons[Option[String], HCons[Option[String], HNil]]]
](tag, "joiner") {
  ...
  def * = id :: name :: contact :: HNil
}

我们用 30-40 列测试没有问题.

Tested by us with 30-40 columns without problems.

目前在 Scala 2.10.4-RC2 中似乎仍然存在偶发性编译错误的问题,看起来它将在即将到来的 2.10.4-RC3 中修复.请参阅 https://issues.scala-lang.org/browse/SI-8146

There currently still seem to be a problem with occasional sporadic compilation errors in Scala 2.10.4-RC2, which looks like it will be fixed in the upcoming 2.10.4-RC3. See https://issues.scala-lang.org/browse/SI-8146

示例用法

Joiners.run.map( r => r(2) ) // Gets column contact. It's typesafe. .apply is a macro. Only works for literals not for variables as positions.

将元组用于 <22 能够将它们映射到案例类.将 HLists 用于 > 22 而不映射到案例类(Scala 2.10 中的最大字段限制为 22).

Use tuples for < 22 to be able to map them to a case class. Use HLists for > 22 without mapping to a case class (max field limit in Scala 2.10 is 22).

另外:不要使用 O.Nullable.请改用 column[Option[String]].它推断可空性.

Also: Do NOT use O.Nullable. Use column[Option[String]] instead. It infers nullability.

这篇关于如何使用新的 Slick 2.0 HList 来克服 22 列的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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