Skip to content

Commit 1058ba3

Browse files
committed
Query' altered execution: Allow changing result type
1 parent d685883 commit 1058ba3

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

modules/core/src/main/scala/doobie/util/query.scala

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ object query {
107107

108108
/** Just like `to` but allowing to alter `PreparedExecution`.
109109
*/
110-
def toAlteringExecution[F[_]](
110+
def toAlteringExecution[F[_], R](
111111
a: A,
112-
fn: PreparedExecution[F[B]] => PreparedExecution[F[B]]
113-
)(implicit f: FactoryCompat[B, F[B]]): ConnectionIO[F[B]] = {
112+
fn: PreparedExecution[F[B]] => PreparedExecution[R]
113+
)(implicit f: FactoryCompat[B, F[B]]): ConnectionIO[R] = {
114114
toConnectionIOAlteringExecution(a, IHRS.build[F, B], fn)
115115
}
116116

@@ -124,13 +124,13 @@ object query {
124124

125125
/** Just like `toMap` but allowing to alter `PreparedExecution`.
126126
*/
127-
def toMapAlteringExecution[K, V](
127+
def toMapAlteringExecution[K, V, R](
128128
a: A,
129-
fn: PreparedExecution[Map[K, V]] => PreparedExecution[Map[K, V]]
129+
fn: PreparedExecution[Map[K, V]] => PreparedExecution[R]
130130
)(implicit
131131
ev: B =:= (K, V),
132132
f: FactoryCompat[(K, V), Map[K, V]]
133-
): ConnectionIO[Map[K, V]] =
133+
): ConnectionIO[R] =
134134
toConnectionIOAlteringExecution(a, IHRS.buildPair[Map, K, V](f, read.map(ev)), fn)
135135

136136
/** Apply the argument `a` to construct a program in `[[doobie.free.connection.ConnectionIO ConnectionIO]]` yielding
@@ -142,10 +142,10 @@ object query {
142142

143143
/** Just like `accumulate` but allowing to alter `PreparedExecution`.
144144
*/
145-
def accumulateAlteringExecution[F[_]: Alternative](
145+
def accumulateAlteringExecution[F[_]: Alternative, R](
146146
a: A,
147-
fn: PreparedExecution[F[B]] => PreparedExecution[F[B]]
148-
): ConnectionIO[F[B]] =
147+
fn: PreparedExecution[F[B]] => PreparedExecution[R]
148+
): ConnectionIO[R] =
149149
toConnectionIOAlteringExecution(a, IHRS.accumulate[F, B], fn)
150150

151151
/** Apply the argument `a` to construct a program in `[[doobie.free.connection.ConnectionIO ConnectionIO]]` yielding
@@ -157,7 +157,7 @@ object query {
157157

158158
/** Just like `unique` but allowing to alter `PreparedExecution`.
159159
*/
160-
def uniqueAlteringExecution(a: A, fn: PreparedExecution[B] => PreparedExecution[B]): ConnectionIO[B] =
160+
def uniqueAlteringExecution[R](a: A, fn: PreparedExecution[B] => PreparedExecution[R]): ConnectionIO[R] =
161161
toConnectionIOAlteringExecution(a, IHRS.getUnique[B], fn)
162162

163163
/** Apply the argument `a` to construct a program in `[[doobie.free.connection.ConnectionIO ConnectionIO]]` yielding
@@ -169,10 +169,10 @@ object query {
169169

170170
/** Just like `option` but allowing to alter `PreparedExecution`.
171171
*/
172-
def optionAlteringExecution(
172+
def optionAlteringExecution[R](
173173
a: A,
174-
fn: PreparedExecution[Option[B]] => PreparedExecution[Option[B]]
175-
): ConnectionIO[Option[B]] =
174+
fn: PreparedExecution[Option[B]] => PreparedExecution[R]
175+
): ConnectionIO[R] =
176176
toConnectionIOAlteringExecution(a, IHRS.getOption[B], fn)
177177

178178
/** Apply the argument `a` to construct a program in `[[doobie.free.connection.ConnectionIO ConnectionIO]]` yielding
@@ -185,20 +185,20 @@ object query {
185185

186186
/** Just like `nel` but allowing to alter `PreparedExecution`.
187187
*/
188-
def nelAlteringExecution(
188+
def nelAlteringExecution[R](
189189
a: A,
190-
fn: PreparedExecution[NonEmptyList[B]] => PreparedExecution[NonEmptyList[B]]
191-
): ConnectionIO[NonEmptyList[B]] =
190+
fn: PreparedExecution[NonEmptyList[B]] => PreparedExecution[R]
191+
): ConnectionIO[R] =
192192
toConnectionIOAlteringExecution(a, IHRS.nel[B], fn)
193193

194194
private def toConnectionIO[C](a: A, rsio: ResultSetIO[C]): ConnectionIO[C] =
195195
IHC.executeWithResultSet(preparedExecution(sql, a, rsio), mkLoggingInfo(a))
196196

197-
private def toConnectionIOAlteringExecution[C](
197+
private def toConnectionIOAlteringExecution[C, R](
198198
a: A,
199199
rsio: ResultSetIO[C],
200-
fn: PreparedExecution[C] => PreparedExecution[C]
201-
): ConnectionIO[C] =
200+
fn: PreparedExecution[C] => PreparedExecution[R]
201+
): ConnectionIO[R] =
202202
IHC.executeWithResultSet(fn(preparedExecution(sql, a, rsio)), mkLoggingInfo(a))
203203

204204
private def preparedExecution[C](sql: String, a: A, rsio: ResultSetIO[C]): PreparedExecution[C] =

modules/core/src/test/scala/doobie/util/QuerySuite.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import doobie.implicits.*
1313

1414
class QuerySuite extends munit.CatsEffectSuite {
1515

16+
import doobie.FPS
17+
1618
val xa = Transactor.fromDriverManager[IO](
1719
driver = "org.h2.Driver",
1820
url = "jdbc:h2:mem:queryspec;DB_CLOSE_DELAY=-1",
@@ -64,7 +66,7 @@ class QuerySuite extends munit.CatsEffectSuite {
6466

6567
test("Query toAlteringExecution (result set operations)") {
6668
var didRun = false
67-
pairQuery.toAlteringExecution[List](
69+
pairQuery.toAlteringExecution[List, List[(String, Int)]](
6870
"x",
6971
{ preparedExec =>
7072
val process = IHRS.delay { didRun = true } *> preparedExec.process
@@ -78,7 +80,7 @@ class QuerySuite extends munit.CatsEffectSuite {
7880
}
7981
test("Query toMapAlteringExecution (result set operations)") {
8082
var didRun = false
81-
pairQuery.toMapAlteringExecution[String, Int](
83+
pairQuery.toMapAlteringExecution[String, Int, Map[String, Int]](
8284
"x",
8385
{ preparedExec =>
8486
val process = IHRS.delay { didRun = true } *> preparedExec.process
@@ -92,7 +94,7 @@ class QuerySuite extends munit.CatsEffectSuite {
9294
}
9395
test("Query accumulateAlteringExecution (result set operations)") {
9496
var didRun = false
95-
pairQuery.accumulateAlteringExecution[List](
97+
pairQuery.accumulateAlteringExecution[List, List[(String, Int)]](
9698
"x",
9799
{ preparedExec =>
98100
val process = IHRS.delay { didRun = true } *> preparedExec.process

0 commit comments

Comments
 (0)