I have a requirement to generate Slick custom code for the tables present in schema not owned by me but having select privileges on them. The code I am using is :
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
}
}
The build.sbt has below details for Slick and 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 generates the Slick code successfully when the tables reside in user schema (user), but not when the tables reside in other schema(like GEO above). Can somebody suggest me how can I get the slick code generated for Oracle table when the table reside in different schemas? Thanks in advance for your help.
The below one generated slick code for tables from the selected schema: