scala – 如何使用新的Slick 2.0 HList克服22列限制?
发布时间:2020-12-16 09:22:51 所属栏目:安全 来源:网络整理
导读:我目前正在编写Slick代码以将两个表 22列.我如何 use the new HList code?在Scala 2.10.3下,我的2.0-M3在其他方面工作正常. 这是我正在使用的case类/元组的语法.我会怎么做到 use the new HLists mentioned in the docs? case class Joiner( id: Int,name:
我目前正在编写Slick代码以将两个表> 22列.我如何
use the new HList code?在Scala 2.10.3下,我的2.0-M3在其他方面工作正常.
这是我正在使用的case类/元组的语法.我会怎么做到 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. 解决方法
定义
Scala> = 2.10.4-RC2(也由Slick 2.0.0代码生成器发出): 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列而言不可行. Scala的解决方法< = 2.10.3 / 2.10.4-RC1(也由Slick 2.0.1代码生成器发出) import scala.slick.collection.heterogenous._ import syntax._ class Joiners(tag: Tag) extends Table[ HCons[Int,HCons[Option[String],HNil]]] ](tag,"joiner") { ... def * = id :: name :: contact :: HNil } 由我们测试30-40列没有问题. 目前仍然存在Scala 2.10.4-RC2中偶尔的零星编译错误的问题,它似乎将在即将到来的2.10.4-RC3中被修复.见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). 另外:不要使用O.Nullable.改用列[Option [String]].它推测无效. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |