-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tracking of latest SQL string and ExecutionInfo in ZIO
- Loading branch information
1 parent
73feec6
commit f3c136f
Showing
7 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
quill-jdbc-zio/src/main/scala/io/getquill/Diagnostic.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.getquill | ||
|
||
import zio.ZIO | ||
import zio.UIO | ||
import io.getquill.context.{ExecutionInfo, ZioQuillLog} | ||
|
||
def getLastExecutedQuery(): UIO[Option[String]] = | ||
ZioQuillLog.latestSqlQuery.get | ||
|
||
def getLastExecutionInfo(): UIO[Option[ExecutionInfo]] = | ||
ZioQuillLog.latestExecutionInfo.get |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 24 additions & 7 deletions
31
quill-jdbc-zio/src/main/scala/io/getquill/context/ZioQuillLog.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
package io.getquill.context | ||
|
||
import zio.FiberRef | ||
import zio.* | ||
|
||
object ZioQuillLog { | ||
val latestExecutionInfo: FiberRef[Option[ExecutionInfo]] = | ||
zio.Unsafe.unsafe { | ||
FiberRef.unsafe.make[Option[ExecutionInfo]](None) | ||
} | ||
|
||
val currentExecutionInfo: FiberRef[Option[ExecutionInfo]] = | ||
FiberRef.unsafe.make[Option[ExecutionInfo]](None)(Unsafe.unsafe) | ||
final class ExecutionInfoAware(val executionInfo: () => ExecutionInfo) { self => | ||
def apply[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = | ||
latestExecutionInfo.set(Some(executionInfo())) *> zio | ||
} | ||
|
||
def withExecutionInfo(info: => ExecutionInfo): ExecutionInfoAware = | ||
new ExecutionInfoAware(() => info) | ||
|
||
final class ExecutionInfoInformed(val executionInfo: () => ExecutionInfo) { self => | ||
|
||
val latestSqlQuery: FiberRef[Option[String]] = | ||
zio.Unsafe.unsafe { | ||
FiberRef.unsafe.make[Option[String]](None) | ||
} | ||
|
||
final class SqlQueryAware(val sqlQuery: () => String) { | ||
self => | ||
def apply[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = | ||
currentExecutionInfo.locallyWith(_ => Some(executionInfo()))(zio) | ||
latestSqlQuery.set(Some(sqlQuery())) *> zio | ||
} | ||
|
||
def withExecutionInfo(info: => ExecutionInfo): ExecutionInfoInformed = | ||
new ExecutionInfoInformed(() => info) | ||
def withSqlQuery(sqlQuery: => String): SqlQueryAware = | ||
new SqlQueryAware(() => sqlQuery) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
quill-jdbc-zio/src/test/scala/io/getquill/postgres/DiagnosticDataSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.getquill.postgres | ||
|
||
import io.getquill.* | ||
import io.getquill.ast.{Filter, ReturningGenerated} | ||
import io.getquill.context.AstSplicing | ||
import io.getquill.context.sql.ProductSpec | ||
|
||
class DiagnosticDataSpec extends ProductSpec with ZioSpec { | ||
|
||
val context = testContextSplicing | ||
import testContextSplicing.* | ||
|
||
override def beforeAll() = { | ||
super.beforeAll() | ||
testContextSplicing.run(quote(query[Product].delete)).runSyncUnsafe() | ||
() | ||
} | ||
|
||
"Diagnostic data" - { | ||
"Should expose last executed query" in { | ||
val (insertQuery, selectQuery, insertInfo, selectInfo) = (for { | ||
_ <- testContextSplicing.run { | ||
liftQuery(productEntries).foreach(e => productInsert(e)) | ||
} | ||
insertQuery <- getLastExecutedQuery() | ||
insertInfo <- getLastExecutionInfo() | ||
|
||
_ <- testContextSplicing.run(query[Product].filter(p => p.description == "Notebook")) | ||
selectQuery <- getLastExecutedQuery() | ||
selectInfo <- getLastExecutionInfo() | ||
|
||
} yield (insertQuery, selectQuery, insertInfo, selectInfo)).runSyncUnsafe() | ||
|
||
insertQuery.get mustBe "INSERT INTO Product (description,sku) VALUES (?, ?) RETURNING id" | ||
selectQuery.get mustBe "SELECT p.id, p.description, p.sku FROM Product p WHERE p.description = 'Notebook'" | ||
|
||
insertInfo.get.ast mustBe a[ReturningGenerated] | ||
selectInfo.get.ast mustBe a[Filter] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters