Permalink
Browse files

fix #1424 - issue with union and limit

  • Loading branch information...
maquessime committed Jun 29, 2016
1 parent 0c191a5 commit 4fb8faf4cd8defda5c3e0dccc2581d614b907231
@@ -38,7 +38,7 @@ object SlickBuild extends Build {
val testDBs = Seq(
h2,
"org.xerial" % "sqlite-jdbc" % "3.8.7",
"org.apache.derby" % "derby" % "10.9.1.0",
"org.apache.derby" % "derby" % "10.10.1.1",
"org.hsqldb" % "hsqldb" % "2.2.8",
"postgresql" % "postgresql" % "9.1-901.jdbc4",
"mysql" % "mysql-connector-java" % "5.1.38",
@@ -1,6 +1,8 @@
package com.typesafe.slick.testkit.tests

import com.typesafe.slick.testkit.util.{RelationalTestDB, AsyncTest}
import com.typesafe.slick.testkit.util.{AsyncTest, RelationalTestDB}

import scala.collection.mutable

class UnionTest extends AsyncTest[RelationalTestDB] {
import tdb.profile.api._
@@ -24,9 +26,28 @@ class UnionTest extends AsyncTest[RelationalTestDB] {
}
lazy val employees = TableQuery[Employees]

def managersQuery = for(m <- managers filter { _.department === "IT" }) yield (m.id, m.name)

def employeesQuery = for(e <- employees filter { _.departmentIs("IT") }) yield (e.id, e.name)

val managersData = Seq(
(1, "Peter", "HR"),
(2, "Amy", "IT"),
(3, "Steve", "IT")
)

val employeesData = Seq(
(4, "Jennifer", 1),
(5, "Tom", 1),
(6, "Leonard", 2),
(7, "Ben", 2),
(8, "Greg", 3)
)

def testBasicUnions = {
val q1 = for(m <- managers filter { _.department === "IT" }) yield (m.id, m.name)
val q2 = for(e <- employees filter { _.departmentIs("IT") }) yield (e.id, e.name)

val q1 = managersQuery
val q2 = employeesQuery
val q3 = (q1 union q2).sortBy(_._2.asc)
val q4 = managers.map(_.id)
val q4b = q4 union q4
@@ -35,18 +56,8 @@ class UnionTest extends AsyncTest[RelationalTestDB] {

(for {
_ <- (managers.schema ++ employees.schema).create
_ <- managers ++= Seq(
(1, "Peter", "HR"),
(2, "Amy", "IT"),
(3, "Steve", "IT")
)
_ <- employees ++= Seq(
(4, "Jennifer", 1),
(5, "Tom", 1),
(6, "Leonard", 2),
(7, "Ben", 2),
(8, "Greg", 3)
)
_ <- managers ++= managersData
_ <- employees ++= employeesData
_ <- mark("q1", q1.result).map(r => r.toSet shouldBe Set((2,"Amy"), (3,"Steve")))
_ <- mark("q2", q2.result).map(r => r.toSet shouldBe Set((7,"Ben"), (8,"Greg"), (6,"Leonard")))
_ <- mark("q3", q3.result).map(_ shouldBe List((2,"Amy"), (7,"Ben"), (8,"Greg"), (6,"Leonard"), (3,"Steve")))
@@ -56,17 +67,26 @@ class UnionTest extends AsyncTest[RelationalTestDB] {
} yield ()) andFinally (managers.schema ++ employees.schema).drop
}

def testUnionWithLimit = {
val q1 = managersQuery
val q2 = employeesQuery.sortBy(_._2.asc)
val union = (q1 ++ q2.take(1)).sortBy(_._2.asc)

(for {
_ <- (managers.schema ++ employees.schema).create
_ <- managers ++= managersData
_ <- employees ++= employeesData
_ <- mark("union", union.result).map(_ shouldBe List((2,"Amy"),(7,"Ben"),(3,"Steve")))
} yield ()) andFinally (managers.schema ++ employees.schema).drop
}

def testUnionWithoutProjection = {
def f (s: String) = managers filter { _.name === s}
val q = f("Peter") union f("Amy")

seq(
managers.schema.create,
managers ++= Seq(
(1, "Peter", "HR"),
(2, "Amy", "IT"),
(3, "Steve", "IT")
),
managers ++= managersData,
q.result.map(r => r.toSet shouldBe Set((1, "Peter", "HR"), (2, "Amy", "IT")))
) andFinally managers.schema.drop
}
@@ -169,6 +169,14 @@ trait DerbyProfile extends JdbcProfile {
} else super.expr(c, skipParens)
case Library.NextValue(SequenceNode(name)) => b"(next value for `$name)"
case Library.CurrentValue(_*) => throw new SlickException("Derby does not support CURRVAL")
case Union(left, right, all) =>
b"\{"
buildFrom(left, None, true)
if(all) b"\nunion all " else b"\nunion "
b"\["
buildFrom(right, None, true)
b"\]"
b"\}"
case _ => super.expr(c, skipParens)
}
}
@@ -425,9 +425,13 @@ trait JdbcStatementBuilderComponent { self: JdbcProfile =>
b"\}"
case Union(left, right, all) =>
b"\{"
b"\["
buildFrom(left, None, true)
b"\]"
if(all) b"\nunion all " else b"\nunion "
b"\["
buildFrom(right, None, true)
b"\]"
b"\}"
case SimpleLiteral(w) => b += w
case s: SimpleExpression => s.toSQL(this)
@@ -177,6 +177,17 @@ trait SQLiteProfile extends JdbcProfile {
case RowNumber(_) => throw new SlickException("SQLite does not support row numbers")
// https://github.com/jOOQ/jOOQ/issues/1595
case Library.Repeat(n, times) => b"replace(substr(quote(zeroblob(($times + 1) / 2)), 3, $times), '0', $n)"
case Union(left, right, all) =>
b"\{ select * from "
b"\["
buildFrom(left, None, true)
b"\]"
if(all) b"\nunion all " else b"\nunion "
b"select * from "
b"\["
buildFrom(right, None, true)
b"\]"
b"\}"
case _ => super.expr(c, skipParens)
}
}

0 comments on commit 4fb8faf

Please sign in to comment.