Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mentegy committed Dec 9, 2017
1 parent 0606e15 commit 956c27e
Show file tree
Hide file tree
Showing 25 changed files with 418 additions and 86 deletions.
28 changes: 5 additions & 23 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import scalariform.formatter.preferences._
import sbtrelease.ReleasePlugin
import scala.sys.process.Process

enablePlugins(TutPlugin)

lazy val scalaVersionProperty = Option(System.getProperty("scalaVersion"))

lazy val modules = Seq[sbt.ClasspathDep[sbt.ProjectReference]](
Expand All @@ -19,7 +17,6 @@ lazy val modules = Seq[sbt.ClasspathDep[sbt.ProjectReference]](
lazy val `quill` =
(project in file("."))
.settings(commonSettings)
.settings(`tut-settings`:_*)
.aggregate(modules.map(_.project): _*)
.dependsOn(modules: _*)

Expand Down Expand Up @@ -80,6 +77,11 @@ lazy val `quill-jdbc` =
)
.dependsOn(`quill-sql-jvm` % "compile->compile;test->test")

lazy val `quill-test` =
(project in file("quill-test"))
.settings(commonSettings: _*)
.dependsOn(`quill-sql-jvm`)

lazy val `quill-spark` =
(project in file("quill-spark"))
.settings(commonSettings: _*)
Expand Down Expand Up @@ -178,26 +180,6 @@ lazy val `quill-orientdb` =
)
.dependsOn(`quill-sql-jvm` % "compile->compile;test->test")

lazy val `tut-sources` = Seq(
"CASSANDRA.md",
"README.md"
)

lazy val `tut-settings` = Seq(
scalacOptions in Tut := Seq(),
tutSourceDirectory := baseDirectory.value / "target" / "tut",
tutNameFilter := `tut-sources`.map(_.replaceAll("""\.""", """\.""")).mkString("(", "|", ")").r,
sourceGenerators in Compile +=
Def.task {
`tut-sources`.foreach { name =>
val source = baseDirectory.value / name
val file = baseDirectory.value / "target" / "tut" / name
val str = IO.read(source).replace("```scala", "```tut")
IO.write(file, str)
}
Seq()
}.taskValue
)

