typelevel / typelevel/doobie

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

Open
#765 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
2.2k
Forks
379
Avg merge
14m
Merged PRs (30d)
8

Description

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 ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/example/src/main/scala/example/Coproduct.scala and the xa.interpret, xa.exec, and xa.rawExec entry points shown in the report. Reproduce the q.unique example and determine the supported way to obtain ConnectionOp ~> M; document or test the resulting usage and its connection-lifetime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, scala
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.