mirror of
https://github.com/circuithub/rel8.git
synced 2024-09-11 16:05:41 +03:00
Export materialize
(#260)
The main reason I wasn't happy with this before is that there was nothing stopping you from writing `materialize query pure`. This returned query would produce invalid SQL if you actually tried to use it, because it would attempt to reference a common table expression outside the scope of the `WITH` statement. The "solution" here is just to throw a `rebind` around the result such that `materialize` incurs an extra `Table Expr b` constraint, which means that you can't return `Query (Query a)` because `Query a` can't satisfy a `Table Expr` constraint.
This commit is contained in:
parent
9f372dc649
commit
cdf0c761d3
@ -251,6 +251,9 @@ module Rel8
|
||||
, without
|
||||
, withoutBy
|
||||
|
||||
-- ** @WITH@
|
||||
, materialize
|
||||
|
||||
-- ** @WITH RECURSIVE@
|
||||
, loop
|
||||
|
||||
|
@ -33,11 +33,12 @@ import Rel8.Table.Opaleye ( unpackspec )
|
||||
-- 'materialize' to use the newer @WITH foo AS MATERIALIZED bar@ syntax
|
||||
-- introduced in PostgreSQL 12 in the future. Currently Rel8 does not use
|
||||
-- @AS MATERIALIZED@ to support earlier PostgreSQL versions.
|
||||
materialize :: Table Expr a => Query a -> (Query a -> Query b) -> Query b
|
||||
materialize :: (Table Expr a, Table Expr b)
|
||||
=> Query a -> (Query a -> Query b) -> Query b
|
||||
materialize query f =
|
||||
fromOpaleye $
|
||||
(>>= rebind "with") . fromOpaleye $
|
||||
withExplicit unpackspec
|
||||
(toOpaleye query')
|
||||
(toOpaleye . f . fromOpaleye)
|
||||
where
|
||||
query' = query >>= rebind "with"
|
||||
query' = query >>= rebind "materialize"
|
||||
|
@ -628,7 +628,7 @@ difference a b = a <* absent b
|
||||
|
||||
|
||||
-- | 'Q.materialize' for 'Tabulation's.
|
||||
materialize :: (Table Expr k, Table Expr a)
|
||||
materialize :: (Table Expr k, Table Expr a, Table Expr b)
|
||||
=> Tabulation k a -> (Tabulation k a -> Query b) -> Query b
|
||||
materialize tabulation f = case peek tabulation of
|
||||
Tabulation query -> do
|
||||
|
Loading…
Reference in New Issue
Block a user