aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/OutputHelpers.scala
blob: 2a3641e7788c1c355171667e46647326779e7cd1 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
trait OOutputHelpers extends slick.codegen.OutputHelpers {

  def imports: String

  def headerComment: String = ""

  override def packageCode(profile: String,
                           pkg: String,
                           container: String,
                           parentType: Option[String]): String = {
    s"""|${headerComment.trim().lines.map("// " + _).mkString("\n")}
        |package $pkg
        |
        |$imports
        |
        |/** Stand-alone Slick data model for immediate use */
        |package object $container extends {
        |  val profile = $profile
        |} with Tables
        |
        |/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */
        |trait Tables${parentType.fold("")(" extends " + _)} {
        |  import profile.api._
        |  ${indent(code)}
        |}""".stripMargin.trim()
  }
}

import slick.codegen.{SourceCodeGenerator, OutputHelpers}

trait TableFileGenerator { self: SourceCodeGenerator =>
  def writeTablesToFile(folder:String, pkg: String, fileName: String): Unit
}

trait RowFileGenerator { self: SourceCodeGenerator =>
  def writeRowsToFile(folder:String, pkg: String, fileName: String): Unit
}

// Dirty work to hide OutputHelpers
trait TableOutputHelpers extends TableFileGenerator with OutputHelpers { self: SourceCodeGenerator =>

  def packageTableCode: String =
    """|${headerComment.trim().lines.map("// " + _).mkString("\n")}
       |package $pkg
       |package $schemaName
       |
       |$imports
       |
       |/** Stand-alone Slick data model for immediate use */
       |// TODO: change this to `object tables`
       |package object $schemaName extends {
       |  val profile = $profile
       |} with Tables
       |
       |/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */
       |trait Tables${parentType.fold("")(" extends " + _)} {
       |  import profile.api._
       |  ${indent(code)}       |
       |""".stripMargin.trim()

  def writeTablesToFile(folder:String, pkg: String, fileName: String): Unit = {
    writeStringToFile(content = packageTableCode, folder = folder, pkg = pkg, fileName = fileName)
  }
}

trait RowOutputHelpers extends RowFileGenerator with OutputHelpers { self: SourceCodeGenerator =>

  def packageRowCode: String =
    """|${headerComment.trim().lines.map("// " + _).mkString("\n")}
       |/** Definitions for table rows types of database schema $schemaName */
       |package $pkg
       |package $schemaName
       |
       |$imports
       |
       |$code
       |""".stripMargin.trim()

  def writeRowsToFile(folder:String, pkg: String, fileName: String): Unit = {
    writeStringToFile(content = packageRowCode, folder = folder, pkg = pkg, fileName = fileName)
  }
}