仅为单个模式生成流畅的代码 [英] Slick code generation for only a single schema
问题描述
有没有办法让 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**
:我使用options
Model (publicSchema
)> 来自原始模型,并使用来自原始模型的tables
集的过滤版本.事实证明,来自public
模式的表在模型中没有获得模式名称.因此isEmpty
.如果您需要来自一个或多个其他架构的表,您可以轻松创建不同的过滤器表达式.//**3**
:我使用创建的publicSchema
模型创建了一个SourceCodeGenerator
.
- I copied the
run
function from theSourceCodeGenerator
class that's in theslick-codegen
library. (I used versionslick-codegen_2.10-3.1.1
.) // **1**
: In the origninal code, the generatedModel
was referenced in aval
calledm
. I renamed that toallSchemas
.// **2**
: I created a newModel
(publicSchema
), using theoptions
from the original model, and using a filtered version of thetables
set from the original model. It turns out tables from thepublic
schema don't get a schema name in the model. Hence theisEmpty
. Should you need tables from one or more other schemas, you can easily create a different filter expression.// **3**
: I create aSourceCodeGenerator
with the createdpublicSchema
model.
当然,如果 Slick 代码生成器可以包含一个选项来选择一个或多个模式,那就更好了.
Of course, it would even be better if the Slick codegenerator could incorporate an option to select one or more schemas.
这篇关于仅为单个模式生成流畅的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!