自定义Slick代码生成器3

自定义Slick代码生成器3

本文介绍了自定义Slick代码生成器3.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

光滑的代码生成器能否生成${container} trait之外的所有映射案例类,以使它们不继承其类型?也许在另一个文件中,即Models.scala?

Can slick codegen generate all the mapped case classes outside of the ${container} trait, so that they don't inherit its type? Maybe in another file altogether i.e. Models.scala?

// SuppliersRowsDAA.scala
import persistence.Tables

object SuppliersRowsDAA {
  case class Save(sup: Tables.SuppliersRow)
}

我收到此编译错误:

[error] /app/src/main/scala/persistence/dal/SuppliersDAA.scala:5: type mismatch;
[error]  found   : persistence.Tables.SuppliersRow
[error]  required: SuppliersDAA.this.SuppliersRow
[error]     case Save(sup) ⇒ sender ! db.run(Suppliers += sup)

使用Tables#SuppliersRow导入会出现相同的错误.

Using Tables#SuppliersRow import gives the same error.

如果我手动剪切&将SuppliersRow case类粘贴到自动生成的trait Tables起作用的外部!

If I manually cut & paste the SuppliersRow case class outside of the auto-generated trait Tables it works!

....
trait Tables {
....
}
case class SuppliersRow(id: Int, userId: Int, name: String)
//EOF

有什么想法吗?

推荐答案

EntityType的原始docWithCode附加到super.packageCode():

import slick.codegen.SourceCodeGenerator
import slick.model.Model
import scala.collection.mutable

class CustomizedCodeGenerator(model: Model) extends SourceCodeGenerator(model) {
  val models = new mutable.MutableList[String]

  override def packageCode(profile: String, pkg: String, container: String, parentType: Option[String]): String = {
    super.packageCode(profile, pkg, container, parentType) + "\n" + outsideCode
  }

  def outsideCode = s"${indent(models.mkString("\n"))}"

  override def Table = new Table(_) {
    override def EntityType = new EntityTypeDef {
      override def docWithCode: String = {
        models += super.docWithCode.toString + "\n"
        ""
      }
    }
  }

}

这篇关于自定义Slick代码生成器3.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:01