Advertisement
Guest User

Untitled

a guest
Apr 11th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. val conf = ConfigFactory.parseFile(new File("conf/application.conf")).resolve()
  2. slick <<= slickCodeGenTask
  3.  
  4. lazy val slick = TaskKey[Seq[File]]("gen-tables")
  5. lazy val slickCodeGenTask = (sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
  6. val outputDir = (dir / "slick").getPath
  7. val url = conf.getString("slick.dbs.default.db.url")
  8. val jdbcDriver = conf.getString("slick.dbs.default.db.driver")
  9. val slickDriver = conf.getString("slick.dbs.default.driver").dropRight(1)
  10. val pkg = "table"
  11. val user = conf.getString("slick.dbs.default.db.user")
  12. val password = conf.getString("slick.dbs.default.db.password")
  13. toError(r.run("codegen.CustomizedCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, pkg, user, password), s.log))
  14. val fname = outputDir + s"/$pkg/Tables.scala"
  15. Seq(file(fname))
  16. }
  17.  
  18. package codegen
  19.  
  20. class CustomizedCodeGenerator(val model: Model) extends SourceCodeGenerator(model) {
  21. // customize Scala entity name (case class, etc.)
  22. override def entityName = dbTableName => dbTableName
  23. // customize Scala table name (table class, table values, ...)
  24. override def tableName = dbTableName => dbTableName + "Table"
  25. }
  26.  
  27. object CustomizedCodeGenerator {
  28. def run(slickDriver: String, jdbcDriver: String, url: String, outputDir: String, pkg: String, user: Option[String], password: Option[String]): Unit = {
  29. val driver: JdbcProfile = Class.forName(slickDriver + "$").getField("MODULE$").get(null).asInstanceOf[JdbcProfile]
  30. val dbFactory = driver.api.Database
  31. val db = dbFactory.forURL(url, driver = jdbcDriver,
  32. user = user.getOrElse(null), password = password.getOrElse(null), keepAliveConnection = true)
  33.  
  34. try {
  35. val m = Await.result(db.run(driver.createModel(None, false)(ExecutionContext.global).withPinnedSession), Duration.Inf)
  36. new CustomizedCodeGenerator(m).writeToFile(slickDriver,outputDir,pkg)
  37. } finally db.close
  38. }
  39.  
  40. def main(args: Array[String]): Unit = {
  41. args.toList match {
  42. case slickDriver :: jdbcDriver :: url :: outputDir :: pkg :: Nil =>
  43. run(slickDriver, jdbcDriver, url, outputDir, pkg, None, None)
  44. case slickDriver :: jdbcDriver :: url :: outputDir :: pkg :: user :: password :: Nil =>
  45. run(slickDriver, jdbcDriver, url, outputDir, pkg, Some(user), Some(password))
  46. case _ => {
  47. println("""
  48. |Usage:
  49. | SourceCodeGenerator configURI [outputDir]
  50. | SourceCodeGenerator slickDriver jdbcDriver url outputDir pkg [user password]
  51. |
  52. |Options:
  53. | configURI: A URL pointing to a standard database config file (a fragment is
  54. | resolved as a path in the config), or just a fragment used as a path in
  55. | application.conf on the class path
  56. | slickDriver: Fully qualified name of Slick driver class, e.g. "slick.driver.H2Driver"
  57. | jdbcDriver: Fully qualified name of jdbc driver class, e.g. "org.h2.Driver"
  58. | url: JDBC URL, e.g. "jdbc:postgresql://localhost/test"
  59. | outputDir: Place where the package folder structure should be put
  60. | pkg: Scala package the generated code should be places in
  61. | user: database connection user name
  62. | password: database connection password
  63. |
  64. |When using a config file, in addition to the standard config parameters from
  65. |slick.backend.DatabaseConfig you can set "codegen.package" and
  66. |"codegen.outputDir". The latter can be overridden on the command line.
  67. """.stripMargin.trim)
  68. System.exit(1)
  69. }
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement