Last active
February 17, 2022 15:41
-
-
Save wpoosanguansit/ad61ee13e7c8b48b436a to your computer and use it in GitHub Desktop.
Doobie Nested Queries
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
selectEventOnlyFutureTarget(id: String, guid: String, timestamp: LocalDateTime, limit: Int): Process[ConnectionIO, Target] | |
selectEventOnlyPastTarget(id: String, guid: String, timestamp: LocalDateTime, limit: Int): Process[ConnectionIO, Target] | |
getSubTarget1(id: String, guid: String): ConnectionIO[List[SubTarget1]] | |
getSubTarget2(ooid: String, guid: String): Process[ConnectionIO, SubTarget2] | |
getSubTarget3(ooid: String, guid: String): Process[ConnectionIO, SubTarget3] | |
val statements = List[ConnectionIO[List[Target]]]( | |
selectEventOnlyFutureTarget(id, guid, timestamp, futureLimit).list, | |
selectEventOnlyPastTarget(id, guid, timestamp, pastLimit).list | |
) | |
val targets: ConnectionIO[List[Target]] = for { | |
tlss <- statements.sequenceU | |
tlssf <- tlss.flatten.point[ConnectionIO] | |
tls <- tlssf.traverse(tl => { | |
val st1 = getSubTarget1(id, tl.guid).transact(xa).run | |
val st2 = getSubTarget2(id, tl.guid).list.transact(xa).run | |
val st2 = getSubTarget3(id, tl.guid).list.transact(xa).run | |
tl.copy(subtarget1 = st1, | |
subtarget2 = if (!st2.isEmpty) Option(lks) else None, | |
subtarget3 = if (!st2.isEmpty) Option(mtg) else None | |
).point[ConnectionIO] | |
}) | |
} yield { | |
tls | |
} | |
val result: List[Target] = targets.transact(xa).run | |
result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment