scala – 是否可以自定义Slick代码生成以使生成的类扩展自定义特征?

我目前正在使用Slick codegen(版本3.2.0-M1)为数据库生成Slick代码.我的许多表包含相同的列(具有相同的名称和类型),因此我希望有一些方法可以通用方式对这些表执行操作,例如,可以从任何一个中选择行的泛型方法这些表基于特定的共享字段.

为此,我可以创建一个包含这些共享字段的特征,然后让Slick表类扩展它们或将它们混合.理想情况下,我希望代码生成器添加扩展< trait>或者与< trait>对我来说这些课程.

我看到生成器中有一个可重写的代码方法,但我想避免直接搞乱代码,例如通过正则表达式.

我没有在网上或在Slick文档中找到任何指向使用代码生成器定制的简单解决方案,所以我想知道是否有人知道这是否可能.

最佳答案 我已经设法使用从slick.codegen.AbstractSourceCodeGenerator修改的代码覆盖了几个源代码生成器的可自定义方法:

/* `TableUtils` contains the definitions of the `GenericRow`
      and `GenericTable` traits. */
  override def code = "import data.TableUtils._\n" + super.code

  override def Table = new Table(_) {

    override def EntityType = new EntityTypeDef {

      /* This code is adapted from the `EntityTypeDef` trait's `code` method
         within `AbstractSourceCodeGenerator`.
         All code is identical except for those lines which have a corresponding
         comment above them. */
      override def code = {
        val args = columns.map(c=>
          c.default.map( v =>
            s"${c.name}: ${c.exposedType} = $v"
          ).getOrElse(
            s"${c.name}: ${c.exposedType}"
          )
        ).mkString(", ")
        if(classEnabled){
          /* `rowList` contains the names of the generated "Row" case classes we
              wish to have extend our `GenericRow` trait. */
          val newParents = if (rowList.contains(name)) parents :+ "GenericRow" else parents
          /* Use our modified parent class sequence in place of the old one. */
          val prns = (newParents.take(1).map(" extends "+_) ++ newParents.drop(1).map(" with "+_)).mkString("")
          s"""case class $name($args)$prns"""
        } else {
          s"""type $name = $types
              /** Constructor for $name providing default values if available in the database schema. */
              def $name($args): $name = {
              ${compoundValue(columns.map(_.name))}
              }
            """.trim
        }
      }
    }

    override def TableClass = new TableClassDef {

      /* This code is adapted from the `TableClassDef` trait's `code` method
         within `AbstractSourceCodeGenerator`.
         All code is identical except for those lines which have a corresponding
         comment above them. */
      override def code = {
        /* `tableList` contains the names of the generated table classes we
            wish to have extend our `GenericTable` trait. */
        val newParents = if (tableList.contains(name)) parents :+ "GenericTable" else parents
        /* Use our modified parent class sequence in place of the old one. */
        val prns = newParents.map(" with " + _).mkString("")
        val args = model.name.schema.map(n => s"""Some("$n")""") ++ Seq("\""+model.name.table+"\"")
        s"""class $name(_tableTag: Tag) extends profile.api.Table[$elementType](_tableTag, ${args.mkString(", ")})$prns {
            ${indent(body.map(_.mkString("\n")).mkString("\n\n"))}
            }
          """.trim()
      }
    }
  }
}

这个解决方案适用于我的目的,但复制和修改源代码感觉有点不优雅.如果有人知道更好的方法,我会很高兴看到你提出了什么.

点赞