gpt4 book ai didi

scala - 如何使用 Slick 代码生成器来包含数据库 View ?

转载 作者:行者123 更新时间:2023-12-04 19:03:01 26 4
gpt4 key购买 nike

我正在尝试使用 Slick 3.0.3 为我的模式中的数据库表和 View 生成 Scala 代码。服用 this blog例如,我有以下文件 build.sbt .但是,这将为我的数据库表生成代码,并且不包括数据库 View 。我怎样才能获得生成的 View ?

根据 slick issue 1022我认为可以这样做,但 API 与 slick.codegen.SourceCodeGenerator 不一样没有 getTablesdefaultTables包括 View 名称。

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 下为表和 View 生成代码:

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

关于scala - 如何使用 Slick 代码生成器来包含数据库 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32756629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com