-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move rebind to separate module and allow specifying variable name (#128)
This can make the generated SQL a bit easier to follow.
- Loading branch information
1 parent
0272bcc
commit 40994a7
Showing
5 changed files
with
49 additions
and
29 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
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,35 @@ | ||
{-# language FlexibleContexts #-} | ||
|
||
module Rel8.Query.Rebind | ||
( rebind | ||
) | ||
where | ||
|
||
-- base | ||
import Prelude | ||
|
||
-- 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 | ||
import qualified Opaleye.Internal.Unpackspec as Opaleye | ||
|
||
-- rel8 | ||
import Rel8.Expr ( Expr ) | ||
import Rel8.Query ( Query( Query ) ) | ||
import Rel8.Table ( Table ) | ||
import Rel8.Table.Opaleye ( unpackspec ) | ||
|
||
|
||
-- | 'rebind' takes a variable name, some expressions, and binds each of them | ||
-- to a new variable in the SQL. The @a@ returned consists only of these | ||
-- variables. It's essentially a @let@ binding for Postgres expressions. | ||
rebind :: Table Expr a => String -> a -> Query a | ||
rebind prefix a = Query $ \_ -> Opaleye.QueryArr $ \(_, tag) -> | ||
let | ||
tag' = Opaleye.next tag | ||
(a', bindings) = Opaleye.run $ | ||
Opaleye.runUnpackspec unpackspec (Opaleye.extractAttr prefix tag) a | ||
in | ||
((mempty, a'), \_ -> Opaleye.Rebind True bindings, tag') |