From 4941337ca36a39bc9e546583a4087f69c37d0706 Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Sun, 21 Nov 2021 22:42:48 +0300 Subject: [PATCH 1/6] It compiles, but streamQuery is broken --- README.md | 2 +- build.sbt | 2 +- .../doobiequill/DoobieContextBase.scala | 104 +++++++++++++----- 3 files changed, 79 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 7153af0..5098822 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ For now docs are [here](docs/src/main/mdoc/docs/main.md), we'll have a site in t | This library | Doobie | Quill | | ------------ | --------- | ----- | -| 0.0.1 | 1.0.0-RC1 | 3.8.0 | +| 0.0.1 | 1.0.0-RC1 | 3.11.0 | ## Migrating from original integration diff --git a/build.sbt b/build.sbt index 425bca0..976e9f6 100644 --- a/build.sbt +++ b/build.sbt @@ -70,7 +70,7 @@ val root = project .settings( name := "doobie-quill", libraryDependencies ++= Seq( - "io.getquill" %% "quill-jdbc" % "3.8.0", + "io.getquill" %% "quill-jdbc" % "3.11.0", "org.tpolecat" %% "doobie-core" % "1.0.0-RC1", "org.tpolecat" %% "doobie-postgres" % "1.0.0-RC1" % Test, "org.scalameta" %% "munit" % "0.7.29" % Test, diff --git a/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala b/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala index 1487b5c..ab9fe90 100644 --- a/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala +++ b/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala @@ -9,7 +9,9 @@ import fs2.Stream import io.getquill.NamingStrategy import io.getquill.context.sql.idiom.SqlIdiom import io.getquill.context.StreamingContext +import io.getquill.context.ExecutionInfo import java.sql.Connection +import scala.annotation.nowarn import scala.util.Success import scala.util.Try import doobie.enumerated.AutoGeneratedKeys @@ -38,10 +40,15 @@ trait DoobieContextBase[Dialect <: SqlIdiom, Naming <: NamingStrategy] // to log.underlying below. private val log: ContextLogger = new ContextLogger("DoobieContext") + private def useConnection[A](f: Connection => PreparedStatementIO[A]): PreparedStatementIO[A] = + FPS.getConnection.flatMap(f) + private def prepareAndLog( sql: String, p: Prepare, - ): PreparedStatementIO[Unit] = FPS.raw(p).flatMap { case (params, _) => + )( + implicit connection: Connection + ): PreparedStatementIO[Unit] = FPS.raw(p(_, connection)).flatMap { case (params, _) => FPS.delay(log.logQuery(sql, params)) } @@ -49,45 +56,64 @@ trait DoobieContextBase[Dialect <: SqlIdiom, Naming <: NamingStrategy] sql: String, prepare: Prepare = identityPrepare, extractor: Extractor[A] = identityExtractor, + )( + info: ExecutionInfo, + dc: DatasourceContext, ): ConnectionIO[List[A]] = HC.prepareStatement(sql) { - prepareAndLog(sql, prepare) *> - HPS.executeQuery { - HRS.list(extractor) - } + useConnection { implicit connection => + prepareAndLog(sql, prepare) *> + HPS.executeQuery { + HRS.list(extractor) + } + } } override def executeQuerySingle[A]( sql: String, prepare: Prepare = identityPrepare, extractor: Extractor[A] = identityExtractor, + )( + info: ExecutionInfo, + dc: DatasourceContext, ): ConnectionIO[A] = HC.prepareStatement(sql) { - prepareAndLog(sql, prepare) *> - HPS.executeQuery { - HRS.getUnique(extractor) - } + useConnection { implicit connection => + prepareAndLog(sql, prepare) *> + HPS.executeQuery { + HRS.getUnique(extractor) + } + } } + @nowarn("msg=is never used") def streamQuery[A]( fetchSize: Option[Int], sql: String, prepare: Prepare = identityPrepare, extractor: Extractor[A] = identityExtractor, + )( + info: ExecutionInfo, + dc: DatasourceContext, ): Stream[ConnectionIO, A] = HC.stream( sql, - prepareAndLog(sql, prepare), + useConnection(implicit connection => prepareAndLog(sql, prepare)), fetchSize.getOrElse(DefaultChunkSize), - )(extractor) + )(extractorToRead(extractor)(null)) override def executeAction[A]( sql: String, prepare: Prepare = identityPrepare, + )( + info: ExecutionInfo, + dc: DatasourceContext, ): ConnectionIO[Long] = HC.prepareStatement(sql) { - prepareAndLog(sql, prepare) *> - HPS.executeUpdate.map(_.toLong) + useConnection { implicit connection => + prepareAndLog(sql, prepare) *> + HPS.executeUpdate.map(_.toLong) + } } private def prepareConnections[A](returningBehavior: ReturnAction) = @@ -105,44 +131,68 @@ trait DoobieContextBase[Dialect <: SqlIdiom, Naming <: NamingStrategy] prepare: Prepare = identityPrepare, extractor: Extractor[A], returningBehavior: ReturnAction, + )( + info: ExecutionInfo, + dc: DatasourceContext, ): ConnectionIO[A] = prepareConnections[A](returningBehavior)(sql) { - prepareAndLog(sql, prepare) *> - FPS.executeUpdate *> - HPS.getGeneratedKeys(HRS.getUnique(extractor)) + useConnection { implicit connection => + prepareAndLog(sql, prepare) *> + FPS.executeUpdate *> + HPS.getGeneratedKeys(HRS.getUnique(extractor)) + } } - private def prepareBatchAndLog(sql: String, p: Prepare): PreparedStatementIO[Unit] = - FPS.raw(p) flatMap { case (params, _) => + private def prepareBatchAndLog( + sql: String, + p: Prepare, + )( + implicit connection: Connection + ): PreparedStatementIO[Unit] = + FPS.raw(p(_, connection)) flatMap { case (params, _) => FPS.delay(log.logBatchItem(sql, params)) } override def executeBatchAction( groups: List[BatchGroup] + )( + info: ExecutionInfo, + dc: DatasourceContext, ): ConnectionIO[List[Long]] = groups.flatTraverse { case BatchGroup(sql, preps) => HC.prepareStatement(sql) { - FPS.delay(log.underlying.debug("Batch: {}", sql)) *> - preps.traverse(prepareBatchAndLog(sql, _) *> FPS.addBatch) *> - Nested(HPS.executeBatch).map(_.toLong).value + useConnection { implicit connection => + FPS.delay(log.underlying.debug("Batch: {}", sql)) *> + preps.traverse(prepareBatchAndLog(sql, _) *> FPS.addBatch) *> + Nested(HPS.executeBatch).map(_.toLong).value + } } } override def executeBatchActionReturning[A]( groups: List[BatchGroupReturning], extractor: Extractor[A], + )( + info: ExecutionInfo, + dc: DatasourceContext, ): ConnectionIO[List[A]] = groups.flatTraverse { case BatchGroupReturning(sql, returningBehavior, preps) => prepareConnections(returningBehavior)(sql) { - FPS.delay(log.underlying.debug("Batch: {}", sql)) *> - preps.traverse(prepareBatchAndLog(sql, _) *> FPS.addBatch) *> - HPS.executeBatch *> - HPS.getGeneratedKeys(HRS.list(extractor)) + + useConnection { implicit connection => + FPS.delay(log.underlying.debug("Batch: {}", sql)) *> + preps.traverse(prepareBatchAndLog(sql, _) *> FPS.addBatch) *> + HPS.executeBatch *> + HPS.getGeneratedKeys(HRS.list(extractor)) + } } } // Turn an extractor into a `Read` so we can use the existing resultset. - private implicit def extractorToRead[A](ex: Extractor[A]): Read[A] = - new Read[A](Nil, (rs, _) => ex(rs)) + private implicit def extractorToRead[A]( + ex: Extractor[A] + )( + implicit connection: Connection + ): Read[A] = new Read[A](Nil, (rs, _) => ex(rs, connection)) // Nothing to do here. override def close(): Unit = () From deb0d7d43aa02671da86cfb6ca50e7a77c68f209 Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Mon, 22 Nov 2021 00:44:09 +0300 Subject: [PATCH 2/6] streamQuery with connection --- .../doobiequill/DoobieContextBase.scala | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala b/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala index ab9fe90..3dc129f 100644 --- a/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala +++ b/src/main/scala/org/polyvariant/doobiequill/DoobieContextBase.scala @@ -96,11 +96,15 @@ trait DoobieContextBase[Dialect <: SqlIdiom, Naming <: NamingStrategy] info: ExecutionInfo, dc: DatasourceContext, ): Stream[ConnectionIO, A] = - HC.stream( - sql, - useConnection(implicit connection => prepareAndLog(sql, prepare)), - fetchSize.getOrElse(DefaultChunkSize), - )(extractorToRead(extractor)(null)) + for { + connection <- Stream.eval(FC.raw(identity)) + result <- + HC.stream( + sql, + prepareAndLog(sql, prepare)(connection), + fetchSize.getOrElse(DefaultChunkSize), + )(extractorToRead(extractor)(connection)) + } yield result override def executeAction[A]( sql: String, From d45f39d69a6f443a49b8fd407fa30197d6728134 Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Mon, 22 Nov 2021 00:51:40 +0300 Subject: [PATCH 3/6] README fixed --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5098822..32ddf71 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ For now docs are [here](docs/src/main/mdoc/docs/main.md), we'll have a site in t | This library | Doobie | Quill | | ------------ | --------- | ----- | -| 0.0.1 | 1.0.0-RC1 | 3.11.0 | +| 0.0.1 | 1.0.0-RC1 | 3.8.0 | +| 0.0.2 | 1.0.0-RC1 | 3.11.0 | ## Migrating from original integration From 69a10b2d5114d713ee7a37c64fc1b7a7f57258fc Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Mon, 22 Nov 2021 12:52:17 +0300 Subject: [PATCH 4/6] explicit ZIO dependency --- README.md | 8 ++++---- build.sbt | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 32ddf71..cf858f7 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ For now docs are [here](docs/src/main/mdoc/docs/main.md), we'll have a site in t ## Version compatibility -| This library | Doobie | Quill | -| ------------ | --------- | ----- | -| 0.0.1 | 1.0.0-RC1 | 3.8.0 | -| 0.0.2 | 1.0.0-RC1 | 3.11.0 | +| This library | Doobie | Quill | ZIO | +| ------------ | --------- | ------ | ----- | +| 0.0.1 | 1.0.0-RC1 | 3.8.0 | N/A | +| 0.0.2 | 1.0.0-RC1 | 3.11.0 | 1.0.12| ## Migrating from original integration diff --git a/build.sbt b/build.sbt index 976e9f6..9a76af1 100644 --- a/build.sbt +++ b/build.sbt @@ -71,6 +71,7 @@ val root = project name := "doobie-quill", libraryDependencies ++= Seq( "io.getquill" %% "quill-jdbc" % "3.11.0", + "dev.zio" %% "zio" % "1.0.12", "org.tpolecat" %% "doobie-core" % "1.0.0-RC1", "org.tpolecat" %% "doobie-postgres" % "1.0.0-RC1" % Test, "org.scalameta" %% "munit" % "0.7.29" % Test, From 29a49fb0d404c12904150fa683e939f23514313c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Mon, 22 Nov 2021 16:14:51 +0100 Subject: [PATCH 5/6] Remove direct zio dep, use fork in tests --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 9a76af1..bd26d66 100644 --- a/build.sbt +++ b/build.sbt @@ -71,9 +71,9 @@ val root = project name := "doobie-quill", libraryDependencies ++= Seq( "io.getquill" %% "quill-jdbc" % "3.11.0", - "dev.zio" %% "zio" % "1.0.12", "org.tpolecat" %% "doobie-core" % "1.0.0-RC1", "org.tpolecat" %% "doobie-postgres" % "1.0.0-RC1" % Test, "org.scalameta" %% "munit" % "0.7.29" % Test, ) ++ compilerPlugins, + Test / fork := true ) From 315e5c7a36dc1bb4a40867ef5e0b7cda1ca62f2e Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Mon, 22 Nov 2021 18:18:46 +0300 Subject: [PATCH 6/6] removed zio from readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cf858f7..7d845d7 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ For now docs are [here](docs/src/main/mdoc/docs/main.md), we'll have a site in t ## Version compatibility -| This library | Doobie | Quill | ZIO | -| ------------ | --------- | ------ | ----- | -| 0.0.1 | 1.0.0-RC1 | 3.8.0 | N/A | -| 0.0.2 | 1.0.0-RC1 | 3.11.0 | 1.0.12| +| This library | Doobie | Quill | +| ------------ | --------- | ------ | +| 0.0.1 | 1.0.0-RC1 | 3.8.0 | +| 0.0.2 | 1.0.0-RC1 | 3.11.0 | ## Migrating from original integration