on e4b4b63 help sign in
rel8/src/Rel8/Query/Set.hs
76 lines 2.7 kB history raw
1{-# language FlexibleContexts #-}
3module Rel8.Query.Set
4 ( union, unionAll
5 , intersect, intersectAll
6 , except, exceptAll
7 )
8where
10-- base
11import Prelude ()
13-- opaleye
14import qualified Opaleye.Binary as Opaleye
16-- rel8
17import Rel8.Expr ( Expr )
18import {-# SOURCE #-} Rel8.Query ( Query )
19import Rel8.Query.Opaleye ( zipOpaleyeWith )
20import Rel8.Table ( Table )
21import Rel8.Table.Eq ( EqTable )
22import Rel8.Table.Opaleye ( binaryspec )
25-- | Combine the results of two queries of the same type, collapsing
26-- duplicates. @union a b@ is the same as the SQL statement @x UNION b@.
27--
28-- >>> select c $ values [lit True, lit True, lit False] `union` values [lit True]
29-- [False,True]
30union :: EqTable a => Query a -> Query a -> Query a
31union = zipOpaleyeWith (Opaleye.unionExplicit binaryspec)
34-- | Combine the results of two queries of the same type, retaining duplicates.
35-- @unionAll a b@ is the same as the SQL statement @x UNION ALL b@.
36--
37-- >>> select c $ values [lit True, lit True, lit False] `unionAll` values [lit True]
38-- [True,True,False,True]
39unionAll :: Table Expr a => Query a -> Query a -> Query a
40unionAll = zipOpaleyeWith (Opaleye.unionAllExplicit binaryspec)
43-- | Find the intersection of two queries, collapsing duplicates. @intersect a
44-- b@ is the same as the SQL statement @x INTERSECT b@.
45--
46-- >>> select c $ values [lit True, lit True, lit False] `intersect` values [lit True]
47-- [True]
48intersect :: EqTable a => Query a -> Query a -> Query a
49intersect = zipOpaleyeWith (Opaleye.intersectExplicit binaryspec)
52-- | Find the intersection of two queries, retaining duplicates. @intersectAll
53-- a b@ is the same as the SQL statement @x INTERSECT ALL b@.
54--
55-- >>> select c $ values [lit True, lit True, lit False] `intersectAll` values [lit True, lit True]
56-- [True,True]
57intersectAll :: EqTable a => Query a -> Query a -> Query a
58intersectAll = zipOpaleyeWith (Opaleye.intersectAllExplicit binaryspec)
61-- | Find the difference of two queries, collapsing duplicates @except a b@ is
62-- the same as the SQL statement @x INTERSECT b@.
63--
64-- >>> select c $ values [lit True, lit False, lit False] `except` values [lit True]
65-- [False]
66except :: EqTable a => Query a -> Query a -> Query a
67except = zipOpaleyeWith (Opaleye.exceptExplicit binaryspec)
70-- | Find the difference of two queries, retaining duplicates. @exceptAll a b@
71-- is the same as the SQL statement @x EXCEPT ALL b@.
72--
73-- >>> select c $ values [lit True, lit False, lit False] `exceptAll` values [lit True]
74-- [False,False]
75exceptAll :: EqTable a => Query a -> Query a -> Query a
76exceptAll = zipOpaleyeWith (Opaleye.exceptAllExplicit binaryspec)