我需要为不属于我的架构中存在的表生成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 Driver的以下详细信息:
"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)时则不会生成。当有人驻留在不同的模式中时,有人可以建议我如何获得为Oracle表生成的光滑代码?在此先感谢您的帮助。
答案 0 :(得分: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
}
}