0

我需要为不属于我但对它们具有选择权限的模式中存在的表生成 Slick 自定义代码。我正在使用的代码是:

import scala.util.{Failure, Success}
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import com.typesafe.slick.driver.oracle.OracleDriver

object CustomCodeGenerator extends  App{

  val url = "jdbc:oracle:thin:@//localhost:1521/xe"
  val jdbcDriver = "oracle.jdbc.OracleDriver"
  val userName = "user"
  val password = "password"
  val db = OracleDriver.api.Database.forURL(url,user=userName,password=password,driver=jdbcDriver)

  // filter out desired tables
  val included = Seq("GEO.BR_FLD","GEO.FLD_GM_SDO","GEO.BR_REP","GEO.PLT","GEO.PLT_GM_SDO")
  val codegen = db.run{
    OracleDriver.defaultTables.map(_.filter(t => included contains t.name.name)).flatMap( OracleDriver.createModelBuilder(_,false).buildModel)
  }
    .map{ model =>
      new slick.codegen.SourceCodeGenerator(model){
        override def entityName = dbTableName => dbTableName match {
          case "GEO.BR_FLD" => "BrFld"
          case "GEO.FLD_GM_SDO" => "FldGmSDO"
          case "GEO.BR_REP" => "BrRep"
          case "GEO.PLT" => "Plt"
          case "GEO.PLT_GM_SDO" => "PltGmSDO"
          case _ => super.entityName(dbTableName)
        }
      }
    }
  Await.ready(
    codegen.map(_.writeToFile(
      "com.typesafe.slick.driver.oracle.OracleDriver",
      ".",
      "demo",
      "Tables",
      "Tables.scala"
    )),
    300.seconds
  ).onComplete{
    case Success(value) => println("Code Generation Done")
    case Failure(e) => e.printStackTrace
  }

}

build.sbt 包含以下有关 Slick 和 Oracle 驱动程序的详细信息:

  "com.typesafe.slick"      %% "slick"                  % "3.1.1",
  "com.typesafe.slick"      %% "slick-codegen"          % "3.1.1",
  "com.typesafe.slick"      %% "slick-hikaricp"         % "3.1.1",
  "com.typesafe.slick"      %% "slick-extensions"       % "3.1.0",
  "com.hynnet"               % "oracle-driver-ojdbc"    % "12.1.0.2"

当表驻留在用户模式(用户)中时,CustomCodeGenerator 成功生成 Slick 代码,但当表驻留在其他模式中(如上面的 GEO)时不会成功生成 Slick 代码。当表驻留在不同的模式中时,有人可以建议我如何获得为 Oracle 表生成的流畅代码吗?在此先感谢您的帮助。

4

1 回答 1

0

下面的一个为所选模式中的表生成了漂亮的代码:

object CustomCodeGenerator extends  App {
  val url = "jdbc:oracle:thin:@//localhost:1521/xe"
  val jdbcDriver = "oracle.jdbc.OracleDriver"
  val userName = "user"
  val password = "password"
  val db = OracleDriver.api.Database.forURL(url, user = userName, password = password, driver = jdbcDriver)

  // filter out desired tables
  val requiredTables = Seq("br_fld", "fld_gm_sdo", "br_rep", "plt_gm_sdo")
  // filter out desired schema
  val requiredSchema = Seq("plot")
  val codegen = db.run {
    OracleDriver.defaultTables
      .map(_.filter(p => requiredTables.contains(p.name.name.toLowerCase) && requiredSchema.contains(p.name.schema.get.toLowerCase)))
      .flatMap(OracleDriver.createModelBuilder(_, false).buildModel)
  }
    .map { model =>
      new slick.codegen.SourceCodeGenerator(model) {
        override def entityName = dbTableName => dbTableName match {
          case _ => super.entityName(dbTableName)
        }
      }
    }

  Await.ready(
    codegen.map(_.writeToFile(
      "com.typesafe.slick.driver.oracle.OracleDriver", ".", "demo", "Tables", "Tables.scala")),
    300.seconds
  ).onComplete {
    case Success(value) => println("Code Generation Done")
    case Failure(e) => e.printStackTrace
  }
}
于 2016-11-09T23:52:10.300 回答