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 ) } } }