Slick for table>的编译错误22列(使用HLists)

时间:2014-03-11 11:41:07

标签: scala generator slick

我已经创建了一个自定义生成器来将我的数据库映射到我的应用程序中,但我遇到了问题。我已经自定义它以生成自动增量列作为可选项,所以我做了这个:

val codegen = new scala.slick.model.codegen.SourceCodeGenerator(model) {
  override def Table = new Table(_){
    override def autoIncLastAsOption = true
  }
}

对于具有超过22列的表,它使用HList实现而不是Scala Tuple。它产生了这个:

implicit def GetResultVoicemailRow(implicit e0: GR[String], e1: GR[Short], e2: GR[java.sql.Timestamp], e3: GR[Option[Int]]): GR[VoicemailRow] = GR{
   prs => import prs._
   val positional = <<?[Int] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[Short] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[java.sql.Timestamp] :: HNil
   import positional._
   r(1) :: r(2) :: r(3) :: r(4) :: r(5) :: r(6) :: r(7) :: r(8) :: r(9) :: r(10) :: r(11) :: r(12) :: r(13) :: r(14) :: r(15) :: r(16) :: r(17) :: r(18) :: r(19) :: r(20) :: r(21) :: r(22) :: r(23) :: r(24) :: r(0) :: HNil // putting AutoInc last
}

这导致编译错误。 “未找到:价值r”。我该怎么做才能解决这个问题? 感谢。

1 个答案:

答案 0 :(得分:1)

看起来像Slick 2.0.1-RC1中的一个错误。

作为一种解决方法,使用以下修复程序自定义代码生成器。

http://slick.typesafe.com/doc/2.0.1-RC1/code-generation.html#customization

class MyCodeGenerator(model: Model) extends SourceCodeGenerator(model: Model){
  protected def mytuple(i: Int) = termName(s"_${i+1}")
  override def Table = new Table(_){
    override def autoIncLastAsOption = true
    override def PlainSqlMapper = new PlainSqlMapper{
      override def code = {
        val positional = compound(columnsPositional.map(c => (if(c.fakeNullable || c.model.nullable)s"<<?[${c.rawType}]"else s"<<[${c.rawType}]")))
        val dependencies = columns.map(_.exposedType).distinct.zipWithIndex.map{ case (t,i) => s"""e$i: GR[$t]"""}.mkString(", ")
        val rearranged = compound(desiredColumnOrder.map(i => if(hlistEnabled) s"r($i)" else mytuple(i)))
        def result(args: String) = if(mappingEnabled) s"$factory($args)" else args
        val body =
          if(autoIncLastAsOption && columns.size > 1){
            s"""
val r = $positional
import r._
${result(rearranged)} // putting AutoInc last
            """.trim
          } else
            result(positional)
        s"""
implicit def ${name}(implicit $dependencies): GR[${TableClass.elementType}] = GR{
  prs => import prs._
  ${indent(body)}
}
        """.trim
      }
    }
  }
}