diff options
Diffstat (limited to 'codegen')
-rw-r--r-- | codegen/ModelGenerator.scala | 60 |
1 files changed, 60 insertions, 0 deletions
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 @@ | |||
1 | package codegen | ||
2 | |||
3 | object ModelGenerator { | ||
4 | |||
5 | import scala.reflect.runtime.currentMirror | ||
6 | import scala.slick.codegen.SourceCodeGenerator | ||
7 | import scala.slick.driver.JdbcProfile | ||
8 | import scala.slick.model.Model | ||
9 | |||
10 | |||
11 | class CustomSourceCodeGenerator(model: Model) extends SourceCodeGenerator(model) { | ||
12 | override def entityName = dbTableName => dbTableName.dropRight(1).toLowerCase.toCamelCase | ||
13 | |||
14 | override def tableName = dbTableName => dbTableName.toLowerCase.toCamelCase | ||
15 | } | ||
16 | |||
17 | def main(args: Array[String]) = { | ||
18 | |||
19 | args.toList match { | ||
20 | |||
21 | case List(slickDriver, jdbcDriver, url, tables, outputFolder, className, pkg) => | ||
22 | |||
23 | val driver: JdbcProfile = { | ||
24 | val module = currentMirror.staticModule(slickDriver) | ||
25 | val reflectedModule = currentMirror.reflectModule(module) | ||
26 | val driver = reflectedModule.instance.asInstanceOf[JdbcProfile] | ||
27 | driver | ||
28 | } | ||
29 | |||
30 | driver.simple.Database.forURL(url, driver = jdbcDriver).withSession { implicit session => | ||
31 | val tableSeq = Option(driver.getTables.list.filter { t => tables.split(",") contains t.name.name}.toSeq) | ||
32 | val model = driver.createModel(tableSeq) | ||
33 | new CustomSourceCodeGenerator(model).writeToFile( | ||
34 | "play.api.db.slick.Config.driver", | ||
35 | outputFolder, | ||
36 | pkg, | ||
37 | className, | ||
38 | className + ".scala" | ||
39 | ) | ||
40 | } | ||
41 | |||
42 | |||
43 | case _ => | ||
44 | println( """ | ||
45 | Usage: SourceCodeGenerator.main(Array( slickDriver, jdbcDriver, url, outputFolder, pkg )) | ||
46 | slickDriver: Fully qualified name of Slick driver class, e.g. "scala.slick.driver.PostgresDriver" | ||
47 | jdbcDriver: Fully qualified name of jdbc driver class, e.g. "org.postgresql.Driver" | ||
48 | url: jdbc url, e.g. "jdbc:postgresql://localhost/test?user=someone&password=somepass&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory" | ||
49 | tables: List of the tables | ||
50 | outputFolder: Place where the package folder structure should be put | ||
51 | className : Name of the class | ||
52 | pkg: Scala package the generated code should be places in | ||
53 | """.trim | ||
54 | ) | ||
55 | |||
56 | } | ||
57 | |||
58 | } | ||
59 | |||
60 | } | ||