Skip to content

Commit

Permalink
Add schema definition
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Sep 8, 2016
1 parent 7aa9cd9 commit bdec951
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"com.github.jsqlparser" % "jsqlparser" % "0.9.6",
"org.scalamacros" %% "resetallattrs" % "1.0.0",
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.7.2",
"com.github.pathikrit" %% "better-files" % "2.15.0",
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
"org.scala-lang" % "scala-compiler" % scalaVersion.value % "provided",
"org.scalatest" %% "scalatest" % "2.2.1" % "test"
Expand Down
37 changes: 37 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"tables":[
{
"name": "USER",
"columns": [
{
"name": "USER_ID"
},
{
"name": "USER_NAME"
}
]
},
{
"name": "COMPANY",
"columns": [
{
"name": "COMPANY_ID"
},
{
"name": "COMPANY_NAME"
}
]
},
{
"name": "DEPT",
"columns": [
{
"name": "DEPT_ID"
},
{
"name": "DEPT_NAME"
}
]
}
]
}
19 changes: 19 additions & 0 deletions src/main/scala/com/github/takezoe/scala/jdbc/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.takezoe.scala

import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.github.takezoe.scala.jdbc.SqlTemplate
import net.sf.jsqlparser.JSQLParserException
import net.sf.jsqlparser.parser.CCJSqlParserUtil
Expand All @@ -9,6 +11,10 @@ import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import com.github.takezoe.scala.jdbc.validation._

import scala.reflect.ClassTag
import better.files._
import java.io.{File => JFile}

package object jdbc {

/**
Expand All @@ -34,6 +40,11 @@ package object jdbc {

object Macros {

private val mapper = new ObjectMapper()
mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.registerModule(DefaultScalaModule)

def sqlMacro(c: Context)(sql: c.Expr[String]): c.Expr[com.github.takezoe.scala.jdbc.SqlTemplate] = {
import c.universe._
sql.tree match {
Expand All @@ -58,6 +69,14 @@ object Macros {
}

private def validateSql(sql: String, c: Context): Unit = {
val file = File("schema.json")
val schema = if(file.exists){
val json = file.contentAsString
val schema = mapper.readValue(json, classOf[SchemaDef])
schema.tables.map { t => t.name -> t }.toMap
} else {
Map.empty
}
try {
val parse = CCJSqlParserUtil.parse(sql)
parse.accept(new StatementVisitorAdapter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.takezoe.scala.jdbc.validation

case class SchemaDef(tables: Seq[TableDef])

case class TableDef(name:String, columns: Seq[ColumnDef])

case class ColumnDef(name: String)

0 comments on commit bdec951

Please sign in to comment.