on 40994a7 help sign in
rel8/src/Rel8/Query/Rebind.hs
35 lines 1.1 kB history raw
1{-# language FlexibleContexts #-}
3module Rel8.Query.Rebind
4 ( rebind
5 )
6where
8-- base
9import Prelude
11-- opaleye
12import qualified Opaleye.Internal.PackMap as Opaleye
13import qualified Opaleye.Internal.PrimQuery as Opaleye
14import qualified Opaleye.Internal.QueryArr as Opaleye
15import qualified Opaleye.Internal.Tag as Opaleye
16import qualified Opaleye.Internal.Unpackspec as Opaleye
18-- rel8
19import Rel8.Expr ( Expr )
20import Rel8.Query ( Query( Query ) )
21import Rel8.Table ( Table )
22import Rel8.Table.Opaleye ( unpackspec )
25-- | 'rebind' takes a variable name, some expressions, and binds each of them
26-- to a new variable in the SQL. The @a@ returned consists only of these
27-- variables. It's essentially a @let@ binding for Postgres expressions.
28rebind :: Table Expr a => String -> a -> Query a
29rebind prefix a = Query $ \_ -> Opaleye.QueryArr $ \(_, tag) ->
30 let
31 tag' = Opaleye.next tag
32 (a', bindings) = Opaleye.run $
33 Opaleye.runUnpackspec unpackspec (Opaleye.extractAttr prefix tag) a
34 in
35 ((mempty, a'), \_ -> Opaleye.Rebind True bindings, tag')