aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStewart Stewart <stewinsalot@gmail.com>2017-02-24 08:18:21 -0500
committerStewart Stewart <stewinsalot@gmail.com>2017-02-24 08:18:21 -0500
commit102aa93a791e9941d15d78caead93e8bf8d58bdc (patch)
treed127533bc8ddb72ea4adba198219175b6251c924
parent73ff2c2e532436d691d7ba169be0482e3dae5851 (diff)
downloadslick-codegen-plugin-102aa93a791e9941d15d78caead93e8bf8d58bdc.tar.gz
slick-codegen-plugin-102aa93a791e9941d15d78caead93e8bf8d58bdc.tar.bz2
slick-codegen-plugin-102aa93a791e9941d15d78caead93e8bf8d58bdc.zip
move schema parser to separate file
-rw-r--r--src/main/scala/NamespacedCodegen.scala56
-rw-r--r--src/main/scala/SchemaParser.scala60
2 files changed, 60 insertions, 56 deletions
diff --git a/src/main/scala/NamespacedCodegen.scala b/src/main/scala/NamespacedCodegen.scala
index 3f8034e..364dcab 100644
--- a/src/main/scala/NamespacedCodegen.scala
+++ b/src/main/scala/NamespacedCodegen.scala
@@ -6,9 +6,7 @@ import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import slick.backend.DatabaseConfig
import slick.codegen.{SourceCodeGenerator, StringGeneratorHelpers}
-import slick.dbio.DBIO
import slick.driver.JdbcProfile
-import slick.jdbc.meta.MTable
import slick.{model => sModel}
import slick.model.{Column, Model, Table, QualifiedName}
@@ -172,57 +170,3 @@ class TableSourceCodeGenerator(
}
}
}
-
-object SchemaParser {
- def references(dbModel: Model,
- tcMappings: Map[(String, String), (String, String)])
- : Map[(String, String), (Table, Column)] = {
- def getTableColumn(tc: (String, String)): (Table, Column) = {
- val (tableName, columnName) = tc
- val table = dbModel.tables
- .find(_.name.asString == tableName)
- .getOrElse(throw new RuntimeException("No table " + tableName))
- val column = table.columns
- .find(_.name == columnName)
- .getOrElse(throw new RuntimeException(
- "No column " + columnName + " in table " + tableName))
- (table, column)
- }
-
- tcMappings.map {
- case (from, to) => ({ getTableColumn(from); from }, getTableColumn(to))
- }
- }
-
- def parse(schemaTableNames: List[String]): Map[String, List[String]] =
- schemaTableNames
- .map(_.split('.'))
- .groupBy(_.head)
- .mapValues(_.flatMap(_.tail))
-
- def createModel(
- jdbcProfile: JdbcProfile,
- mappedSchemasOpt: Option[Map[String, List[String]]]): DBIO[Model] = {
- import slick.jdbc.meta.MQName
-
- val filteredTables = mappedSchemasOpt.map { mappedSchemas =>
- MTable.getTables.map { (tables: Vector[MTable]) =>
- mappedSchemas.flatMap {
- case (schemaName, tableNames) =>
- tableNames.map(
- tableName =>
- tables
- .find(table =>
- table.name match {
- case MQName(_, Some(`schemaName`), `tableName`) => true
- case _ => false
- })
- .getOrElse(throw new IllegalArgumentException(
- s"$schemaName.$tableName does not exist in the connected database.")))
- }.toList
- }
- }
-
- jdbcProfile.createModel(filteredTables)
- }
-}
diff --git a/src/main/scala/SchemaParser.scala b/src/main/scala/SchemaParser.scala
new file mode 100644
index 0000000..1186f11
--- /dev/null
+++ b/src/main/scala/SchemaParser.scala
@@ -0,0 +1,60 @@
+import scala.concurrent.ExecutionContext.Implicits.global
+
+import slick.dbio.DBIO
+import slick.driver.JdbcProfile
+import slick.jdbc.meta.MTable
+import slick.{model => m}
+
+object SchemaParser {
+ def references(dbModel: m.Model,
+ tcMappings: Map[(String, String), (String, String)])
+ : Map[(String, String), (m.Table, m.Column)] = {
+ def getTableColumn(tc: (String, String)): (m.Table, m.Column) = {
+ val (tableName, columnName) = tc
+ val table = dbModel.tables
+ .find(_.name.asString == tableName)
+ .getOrElse(throw new RuntimeException("No table " + tableName))
+ val column = table.columns
+ .find(_.name == columnName)
+ .getOrElse(throw new RuntimeException(
+ "No column " + columnName + " in table " + tableName))
+ (table, column)
+ }
+
+ tcMappings.map {
+ case (from, to) => ({ getTableColumn(from); from }, getTableColumn(to))
+ }
+ }
+
+ def parse(schemaTableNames: List[String]): Map[String, List[String]] =
+ schemaTableNames
+ .map(_.split('.'))
+ .groupBy(_.head)
+ .mapValues(_.flatMap(_.tail))
+
+ def createModel(
+ jdbcProfile: JdbcProfile,
+ mappedSchemasOpt: Option[Map[String, List[String]]]): DBIO[m.Model] = {
+ import slick.jdbc.meta.MQName
+
+ val filteredTables = mappedSchemasOpt.map { mappedSchemas =>
+ MTable.getTables.map { (tables: Vector[MTable]) =>
+ mappedSchemas.flatMap {
+ case (schemaName, tableNames) =>
+ tableNames.map(
+ tableName =>
+ tables
+ .find(table =>
+ table.name match {
+ case MQName(_, Some(`schemaName`), `tableName`) => true
+ case _ => false
+ })
+ .getOrElse(throw new IllegalArgumentException(
+ s"$schemaName.$tableName does not exist in the connected database.")))
+ }.toList
+ }
+ }
+
+ jdbcProfile.createModel(filteredTables)
+ }
+}