-
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.
None of these functions actually worked (they would generate invalid SQL that referenced the inner tag column where it was no longer in scope). The root of the problem was in the Nullifiable instance for the Aggregate context. The new version works, at the cost of grouping by the tag `n + 1` times instead of just once (where `n` is the number of columns in the `MaybeTable`).
- Loading branch information
1 parent
35c6127
commit d8a6b4f
Showing
3 changed files
with
49 additions
and
38 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
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') |
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