lazy val mimaSettings = MimaPlugin.mimaDefaultSettings ++ Seq(
mimaPreviousArtifacts := {
Expand Down
2 changes: 0 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ resolvers += Resolver.url(
url("http://dl.bintray.com/content/tpolecat/sbt-plugin-releases"))(
Resolver.ivyStylePatterns)

addSbtPlugin("org.tpolecat" % "tut-plugin" % "0.6.1")

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1")

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.3")
Expand Down
16 changes: 15 additions & 1 deletion quill-core/src/main/scala/io/getquill/MirrorIdiom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MirrorIdiom extends Idiom {
case ast: QuotedReference => ast.ast.token
case ast: Lift => ast.token
case ast: Assignment => ast.token
case ast: Excluded => ast.property.token
}

implicit def ifTokenizer(implicit liftTokenizer: Tokenizer[Lift]): Tokenizer[If] = Tokenizer[If] {
Expand Down Expand Up @@ -184,10 +185,23 @@ class MirrorIdiom extends Idiom {
case Delete(query) => stmt"${query.token}.delete"
case Returning(query, alias, body) => stmt"${query.token}.returning((${alias.token}) => ${body.token})"
case Foreach(query, alias, body) => stmt"${query.token}.foreach((${alias.token}) => ${body.token})"
case Conflict(query, target, act) => stmt"${query.token}.${target.token}.${act.token}"
}

implicit def conflictTargetTokenizer(implicit liftTokenizer: Tokenizer[Lift]): Tokenizer[ConflictTarget] = Tokenizer[ConflictTarget] {
case NoTarget => stmt"onConflict"
case ConstraintTarget(n) => stmt"onConflict(${n.token})"
case ColumnsTarget(p) => stmt"onConflict(${p.token})"
}

implicit def conflictActionTokenizer(implicit liftTokenizer: Tokenizer[Lift]): Tokenizer[ConflictAction] = Tokenizer[ConflictAction] {
case DoNothingOnConflict => stmt"doNothing"
case DoUpdateOnConflict(a) => stmt"doUpdate(${a.map(a => a.copy()).token})"
}

implicit def assignmentTokenizer(implicit liftTokenizer: Tokenizer[Lift]): Tokenizer[Assignment] = Tokenizer[Assignment] {
case Assignment(ident, property, value) => stmt"${ident.token} => ${property.token} -> ${value.token}"
case Assignment(ident, Property(pi, name), value) if pi == ident => stmt"_.${name.token} -> ${value.token}"
case Assignment(ident, property, value) => stmt"${ident.token} => ${property.token} -> ${value.token}"
}

implicit def infixTokenizer(implicit liftTokenizer: Tokenizer[Lift]): Tokenizer[Infix] = Tokenizer[Infix] {
Expand Down
13 changes: 12 additions & 1 deletion quill-core/src/main/scala/io/getquill/ast/Ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ case class ListContains(ast: Ast, body: Ast) extends TraversableOperation
case class If(condition: Ast, `then`: Ast, `else`: Ast) extends Ast

case class Assignment(alias: Ident, property: Ast, value: Ast) extends Ast

case class Excluded(property: Ast) extends Ast
//************************************************************

sealed trait Operation extends Ast
Expand Down Expand Up @@ -126,6 +126,17 @@ case class Returning(action: Ast, alias: Ident, property: Ast) extends Action

case class Foreach(query: Ast, alias: Ident, body: Ast) extends Action

case class Conflict(insert: Ast, target: ConflictTarget, action: ConflictAction) extends Action

sealed trait ConflictTarget
case object NoTarget extends ConflictTarget
case class ConstraintTarget(name: String) extends ConflictTarget
case class ColumnsTarget(properties: List[Property]) extends ConflictTarget

sealed trait ConflictAction
case object DoNothingOnConflict extends ConflictAction
case class DoUpdateOnConflict(assignments: List[Assignment]) extends ConflictAction

//************************************************************

case class Dynamic(tree: Any) extends Ast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ trait StatefulTransformer[T] {
case e: Ident => (e, this)
case e: OptionOperation => apply(e)
case e: TraversableOperation => apply(e)
case e: Property => apply(e)

case Function(a, b) =>
val (bt, btt) = apply(b)
(Function(a, bt), btt)

case Property(a, b) =>
case Excluded(a) =>
val (at, att) = apply(a)
(Property(at, b), att)
(Excluded(at), att)

case Infix(a, b) =>
val (bt, btt) = apply(b)(_.apply)
Expand Down Expand Up @@ -168,6 +169,13 @@ trait StatefulTransformer[T] {
(Assignment(a, bt, ct), ctt)
}

def apply(e: Property): (Property, StatefulTransformer[T]) =
e match {
case Property(a, b) =>
val (at, att) = apply(a)
(Property(at, b), att)
}

def apply(e: Operation): (Operation, StatefulTransformer[T]) =
e match {
case UnaryOperation(o, a) =>
Expand Down Expand Up @@ -217,6 +225,27 @@ trait StatefulTransformer[T] {
val (at, att) = apply(a)
val (ct, ctt) = att.apply(c)
(Foreach(at, b, ct), ctt)
case Conflict(a, b, c) =>
val (at, att) = apply(a)
val (bt, btt) = att.apply(b)
val (ct, ctt) = btt.apply(c)
(Conflict(at, bt, ct), ctt)
}

def apply(e: ConflictTarget): (ConflictTarget, StatefulTransformer[T]) =
e match {
case NoTarget | ConstraintTarget(_) => (e, this)
case ColumnsTarget(a) =>
val (at, att) = apply(a)(_.apply)
(ColumnsTarget(at), att)
}

def apply(e: ConflictAction): (ConflictAction, StatefulTransformer[T]) =
e match {
case DoNothingOnConflict => (e, this)
case DoUpdateOnConflict(a) =>
val (at, att) = apply(a)(_.apply)
(DoUpdateOnConflict(at), att)
}

def apply[U, R](list: List[U])(f: StatefulTransformer[T] => U => (R, StatefulTransformer[T])) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ trait StatelessTransformer {
case e: Assignment => apply(e)
case Function(params, body) => Function(params, apply(body))
case e: Ident => e
case Property(a, name) => Property(apply(a), name)
case Excluded(a) => Excluded(apply(a))
case e: Property => apply(e)
case Infix(a, b) => Infix(a, b.map(apply))
case e: OptionOperation => apply(e)
case e: TraversableOperation => apply(e)
Expand Down Expand Up @@ -69,6 +70,11 @@ trait StatelessTransformer {
case Assignment(a, b, c) => Assignment(a, apply(b), apply(c))
}

def apply(e: Property): Property =
e match {
case Property(a, name) => Property(apply(a), name)
}

def apply(e: Operation): Operation =
e match {
case UnaryOperation(o, a) => UnaryOperation(o, apply(a))
Expand All @@ -94,6 +100,19 @@ trait StatelessTransformer {
case Delete(query) => Delete(apply(query))
case Returning(query, alias, property) => Returning(apply(query), alias, apply(property))
case Foreach(query, alias, body) => Foreach(apply(query), alias, apply(body))
case Conflict(query, target, action) => Conflict(apply(query), apply(target), apply(action))
}

def apply(e: ConflictTarget): ConflictTarget =
e match {
case NoTarget | ConstraintTarget(_) => e
case ColumnsTarget(props) => ColumnsTarget(props.map(apply))
}

def apply(e: ConflictAction): ConflictAction =
e match {
case DoNothingOnConflict => e
case DoUpdateOnConflict(assigns) => DoUpdateOnConflict(assigns.map(apply))
}

}
1 change: 1 addition & 0 deletions quill-core/src/main/scala/io/getquill/dsl/CoreDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.getquill.dsl

private[getquill] trait CoreDsl
extends InfixDsl
with UpsertDsl
with OrdDsl
with QueryDsl
with QuotationDsl
Expand Down
14 changes: 14 additions & 0 deletions quill-core/src/main/scala/io/getquill/dsl/UpsertDsl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.getquill.dsl

import io.getquill.quotation.NonQuotedException

private[dsl] trait UpsertDsl {
this: QueryDsl =>

def excluded[T]: T = NonQuotedException()

protected trait ConflictResolution[T] {
def doNothing: Action[T] = NonQuotedException()
def doUpdate(assign: (T => (Any, Any)), assigns: (T => (Any, Any))*): Action[T] = NonQuotedException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ object NormalizeReturning {

def apply(e: Action): Action = {
e match {
case Returning(Insert(query, assignments), alias, body) =>
Returning(Insert(query, filterReturnedColumn(assignments, body)), alias, body)
case Returning(Update(query, assignments), alias, body) =>
Returning(Update(query, filterReturnedColumn(assignments, body)), alias, body)
case e => e
case Returning(a: Action, alias, body) => Returning(apply(a, body), alias, body)
case _ => e
}
}

private def apply(e: Action, body: Ast): Action = e match {
case Insert(query, assignments) => Insert(query, filterReturnedColumn(assignments, body))
case Update(query, assignments) => Update(query, filterReturnedColumn(assignments, body))
case Conflict(a: Action, target, act) => Conflict(apply(a, body), target, act)
case _ => e
}

private def filterReturnedColumn(assignments: List[Assignment], column: Ast): List[Assignment] =
assignments.flatMap(filterReturnedColumn(_, column))

Expand Down
12 changes: 12 additions & 0 deletions quill-core/src/main/scala/io/getquill/norm/RenameProperties.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.getquill.norm

import java.util.concurrent.atomic.AtomicInteger

import io.getquill.ast._

object RenameProperties extends StatelessTransformer {
Expand Down Expand Up @@ -29,17 +31,27 @@ object RenameProperties extends StatelessTransformer {
val bodyr = BetaReduction(body, replace: _*)
(Returning(action, alias, bodyr), schema)
}
case Conflict(a: Action, target, act) =>
applySchema(a) match {
case (action, schema) =>
// TODO target & act?
(Conflict(action, target, act), schema)
}
case q => (q, Tuple(List.empty))
}

private val counter = new AtomicInteger()
private def applySchema(q: Query, a: List[Assignment], f: (Query, List[Assignment]) => Action): (Action, Ast) =
applySchema(q) match {
case (q, schema) =>
val ar = a.map {
case Assignment(alias, prop, value) =>
val i = counter.incrementAndGet()
println(s"RENAME PROPS $i BEFORE: $alias, $prop, $value")
val replace = replacements(alias, schema)
val propr = BetaReduction(prop, replace: _*)
val valuer = BetaReduction(value, replace: _*)
println(s"RENAME PROPS $i AFTER: $alias, $propr, $valuer")
Assignment(alias, propr, valuer)
}
(f(q, ar), schema)
Expand Down
23 changes: 22 additions & 1 deletion quill-core/src/main/scala/io/getquill/quotation/Liftables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ trait Liftables {
case ast: Assignment => assignmentLiftable(ast)
case ast: OptionOperation => optionOperationLiftable(ast)
case ast: TraversableOperation => traversableOperationLiftable(ast)
case ast: Property => propertyLiftable(ast)
case ast: Excluded => excludedLiftable(ast)
case Val(name, body) => q"$pack.Val($name, $body)"
case Block(statements) => q"$pack.Block($statements)"
case Property(a, b) => q"$pack.Property($a, $b)"
case Function(a, b) => q"$pack.Function($a, $b)"
case FunctionApply(a, b) => q"$pack.FunctionApply($a, $b)"
case BinaryOperation(a, b, c) => q"$pack.BinaryOperation($a, $b, $c)"
Expand Down Expand Up @@ -113,6 +114,14 @@ trait Liftables {
case PropertyAlias(a, b) => q"$pack.PropertyAlias($a, $b)"
}

implicit val propertyLiftable: Liftable[Property] = Liftable[Property] {
case Property(a, b) => q"$pack.Property($a, $b)"
}

implicit val excludedLiftable: Liftable[Excluded] = Liftable[Excluded] {
case Excluded(a) => q"$pack.Excluded($a)"
}

implicit val orderingLiftable: Liftable[Ordering] = Liftable[Ordering] {
case TupleOrdering(elems) => q"$pack.TupleOrdering($elems)"
case Asc => q"$pack.Asc"
Expand All @@ -136,6 +145,18 @@ trait Liftables {
case Delete(a) => q"$pack.Delete($a)"
case Returning(a, b, c) => q"$pack.Returning($a, $b, $c)"
case Foreach(a, b, c) => q"$pack.Foreach($a, $b, $c)"
case Conflict(a, b, c) => q"$pack.Conflict($a, $b, $c)"
}

implicit val conflictTargetLiftable: Liftable[ConflictTarget] = Liftable[ConflictTarget] {
case NoTarget => q"$pack.NoTarget"
case ConstraintTarget(a) => q"$pack.ConstraintTarget.apply($a)"
case ColumnsTarget(a) => q"$pack.ColumnsTarget.apply($a)"
}

implicit val conflictActionLiftable: Liftable[ConflictAction] = Liftable[ConflictAction] {
case DoNothingOnConflict => q"$pack.DoNothingOnConflict"
case DoUpdateOnConflict(a) => q"$pack.DoUpdateOnConflict.apply($a)"
}

implicit val assignmentLiftable: Liftable[Assignment] = Liftable[Assignment] {
Expand Down
Loading

0 comments on commit 956c27e

Please sign in to comment.