Permalink
Browse files

+slick #1333 add zipWith to DBIOAction

  • Loading branch information...
hepin1989 committed Oct 23, 2015
1 parent 0290716 commit 1f2df845fd036a2130503d72132719b3667acdd2
@@ -126,4 +126,22 @@ class ActionTest extends AsyncTest[RelationalTestDB] {
_ = result shouldBe Seq(2, 3, 1, 5, 4)
} yield ()
}
def testZipWith = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
for {
_ <- db.run {
ts.schema.create >>
(ts ++= Seq(2, 3, 1, 5, 4))
}
q1 = ts.sortBy(_.a).map(_.a).take(1)
result <- db.run(q1.result.head.zipWith(q1.result.head)({ case (a, b) => a + b }))
_ = result shouldBe 2
} yield ()
}
}
@@ -68,6 +68,14 @@ sealed trait DBIOAction[+R, +S <: NoStream, -E <: Effect] extends Dumpable {
(r(0).asInstanceOf[R], r(1).asInstanceOf[R2])
} (DBIO.sameThreadExecutionContext)
/** Run another action after this action, if it completed successfully, and zip the result
* of both actions with a function `f`, then create a new DBIOAction holding this result,
* If either of the two actions fails, the resulting action also fails. */
def zipWith[R2, E2 <: Effect,R3](a: DBIOAction[R2, NoStream, E2])(f:(R,R2) =>R3)(implicit executor: ExecutionContext): DBIOAction[R3, NoStream, E with E2] =
SequenceAction[Any, ArrayBuffer[Any], E with E2](Vector(this, a)).map { r =>
f(r(0).asInstanceOf[R], r(1).asInstanceOf[R2])
} (executor)
/** Run another action after this action, whether it succeeds or fails, and then return the
* result of the first action. If the first action fails, its failure is propagated, whether
* the second action fails or succeeds. If the first action succeeds, a failure of the second

0 comments on commit 1f2df84

Please sign in to comment.