仅为单个模式生成流畅的代码 [英] Slick code generation for only a single schema

查看:38
本文介绍了仅为单个模式生成流畅的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 Slick 的代码生成器只为单个模式生成代码?说,公开?我有创建大量表格的扩展(例如 postgis、pg_jobman),这些扩展使流畅的代码生成巨大的代码.

Is there a way to have Slick's code generation generate code for only a single schema? Say, public? I have extensions that create a whole ton of tables (eg postgis, pg_jobman) that make the code that slick generates gigantic.

推荐答案

我遇到了同样的问题,找到了这个问题.S.Karthik 的回答让我朝着正确的方向前进.但是,答案中的代码略有过时.而且我觉得有点过于复杂了.所以我制定了自己的解决方案:

I encountered the same problem and I found this question. The answer by S.Karthik sent me in the right direction. However, the code in the answer is slightly outdated. And I think a bit over-complicated. So I crafted my own solution:

import slick.codegen.SourceCodeGenerator
import slick.driver.JdbcProfile
import slick.model.Model

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext}

val slickDriver = "slick.driver.PostgresDriver"
val jdbcDriver = "org.postgresql.Driver"
val url = "jdbc:postgresql://localhost:5432/mydb"
val outputFolder = "/path/to/src/test/scala"
val pkg = "com.mycompany"
val user = "user"
val password = "password"


object MySourceCodeGenerator {

  def run(slickDriver: String, jdbcDriver: String, url: String, outputDir: String,
          pkg: String, user: Option[String], password: Option[String]): Unit = {

    val driver: JdbcProfile =
      Class.forName(slickDriver + "$").getField("MODULE$").get(null).asInstanceOf[JdbcProfile]
    val dbFactory = driver.api.Database
    val db = dbFactory.forURL(url, driver = jdbcDriver, user = user.orNull,
                              password = password.orNull, keepAliveConnection = true)
    try {
      // **1**
      val allSchemas = Await.result(db.run(
        driver.createModel(None, ignoreInvalidDefaults = false)(ExecutionContext.global).withPinnedSession), Duration.Inf)
      // **2**
      val publicSchema = new Model(allSchemas.tables.filter(_.name.schema.isEmpty), allSchemas.options)
      // **3**
      new SourceCodeGenerator(publicSchema).writeToFile(slickDriver, outputDir, pkg)
    } finally db.close
  }
}

MySourceCodeGenerator.run(slickDriver, jdbcDriver, url, outputFolder, pkg, Some(user), Some(password))

我会解释这里发生了什么:

I'll explain what's going on here:

  • 我从 slick-codegen 库中的 SourceCodeGenerator 类中复制了 run 函数.(我使用了 slick-codegen_2.10-3.1.1 版本.)
  • //**1**:在原始代码中,生成的Model在名为m的val中被引用.我将其重命名为 allSchemas.
  • //**2**:我使用optionsModel (publicSchema)> 来自原始模型,并使用来自原始模型的 tables 集的过滤版本.事实证明,来自 public 模式的表在模型中没有获得模式名称.因此 isEmpty.如果您需要来自一个或多个其他架构的表,您可以轻松创建不同的过滤器表达式.
  • //**3**:我使用创建的 publicSchema 模型创建了一个 SourceCodeGenerator.
  • I copied the run function from the SourceCodeGenerator class that's in the slick-codegen library. (I used version slick-codegen_2.10-3.1.1.)
  • // **1**: In the origninal code, the generated Model was referenced in a val called m. I renamed that to allSchemas.
  • // **2**: I created a new Model (publicSchema), using the options from the original model, and using a filtered version of the tables set from the original model. It turns out tables from the public schema don't get a schema name in the model. Hence the isEmpty. Should you need tables from one or more other schemas, you can easily create a different filter expression.
  • // **3**: I create a SourceCodeGenerator with the created publicSchema model.

当然,如果 Slick 代码生成器可以包含一个选项来选择一个或多个模式,那就更好了.

Of course, it would even be better if the Slick codegenerator could incorporate an option to select one or more schemas.

这篇关于仅为单个模式生成流畅的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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