Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support contains, exists, forall for optional embedded case classes with optional fields. #838

Merged
merged 1 commit into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ object FlattenOptionOperation extends StatelessTransformer {
apply(BetaReduction(body, alias -> ast))
case OptionForall(ast, alias, body) =>
val isEmpty = BinaryOperation(ast, EqualityOperator.`==`, NullValue)
val exists = BetaReduction(body, alias -> ast)
val exists = apply(BetaReduction(body, alias -> ast))
BinaryOperation(isEmpty, BooleanOperator.`||`, exists)
case OptionExists(ast, alias, body) =>
BetaReduction(body, alias -> ast)
apply(BetaReduction(body, alias -> ast))
case OptionContains(ast, body) =>
BinaryOperation(ast, EqualityOperator.`==`, body)
case other =>
super.apply(ast)
super.apply(other)
}
}
16 changes: 11 additions & 5 deletions quill-core/src/main/scala/io/getquill/quotation/Parsing.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.getquill.quotation


import scala.reflect.ClassTag
import io.getquill.ast._
import io.getquill.Embedded
import io.getquill.norm.BetaReduction
import io.getquill.util.Messages.RichContext
import io.getquill.util.Interleave
Expand Down Expand Up @@ -282,13 +284,17 @@ trait Parsing {
private def ident(x: TermName): Ident = identClean(Ident(x.decodedName.toString))

val optionOperationParser: Parser[OptionOperation] = Parser[OptionOperation] {
case q"$o.map[$t]({($alias) => $body})" if (is[Option[Any]](o)) =>
case q"$o.map[$t]({($alias) => $body})" if is[Option[Any]](o) =>
OptionMap(astParser(o), identParser(alias), astParser(body))
case q"$o.forall({($alias) => $body})" if (is[Option[Any]](o)) =>
OptionForall(astParser(o), identParser(alias), astParser(body))
case q"$o.exists({($alias) => $body})" if (is[Option[Any]](o)) =>
case q"$o.forall({($alias) => $body})" if is[Option[Any]](o) =>
if (is[Option[Embedded]](o)) {
c.fail("Please use Option.exists() instead of Option.forall() with embedded case classes.")
} else {
OptionForall(astParser(o), identParser(alias), astParser(body))
}
case q"$o.exists({($alias) => $body})" if is[Option[Any]](o) =>
OptionExists(astParser(o), identParser(alias), astParser(body))
case q"$o.contains[$t]($body)" if (is[Option[Any]](o)) =>
case q"$o.contains[$t]($body)" if is[Option[Any]](o) =>
OptionContains(astParser(o), astParser(body))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,17 +698,33 @@ class QuotationSpec extends Spec {
}
quote(unquote(q)).ast.body mustEqual OptionMap(Ident("o"), Ident("v"), Ident("v"))
}
"forall" in {
val q = quote {
(o: Option[Boolean]) => o.forall(v => v)
"forall" - {
"simple" in {
val q = quote {
(o: Option[Boolean]) => o.forall(v => v)
}
quote(unquote(q)).ast.body mustEqual OptionForall(Ident("o"), Ident("v"), Ident("v"))
}
"embedded" in {
case class EmbeddedEntity(id: Int) extends Embedded
"quote((o: Option[EmbeddedEntity]) => o.forall(v => v.id == 1))" mustNot compile
}
quote(unquote(q)).ast.body mustEqual OptionForall(Ident("o"), Ident("v"), Ident("v"))
}
"exists" in {
val q = quote {
(o: Option[Boolean]) => o.exists(v => v)
"exists" - {
"simple" in {
val q = quote {
(o: Option[Boolean]) => o.exists(v => v)
}
quote(unquote(q)).ast.body mustEqual OptionExists(Ident("o"), Ident("v"), Ident("v"))
}
"embedded" in {
case class EmbeddedEntity(id: Int) extends Embedded
val q = quote {
(o: Option[EmbeddedEntity]) => o.exists(v => v.id == 1)
}
quote(unquote(q)).ast.body mustEqual OptionExists(Ident("o"), Ident("v"),
BinaryOperation(Property(Ident("v"), "id"), EqualityOperator.`==`, Constant(1)))
}
quote(unquote(q)).ast.body mustEqual OptionExists(Ident("o"), Ident("v"), Ident("v"))
}
"contains" in {
val q = quote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SqlIdiomSpec extends Spec {
val q = quote {
for {
a <- qr1
b <- qr2 if (a.s == b.s)
b <- qr2 if a.s == b.s
} yield {
a.s
}
Expand All @@ -40,30 +40,30 @@ class SqlIdiomSpec extends Spec {
"SELECT x.s, x.i, x.l, x.o FROM (SELECT DISTINCT x.s, x.i, x.l, x.o FROM TestEntity x) x"
}

"distinct single" in {
"single" in {
val q = quote {
qr1.map(i => i.i).distinct
}
testContext.run(q).string mustEqual
"SELECT DISTINCT i.i FROM TestEntity i"
}

"distinct tuple" in {
"tuple" in {
val q = quote {
qr1.map(i => (i.i, i.l)).distinct
}
testContext.run(q).string mustEqual
"SELECT DISTINCT i.i, i.l FROM TestEntity i"
}

"distinct nesting" in {
"nesting" in {
val q = quote {
qr1.map(i => i.i).distinct.map(x => x + 1)
}
testContext.run(q).string mustEqual
"SELECT x + 1 FROM (SELECT DISTINCT i.i FROM TestEntity i) x"
}
"distinct followed by aggregation" in {
"followed by aggregation" in {
val q = quote {
qr1.map(i => i.i).distinct.size
}
Expand Down Expand Up @@ -164,7 +164,7 @@ class SqlIdiomSpec extends Spec {
(a, b) <- qr1.groupBy(t => t.i).map {
case (i, entities) => (i, entities.size)
}
c <- qr2 if (c.i == a)
c <- qr2 if c.i == a
} yield {
(a, b, c)
}
Expand All @@ -174,10 +174,10 @@ class SqlIdiomSpec extends Spec {
}
"limited" in {
val q = quote {
(qr1.groupBy(t => t.i).map {
qr1.groupBy(t => t.i).map {
case (i, e) =>
(i, e.map(_.l).min)
}).take(10)
}.take(10)
}

testContext.run(q).string mustEqual
Expand Down Expand Up @@ -383,6 +383,28 @@ class SqlIdiomSpec extends Spec {
testContext.run(q).string mustEqual
"SELECT (SELECT COUNT(t.i) FROM TestEntity t) = 1"
}
"contains" in {
val q = quote {
qr1.filter(t => qr2.map(p => p.i).contains(t.i))
}
testContext.run(q).string mustEqual
"SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE t.i IN (SELECT p.i FROM TestEntity2 p)"
}

"set" - {
"non-empty" in {
val q = quote {
qr1.filter(t => liftQuery(Set(1, 2, 3)).contains(t.i))
}
testContext.run(q).string mustEqual "SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE t.i IN (?, ?, ?)"
}
"empty" in {
val q = quote {
qr1.filter(t => liftQuery(Set.empty[Int]).contains(t.i))
}
testContext.run(q).string mustEqual "SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE FALSE"
}
}
}
"operations" - {
"unary operation" - {
Expand Down Expand Up @@ -580,43 +602,73 @@ class SqlIdiomSpec extends Spec {
testContext.run(q).string mustEqual
"SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE (t.i % t.l) = 0"
}
}
"option operation" - {
"contains" in {
val q = quote {
qr1.filter(t => t.o.contains(1))
}
testContext.run(q).string mustEqual
"SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE t.o = 1"
}
"exists" in {
val q = quote {
qr1.filter(t => t.o.exists(op => op != 1))
}
testContext.run(q).string mustEqual
"SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE t.o <> 1"
}
"forall" in {
val q = quote {
qr1.filter(t => t.i != 1 && t.o.forall(op => op == 1))
}
testContext.run(q).string mustEqual
"SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE (t.i <> 1) AND ((t.o IS NULL) OR (t.o = 1))"
}
"contains" - {
"query" in {
"embedded" - {
case class TestEntity(optionalEmbedded: Option[EmbeddedEntity])
case class EmbeddedEntity(value: Int) extends Embedded

"exists" in {
val q = quote {
qr1.filter(t => qr2.map(p => p.i).contains(t.i))
query[TestEntity].filter(t => t.optionalEmbedded.exists(_.value == 1))
}

testContext.run(q).string mustEqual
"SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE t.i IN (SELECT p.i FROM TestEntity2 p)"
"SELECT t.value FROM TestEntity t WHERE t.value = 1"
}
"forall" in {
"quote(query[TestEntity].filter(t => t.optionalEmbedded.forall(_.value == 1)))" mustNot compile
}
"option" in {
}
"nested" - {
case class TestEntity(optionalEmbedded: Option[EmbeddedEntity])
case class EmbeddedEntity(optionalValue: Option[Int]) extends Embedded

"contains" in {
val q = quote {
qr1.filter(t => t.o.contains(1))
query[TestEntity].filter(t => t.optionalEmbedded.exists(_.optionalValue.contains(1)))
}

testContext.run(q).string mustEqual
"SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE t.o = 1"
"SELECT t.optionalValue FROM TestEntity t WHERE t.optionalValue = 1"
}
"set" - {
"non-empty" in {
val q = quote {
qr1.filter(t => liftQuery(Set(1, 2, 3)).contains(t.i))
}
testContext.run(q).string mustEqual "SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE t.i IN (?, ?, ?)"
}
"empty" in {
val q = quote {
qr1.filter(t => liftQuery(Set.empty[Int]).contains(t.i))
}
testContext.run(q).string mustEqual "SELECT t.s, t.i, t.l, t.o FROM TestEntity t WHERE FALSE"
"exists" in {
val q = quote {
query[TestEntity].filter(t => t.optionalEmbedded.exists(_.optionalValue.exists(_ == 1)))
}

testContext.run(q).string mustEqual
"SELECT t.optionalValue FROM TestEntity t WHERE t.optionalValue = 1"
}
"forall" in {
val q = quote {
query[TestEntity].filter(t => t.optionalEmbedded.exists(_.optionalValue.forall(_ == 1)))
}

testContext.run(q).string mustEqual
"SELECT t.optionalValue FROM TestEntity t WHERE (t.optionalValue IS NULL) OR (t.optionalValue = 1)"
}
}
}
}
Expand Down