From 4c709046fe329113113b15de62a181cfc8fa5c13 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 1 Feb 2015 00:48:17 +0100 Subject: Add model code generator --- codegen/ModelGenerator.scala | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 codegen/ModelGenerator.scala (limited to 'codegen') diff --git a/codegen/ModelGenerator.scala b/codegen/ModelGenerator.scala new file mode 100644 index 0000000..9e74ff1 --- /dev/null +++ b/codegen/ModelGenerator.scala @@ -0,0 +1,60 @@ +package codegen + +object ModelGenerator { + + import scala.reflect.runtime.currentMirror + import scala.slick.codegen.SourceCodeGenerator + import scala.slick.driver.JdbcProfile + import scala.slick.model.Model + + + class CustomSourceCodeGenerator(model: Model) extends SourceCodeGenerator(model) { + override def entityName = dbTableName => dbTableName.dropRight(1).toLowerCase.toCamelCase + + override def tableName = dbTableName => dbTableName.toLowerCase.toCamelCase + } + + def main(args: Array[String]) = { + + args.toList match { + + case List(slickDriver, jdbcDriver, url, tables, outputFolder, className, pkg) => + + val driver: JdbcProfile = { + val module = currentMirror.staticModule(slickDriver) + val reflectedModule = currentMirror.reflectModule(module) + val driver = reflectedModule.instance.asInstanceOf[JdbcProfile] + driver + } + + driver.simple.Database.forURL(url, driver = jdbcDriver).withSession { implicit session => + val tableSeq = Option(driver.getTables.list.filter { t => tables.split(",") contains t.name.name}.toSeq) + val model = driver.createModel(tableSeq) + new CustomSourceCodeGenerator(model).writeToFile( + "play.api.db.slick.Config.driver", + outputFolder, + pkg, + className, + className + ".scala" + ) + } + + + case _ => + println( """ +Usage: SourceCodeGenerator.main(Array( slickDriver, jdbcDriver, url, outputFolder, pkg )) +slickDriver: Fully qualified name of Slick driver class, e.g. "scala.slick.driver.PostgresDriver" +jdbcDriver: Fully qualified name of jdbc driver class, e.g. "org.postgresql.Driver" +url: jdbc url, e.g. "jdbc:postgresql://localhost/test?user=someone&password=somepass&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory" +tables: List of the tables +outputFolder: Place where the package folder structure should be put +className : Name of the class +pkg: Scala package the generated code should be places in + """.trim + ) + + } + + } + +} -- cgit v1.2.3