doobie icon indicating copy to clipboard operation
doobie copied to clipboard

What is the proper way to create nat `ConnectionOp ~> M`

Open jcouyang opened this issue 7 years ago • 0 comments

I was trying to inject ConnectionOps to my own free program, so I need a ConnectionOp ~> M as one of the program's interpreter

So I compose nat interpret: ConnectionOp ~> Kleisli[M, Connection, ?] and exec: Kleisli[M, Connection, ?] ~> M

but it doesn't work

import doobie.implicits._
import cats.effect.IO
import doobie._
val xa = Transactor.fromDriverManager[IO]("org.postgresql.Driver","jdbc:postgresql:postgres", "postgres", "")
val q = sql"select now()".query[String]

q.unique.foldMap(xa.interpret andThen xa.exec).unsafeRunSync

will exception

org.postgresql.util.PSQLException: This connection has been closed.
  at org.postgresql.jdbc.PgConnection.checkClosed(PgConnection.java:767)
  at org.postgresql.jdbc.PgConnection.getAutoCommit(PgConnection.java:728)
  at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:405)
  at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
  at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
  at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:118)
  at doobie.free.KleisliInterpreter$PreparedStatementInterpreter.$anonfun$executeQuery$2(kleisliinterpreter.scala:770)
  at doobie.free.KleisliInterpreter$PreparedStatementInterpreter$$Lambda$6646/1537134880.apply(Unknown Source)
  at doobie.free.KleisliInterpreter.$anonfun$primitive$2(kleisliinterpreter.scala:99)
  at doobie.free.KleisliInterpreter$$Lambda$6633/460248959.apply(Unknown Source)
  at cats.effect.internals.IORunLoop$.step(IORunLoop.scala:167)
  at cats.effect.IO.unsafeRunTimed(IO.scala:308)
  at cats.effect.IO.unsafeRunSync(IO.scala:243)

while if I change exec to rawExec it works

q.unique.foldMap(xa.interpret andThen xa.rawExec).unsafeRunSync
// res2: String = 2018-08-22 16:06:04.245774+08

but I don't feel it's the right way to do so either

i don't want to liftK my program to Kleisli like this, is there any proper way to create a nat ConnectionOp ~> M ?

jcouyang avatar Aug 22 '18 08:08 jcouyang