diff options
author | Stewart Stewart <stewinsalot@gmail.com> | 2017-02-15 11:05:27 -0800 |
---|---|---|
committer | Stewart Stewart <stewinsalot@gmail.com> | 2017-02-15 11:42:37 -0800 |
commit | 836fc9dcf3e0620247fdd2d1c1eab74265260099 (patch) | |
tree | 548ee465fe165ba147cc3d35b72c25f70baad512 /src | |
parent | cdfbf7fc70eb69a34ae201ffe560db3beb712361 (diff) | |
download | slick-codegen-plugin-836fc9dcf3e0620247fdd2d1c1eab74265260099.tar.gz slick-codegen-plugin-836fc9dcf3e0620247fdd2d1c1eab74265260099.tar.bz2 slick-codegen-plugin-836fc9dcf3e0620247fdd2d1c1eab74265260099.zip |
use separate generator per schema file
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/NamespacedCodegen.scala | 122 |
1 files changed, 60 insertions, 62 deletions
diff --git a/src/main/scala/NamespacedCodegen.scala b/src/main/scala/NamespacedCodegen.scala index c9f613a..f17d595 100644 --- a/src/main/scala/NamespacedCodegen.scala +++ b/src/main/scala/NamespacedCodegen.scala @@ -40,16 +40,21 @@ object Generator { dc.db.close() } - val generator = new Generator(uri, - pkg, - dbModel, - outputPath, - manualForeignKeys, - schemaBaseClass, - idType, - schemaImports, - typeReplacements) - generator.code // Yes... Files are written as a side effect + parsedSchemasOpt.getOrElse(Map.empty).foreach { + case (schemaName, tables) => + val generator = new Generator(uri, + pkg, + dbModel, + schemaName, + outputPath, + manualForeignKeys, + schemaBaseClass, + idType, + schemaImports, + typeReplacements) + generator.code // Yes... Files are written as a side effect + } + parsedSchemasOpt .getOrElse(Map()) .keys @@ -70,7 +75,9 @@ class PackageNameGenerator(pkg: String, dbModel: Model) class ImportGenerator(dbModel: Model, schemaImports: List[String]) extends SourceCodeGenerator(dbModel) { - val baseImports: String = schemaImports.map("import " + _).mkString("\n") + "\n" + val baseImports: String = schemaImports + .map("import " + _) + .mkString("\n") + "\n" val hlistImports: String = """|import slick.collection.heterogeneous._ @@ -91,6 +98,7 @@ class ImportGenerator(dbModel: Model, schemaImports: List[String]) class Generator(uri: URI, pkg: String, dbModel: Model, + schemaName: String, outputPath: String, manualForeignKeys: Map[(String, String), (String, String)], schemaBaseClass: String, @@ -114,40 +122,36 @@ class Generator(uri: URI, override def code: String = { - val sortedSchemaTables: List[(String, Seq[TableDef])] = tables - .groupBy(t => t.model.name.schema.getOrElse("`public`")) - .toList - .sortBy(_._1) - - val schemata: String = sortedSchemaTables.map { - case (schemaName, tableDefs) => - val tableCode = tableDefs - .sortBy(_.model.name.table) - .map(_.code.mkString("\n")) - .mkString("\n\n") - - val ddlCode = - (if (ddlEnabled) { - "\n/** DDL for all tables. Call .create to execute. */" + - ( - if (tableDefs.length > 5) - "\nlazy val schema: profile.SchemaDescription = Array(" + tableDefs - .map(_.TableValue.name + ".schema") - .mkString(", ") + ").reduceLeft(_ ++ _)" - else if (tableDefs.nonEmpty) - "\nlazy val schema: profile.SchemaDescription = " + tableDefs - .map(_.TableValue.name + ".schema") - .mkString(" ++ ") - else - "\nlazy val schema: profile.SchemaDescription = profile.DDL(Nil, Nil)" - ) + - "\n\n" - } else "") - - val generatedSchema = s""" + val schemaTables = + tables.filter(_.model.name.schema.getOrElse("`public`") == schemaName) + + val tableCode = schemaTables + .sortBy(_.model.name.table) + .map(_.code.mkString("\n")) + .mkString("\n\n") + + val ddlCode = + (if (ddlEnabled) { + "\n/** DDL for all tables. Call .create to execute. */" + + ( + if (schemaTables.length > 5) + "\nlazy val schema: profile.SchemaDescription = Array(" + schemaTables + .map(_.TableValue.name + ".schema") + .mkString(", ") + ").reduceLeft(_ ++ _)" + else if (schemaTables.nonEmpty) + "\nlazy val schema: profile.SchemaDescription = " + schemaTables + .map(_.TableValue.name + ".schema") + .mkString(" ++ ") + else + "\nlazy val schema: profile.SchemaDescription = profile.DDL(Nil, Nil)" + ) + + "\n\n" + } else "") + + val generatedSchema = s""" |object ${schemaName} extends { | val profile = slick.backend.DatabaseConfig.forConfig[slick.driver.JdbcProfile]("${uri - .getFragment()}").driver + .getFragment()}").driver |} with $schemaBaseClass { | import profile.api._ | ${tableCode} @@ -155,24 +159,20 @@ class Generator(uri: URI, |} |// scalastyle:on""".stripMargin - writeStringToFile( - packageName + allImports + generatedSchema, - outputPath, - pkg, - s"${schemaName}.scala" - ) - - if (idType.isEmpty) { - writeStringToFile(packageName + defaultIdImplementation, - outputPath, - pkg, - "Id.scala") - } - - generatedSchema - }.mkString("\n\n") + writeStringToFile( // TODO: use writeToFile + packageName + allImports + generatedSchema, + outputPath, + pkg, + s"${schemaName}.scala") + + if (idType.isEmpty) { // TODO move out + writeStringToFile(packageName + defaultIdImplementation, + outputPath, + pkg, + "Id.scala") + } - allImports + schemata + allImports + generatedSchema } override def Table = new Table(_) { table => @@ -280,9 +280,7 @@ class Generator(uri: URI, s"""lazy val $fkName = foreignKey("$dbName", $fkColumns, $qualifiedName)(r => $pkColumns, onUpdate=$onUpdate, onDelete=$onDelete)""" } } - } - } object SchemaParser { |