Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val conf = ConfigFactory.parseFile(new File("conf/application.conf")).resolve()
- slick <<= 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 / "slick").getPath
- val url = conf.getString("slick.dbs.default.db.url")
- val jdbcDriver = conf.getString("slick.dbs.default.db.driver")
- val slickDriver = conf.getString("slick.dbs.default.driver").dropRight(1)
- val pkg = "table"
- val user = conf.getString("slick.dbs.default.db.user")
- val password = conf.getString("slick.dbs.default.db.password")
- toError(r.run("codegen.CustomizedCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, pkg, user, password), s.log))
- val fname = outputDir + s"/$pkg/Tables.scala"
- Seq(file(fname))
- }
- package codegen
- class CustomizedCodeGenerator(val model: Model) extends SourceCodeGenerator(model) {
- // customize Scala entity name (case class, etc.)
- override def entityName = dbTableName => dbTableName
- // customize Scala table name (table class, table values, ...)
- override def tableName = dbTableName => dbTableName + "Table"
- }
- object CustomizedCodeGenerator {
- def run(slickDriver: String, jdbcDriver: String, url: String, outputDir: String, pkg: String, user: Option[String], password: Option[String]): Unit = {
- val driver: JdbcProfile = Class.forName(slickDriver + "$").getField("MODULE$").get(null).asInstanceOf[JdbcProfile]
- val dbFactory = driver.api.Database
- val db = dbFactory.forURL(url, driver = jdbcDriver,
- user = user.getOrElse(null), password = password.getOrElse(null), keepAliveConnection = true)
- try {
- val m = Await.result(db.run(driver.createModel(None, false)(ExecutionContext.global).withPinnedSession), Duration.Inf)
- new CustomizedCodeGenerator(m).writeToFile(slickDriver,outputDir,pkg)
- } finally db.close
- }
- def main(args: Array[String]): Unit = {
- args.toList match {
- case slickDriver :: jdbcDriver :: url :: outputDir :: pkg :: Nil =>
- run(slickDriver, jdbcDriver, url, outputDir, pkg, None, None)
- case slickDriver :: jdbcDriver :: url :: outputDir :: pkg :: user :: password :: Nil =>
- run(slickDriver, jdbcDriver, url, outputDir, pkg, Some(user), Some(password))
- case _ => {
- println("""
- |Usage:
- | SourceCodeGenerator configURI [outputDir]
- | SourceCodeGenerator slickDriver jdbcDriver url outputDir pkg [user password]
- |
- |Options:
- | configURI: A URL pointing to a standard database config file (a fragment is
- | resolved as a path in the config), or just a fragment used as a path in
- | application.conf on the class path
- | slickDriver: Fully qualified name of Slick driver class, e.g. "slick.driver.H2Driver"
- | jdbcDriver: Fully qualified name of jdbc driver class, e.g. "org.h2.Driver"
- | url: JDBC URL, e.g. "jdbc:postgresql://localhost/test"
- | outputDir: Place where the package folder structure should be put
- | pkg: Scala package the generated code should be places in
- | user: database connection user name
- | password: database connection password
- |
- |When using a config file, in addition to the standard config parameters from
- |slick.backend.DatabaseConfig you can set "codegen.package" and
- |"codegen.outputDir". The latter can be overridden on the command line.
- """.stripMargin.trim)
- System.exit(1)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement