如何使用Slick代码生成器也包括数据库视图? [英] How to use Slick code generator to include database views as well?

查看:258
本文介绍了如何使用Slick代码生成器也包括数据库视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Slick 3.0.3为我的架构中的数据库表和视图生成Scala代码。以此博客为例,我有以下文件 build.sbt 。但是,这将为我的数据库表生成代码,并且将不包括数据库视图。如何获取生成的视图?

I'm trying to generate the Scala code for the database tables and views in my schema using Slick 3.0.3. Taking this blog as example I have the following file build.sbt. However, this will generate code for my database tables and will not include the database views. How can I get the views generated as well?

根据 slick问题1022 我认为可以这样做,但API看起来并不相似,并且 slick.codegen.SourceCodeGenerator 没有 getTables defaultTables 来包含视图名称。

According to slick issue 1022 I see it is possible to do but the API doesn't look alike and slick.codegen.SourceCodeGenerator doesn't have a getTables or defaultTables to include view names.

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

After a lot of trial and error due to the many changes in the Slick generator API the following standalone application Generator.scala tested will generate the code for both tables and views under 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 <中找到一个非常过时的示例/ a>,但请注意,它们没有 build.sbt ,但更高级的 Build.scala

Integrating this code into a build.sbt is not that easy because requires defining an external custom code generator file and then compiling and running it from the build.sbt before the actual project is compiled. A very outdated example of this can be found in the github project slick-codegen-customization-example but note that they don't have a build.sbt but a more advanced Build.scala

这篇关于如何使用Slick代码生成器也包括数据库视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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