1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
)
}
}
}
|