From 102aa93a791e9941d15d78caead93e8bf8d58bdc Mon Sep 17 00:00:00 2001 From: Stewart Stewart Date: Fri, 24 Feb 2017 08:18:21 -0500 Subject: move schema parser to separate file --- src/main/scala/NamespacedCodegen.scala | 56 ------------------------------- src/main/scala/SchemaParser.scala | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 56 deletions(-) create mode 100644 src/main/scala/SchemaParser.scala (limited to 'src') 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) + } +} -- cgit v1.2.3