如何使用 Slick 代码生成器来包含数据库视图?
How to use Slick code generator to include database views as well?
我正在尝试使用 Slick 3.0.3 为我的模式中的数据库表和视图生成 Scala 代码。以 this blog 为例,我有以下文件 build.sbt
。但是,这将为我的数据库表生成代码,并且不包括数据库视图。我怎样才能获得生成的视图?
根据 slick issue 1022,我认为可以做到,但是 API 看起来不一样,并且 slick.codegen.SourceCodeGenerator
没有 getTables
或 defaultTables
以包含视图名称。
name := "slickCodeGen"
version := "1.0"
scalaVersion := "2.11.6"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.0.3",
"com.typesafe.slick" %% "slick-codegen" % "3.0.3",
"org.postgresql" % "postgresql" % "9.4-1201-jdbc41",
"com.zaxxer" % "HikariCP" % "2.3.2",
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
)
slick <<= slickCodeGenTask
sourceGenerators in Compile <+= slickCodeGenTask
lazy val slick = TaskKey[Seq[File]]("gen-tables")
lazy val slickCodeGenTask = (sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
val outputDir = (dir / "main/slick").getPath
val username = "postgres"
val password = "xxx"
val url = "jdbc:postgresql://localhost:5555/testdb?searchpath=public"
val jdbcDriver = "com.postgresql.jdbc.Driver"
val slickDriver = "slick.driver.PostgresDriver"
val pkg = "folder1.folder2"
toError(r.run("slick.codegen.SourceCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, pkg, username, password), s.log))
val fname = outputDir + "/folder1/folder2/" + "Tables.scala"
Seq(file(fname))
}
由于 Slick 生成器中的许多更改,经过大量试验和错误后 API 以下独立应用程序 Generator.scala
经过测试 将为表和视图生成代码在 Slick 3.0.3 下:
import java.util.concurrent.TimeUnit
import slick.driver.PostgresDriver
import slick.jdbc.meta.MTable
import slick.codegen.SourceCodeGenerator
import slick.driver.PostgresDriver.simple._
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
object Generator extends App {
val slickDriver = "slick.driver.PostgresDriver"
val jdbcDriver = "org.postgresql.Driver"
val url = "jdbc:postgresql://localhost:5555/testdb?searchpath=public"
val outputDir = "/tmp/"
val pkg = "folder1.folder2"
val username = "postgres"
val password = "xxx"
val db = Database.forURL(url, user, password)
val dbio = PostgresDriver.createModel(Some(MTable.getTables(None, None, None, Some(Seq("TABLE", "VIEW")))))
val model = db.run(dbio)
val future : Future[SourceCodeGenerator] = model.map(model => new SourceCodeGenerator(model))
val codegen : SourceCodeGenerator = Await.result(future, Duration.create(5, TimeUnit.MINUTES))
codegen.writeToFile(slickDriver, outputDir, pkg, "Tables", "Tables.scala")
}
将此代码集成到 build.sbt
中并不是那么容易,因为需要定义一个外部自定义代码生成器文件,然后在实际项目完成之前从 build.sbt
编译和 运行 它编译。可以在 github 项目 slick-codegen-customization-example 中找到一个非常过时的示例,但请注意,他们没有 build.sbt
,而是更高级的 Build.scala
我正在尝试使用 Slick 3.0.3 为我的模式中的数据库表和视图生成 Scala 代码。以 this blog 为例,我有以下文件 build.sbt
。但是,这将为我的数据库表生成代码,并且不包括数据库视图。我怎样才能获得生成的视图?
根据 slick issue 1022,我认为可以做到,但是 API 看起来不一样,并且 slick.codegen.SourceCodeGenerator
没有 getTables
或 defaultTables
以包含视图名称。
name := "slickCodeGen"
version := "1.0"
scalaVersion := "2.11.6"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.0.3",
"com.typesafe.slick" %% "slick-codegen" % "3.0.3",
"org.postgresql" % "postgresql" % "9.4-1201-jdbc41",
"com.zaxxer" % "HikariCP" % "2.3.2",
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
)
slick <<= slickCodeGenTask
sourceGenerators in Compile <+= slickCodeGenTask
lazy val slick = TaskKey[Seq[File]]("gen-tables")
lazy val slickCodeGenTask = (sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
val outputDir = (dir / "main/slick").getPath
val username = "postgres"
val password = "xxx"
val url = "jdbc:postgresql://localhost:5555/testdb?searchpath=public"
val jdbcDriver = "com.postgresql.jdbc.Driver"
val slickDriver = "slick.driver.PostgresDriver"
val pkg = "folder1.folder2"
toError(r.run("slick.codegen.SourceCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, pkg, username, password), s.log))
val fname = outputDir + "/folder1/folder2/" + "Tables.scala"
Seq(file(fname))
}
由于 Slick 生成器中的许多更改,经过大量试验和错误后 API 以下独立应用程序 Generator.scala
经过测试 将为表和视图生成代码在 Slick 3.0.3 下:
import java.util.concurrent.TimeUnit
import slick.driver.PostgresDriver
import slick.jdbc.meta.MTable
import slick.codegen.SourceCodeGenerator
import slick.driver.PostgresDriver.simple._
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
object Generator extends App {
val slickDriver = "slick.driver.PostgresDriver"
val jdbcDriver = "org.postgresql.Driver"
val url = "jdbc:postgresql://localhost:5555/testdb?searchpath=public"
val outputDir = "/tmp/"
val pkg = "folder1.folder2"
val username = "postgres"
val password = "xxx"
val db = Database.forURL(url, user, password)
val dbio = PostgresDriver.createModel(Some(MTable.getTables(None, None, None, Some(Seq("TABLE", "VIEW")))))
val model = db.run(dbio)
val future : Future[SourceCodeGenerator] = model.map(model => new SourceCodeGenerator(model))
val codegen : SourceCodeGenerator = Await.result(future, Duration.create(5, TimeUnit.MINUTES))
codegen.writeToFile(slickDriver, outputDir, pkg, "Tables", "Tables.scala")
}
将此代码集成到 build.sbt
中并不是那么容易,因为需要定义一个外部自定义代码生成器文件,然后在实际项目完成之前从 build.sbt
编译和 运行 它编译。可以在 github 项目 slick-codegen-customization-example 中找到一个非常过时的示例,但请注意,他们没有 build.sbt
,而是更高级的 Build.scala