-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0e47ec
commit 7c33143
Showing
4 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Rel8.Query.Indexed | ||
( indexed | ||
) | ||
where | ||
|
||
-- base | ||
import Data.Int ( Int64 ) | ||
import Prelude | ||
|
||
-- opaleye | ||
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye | ||
import qualified Opaleye.Internal.PackMap as Opaleye | ||
import qualified Opaleye.Internal.PrimQuery as Opaleye | ||
import qualified Opaleye.Internal.QueryArr as Opaleye | ||
import qualified Opaleye.Internal.Tag as Opaleye | ||
|
||
-- rel8 | ||
import Rel8.Expr ( Expr ) | ||
import Rel8.Expr.Opaleye ( fromPrimExpr ) | ||
import Rel8.Query ( Query ) | ||
import Rel8.Query.Opaleye ( mapOpaleye ) | ||
|
||
|
||
-- | Pair each row of a query with its index within the query. | ||
indexed :: Query a -> Query (Expr Int64, a) | ||
indexed = mapOpaleye $ \(Opaleye.QueryArr f) -> Opaleye.QueryArr $ \(_, tag) -> | ||
let | ||
(a, query, tag') = f ((), tag) | ||
tag'' = Opaleye.next tag' | ||
window = Opaleye.ConstExpr $ Opaleye.OtherLit "ROW_NUMBER() OVER () - 1" | ||
(index, bindings) = Opaleye.run $ Opaleye.extractAttr "index" tag' window | ||
query' lateral = Opaleye.Rebind True bindings . query lateral | ||
in | ||
((fromPrimExpr index, a), query', tag'') |