forked from tpolecat/doobie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionio.scala
55 lines (43 loc) · 1.85 KB
/
connectionio.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2013-2020 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.syntax
import cats.data.EitherT
import cats.data.Kleisli
import cats.data.OptionT
import cats.effect.kernel.MonadCancelThrow
import cats.syntax.functor.*
import doobie.ConnectionIO
import doobie.HC
import doobie.util.transactor.Transactor
import scala.language.implicitConversions
class ConnectionIOOps[A](ma: ConnectionIO[A]) {
def transact[M[_]](xa: Transactor[M])(implicit M: MonadCancelThrow[M]): M[A] = xa.trans.apply(ma)
}
class OptionTConnectionIOOps[A](ma: OptionT[ConnectionIO, A]) {
def transact[M[_]: MonadCancelThrow](xa: Transactor[M]): OptionT[M, A] =
OptionT(
xa.trans.apply(ma.orElseF(HC.rollback.as(None)).value),
)
}
class EitherTConnectionIOOps[E, A](ma: EitherT[ConnectionIO, E, A]) {
def transact[M[_]: MonadCancelThrow](xa: Transactor[M]): EitherT[M, E, A] =
EitherT(
xa.trans.apply(ma.leftSemiflatMap(HC.rollback.as(_)).value),
)
}
class KleisliConnectionIOOps[A, B](ma: Kleisli[ConnectionIO, A, B]) {
def transact[M[_]: MonadCancelThrow](xa: Transactor[M]): Kleisli[M, A, B] =
ma.mapK(xa.trans)
}
trait ToConnectionIOOps {
implicit def toConnectionIOOps[A](ma: ConnectionIO[A]): ConnectionIOOps[A] =
new ConnectionIOOps(ma)
implicit def toOptionTConnectionIOOps[A](ma: OptionT[ConnectionIO, A]): OptionTConnectionIOOps[A] =
new OptionTConnectionIOOps(ma)
implicit def toEitherTConnectionIOOps[E, A](ma: EitherT[ConnectionIO, E, A]): EitherTConnectionIOOps[E, A] =
new EitherTConnectionIOOps(ma)
implicit def toKleisliConnectionIOOps[A, B](ma: Kleisli[ConnectionIO, A, B]): KleisliConnectionIOOps[A, B] =
new KleisliConnectionIOOps[A, B](ma)
}
object connectionio extends ToConnectionIOOps