aboutsummaryrefslogblamecommitdiff
path: root/src/main/scala/OutputHelpers.scala
blob: 2a3641e7788c1c355171667e46647326779e7cd1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                                          








                                                                  

                                                                    
         
                 

                                                              
                                            
                                 
                      

                                                                                                                                          
                                                              




                                






















































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