typelevel / doobie

Functional JDBC layer for Scala.
MIT License
2.17k stars 356 forks source link

LogHandler isn't used when query is converted to a Stream #772

Open matthughes opened 6 years ago

matthughes commented 6 years ago
object Repo {
  implicit val printlnLogHandler = LogHandler(println)
  val query: Query0[String] = sql"select name from person".toQuery[String]

  def asStream: fs2.Stream[ConnectionIO, String] = query.stream
  def asVector: ConnectionIO[Vector[String]] = query.to[Vector]
}

Repo.asVector.transact(transactor).unsafeRunSync() // produces log output
Repo.asVector.transact(transactor).compile.toVector.unsafeRunSync() // produces no log output

In Query, when we call to[Vector], we invoke the logging executeQuery:

    def to[F[_]](a: A)(implicit cbf: CanBuildFrom[Nothing, B, F[B]]): ConnectionIO[F[B]] =
      HC.prepareStatement(sql)(HPS.set(ai(a)) *> executeQuery(a, HRS.buildMap[F,O,B](ob)))

but the .stream path loses the logging executeQuery and uses HPS.executeQuery instead.

tpolecat commented 6 years ago

Yeah, this is a known (and documented) limitation and it's annoying. It may be worthwhile to go ahead and add a Log constructor to each Free algebra so we can plumb this in from the bottom. The issue is that stream construction is kind of smeared out so it's hard to add a logging layer on top.